Search in sources :

Example 1 with UpdateRatingEntity

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

Aggregations

RatingEntity (io.gravitee.rest.api.model.RatingEntity)1 UpdateRatingEntity (io.gravitee.rest.api.model.UpdateRatingEntity)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