Search in sources :

Example 1 with NewRatingAnswerEntity

use of io.gravitee.rest.api.model.NewRatingAnswerEntity 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

NewRatingAnswerEntity (io.gravitee.rest.api.model.NewRatingAnswerEntity)1 RatingEntity (io.gravitee.rest.api.model.RatingEntity)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)1 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)1 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)1