Search in sources :

Example 1 with RatingNotFoundException

use of io.gravitee.rest.api.service.exceptions.RatingNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingAnswerResource method deleteApiRatingAnswer.

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.DELETE) })
public Response deleteApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @PathParam("answerId") String answerId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            if (ratingEntity.getAnswers().stream().anyMatch(answer -> answer.getId().equals(answerId))) {
                ratingService.deleteAnswer(ratingId, answerId);
                return Response.status(Status.NO_CONTENT).build();
            }
            throw new RatingAnswerNotFoundException(answerId, ratingId, apiId);
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) RatingAnswerNotFoundException(io.gravitee.rest.api.service.exceptions.RatingAnswerNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 2 with RatingNotFoundException

use of io.gravitee.rest.api.service.exceptions.RatingNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingResource method deleteApiRating.

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.DELETE) })
public Response deleteApiRating(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId) {
    // FIXME: are we sure we need to fetch the api while the permission system alreay allowed the user to delete the rating ?
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            ratingService.delete(ratingId);
            return Response.status(Status.NO_CONTENT).build();
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 3 with RatingNotFoundException

use of io.gravitee.rest.api.service.exceptions.RatingNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingResource method updateApiRating.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.UPDATE) })
public Response updateApiRating(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @Valid RatingInput ratingInput) {
    if (ratingInput == null) {
        throw new BadRequestException("Input must not be null.");
    }
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            UpdateRatingEntity rating = new UpdateRatingEntity();
            rating.setId(ratingId);
            rating.setApi(apiId);
            rating.setComment(ratingInput.getComment());
            rating.setTitle(ratingInput.getTitle());
            rating.setRate(ratingInput.getValue().byteValue());
            RatingEntity updatedRating = ratingService.update(rating);
            return Response.status(Status.OK).entity(ratingMapper.convert(updatedRating, uriInfo)).build();
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 4 with RatingNotFoundException

use of io.gravitee.rest.api.service.exceptions.RatingNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method update.

@Override
public RatingEntity update(final UpdateRatingEntity ratingEntity) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findModelById(ratingEntity.getId());
        final Rating oldRating = new Rating(rating);
        if (!rating.getReferenceId().equals(ratingEntity.getApi())) {
            throw new RatingNotFoundException(ratingEntity.getId(), ratingEntity.getApi());
        }
        final Date now = new Date();
        rating.setUpdatedAt(now);
        rating.setRate(ratingEntity.getRate());
        // we can save a title/comment only once
        if (isBlank(rating.getTitle())) {
            rating.setTitle(ratingEntity.getTitle());
        }
        if (isBlank(rating.getComment())) {
            rating.setComment(ratingEntity.getComment());
        }
        Rating updatedRating = ratingRepository.update(rating);
        auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_UPDATED, updatedRating.getUpdatedAt(), oldRating, updatedRating);
        return convert(updatedRating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update rating {}", ratingEntity.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update rating " + ratingEntity.getId(), ex);
    }
}
Also used : RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with RatingNotFoundException

use of io.gravitee.rest.api.service.exceptions.RatingNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingAnswersResource method createApiRatingAnswer.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.CREATE) })
public Response createApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @Valid RatingAnswerInput ratingAnswerInput) {
    if (ratingAnswerInput == null) {
        throw new BadRequestException("Input must not be null.");
    }
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            NewRatingAnswerEntity ratingAnswerEntity = new NewRatingAnswerEntity();
            ratingAnswerEntity.setComment(ratingAnswerInput.getComment());
            ratingAnswerEntity.setRatingId(ratingId);
            RatingEntity updatedRating = ratingService.createAnswer(ratingAnswerEntity);
            return Response.status(Status.CREATED).entity(ratingMapper.convert(updatedRating, uriInfo)).build();
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) NewRatingAnswerEntity(io.gravitee.rest.api.model.NewRatingAnswerEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Aggregations

RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)5 RatingEntity (io.gravitee.rest.api.model.RatingEntity)4 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)4 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)4 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)4 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)4 UpdateRatingEntity (io.gravitee.rest.api.model.UpdateRatingEntity)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Rating (io.gravitee.repository.management.model.Rating)1 NewRatingAnswerEntity (io.gravitee.rest.api.model.NewRatingAnswerEntity)1 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)1 RatingAnswerNotFoundException (io.gravitee.rest.api.service.exceptions.RatingAnswerNotFoundException)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Date (java.util.Date)1 DELETE (javax.ws.rs.DELETE)1 Produces (javax.ws.rs.Produces)1