Search in sources :

Example 6 with Rating

use of io.gravitee.rest.api.portal.rest.model.Rating in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingsResource method getApiRatingsByApiId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getApiRatingsByApiId(@PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam, @QueryParam("mine") Boolean mine, @QueryParam("order") String order) {
    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))) {
        List<Rating> ratings;
        if (mine != null && mine == true) {
            RatingEntity ratingEntity = ratingService.findByApiForConnectedUser(apiId);
            if (ratingEntity != null) {
                ratings = Arrays.asList(ratingMapper.convert(ratingEntity, uriInfo));
            } else {
                ratings = Collections.emptyList();
            }
        } else {
            final List<RatingEntity> ratingEntities = ratingService.findByApi(apiId);
            Stream<Rating> ratingStream = ratingEntities.stream().map((RatingEntity ratingEntity) -> ratingMapper.convert(ratingEntity, uriInfo));
            if (order != null) {
                ratingStream = ratingStream.sorted(new RatingComparator(order));
            }
            ratings = ratingStream.collect(toList());
        }
        return createListResponse(ratings, paginationParam, true);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) Rating(io.gravitee.rest.api.portal.rest.model.Rating) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Aggregations

Rating (io.gravitee.rest.api.portal.rest.model.Rating)6 RatingEntity (io.gravitee.rest.api.model.RatingEntity)5 Test (org.junit.Test)4 RatingAnswerEntity (io.gravitee.rest.api.model.RatingAnswerEntity)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)3 List (java.util.List)3 Response (javax.ws.rs.core.Response)3 UserEntity (io.gravitee.rest.api.model.UserEntity)2 Error (io.gravitee.rest.api.portal.rest.model.Error)2 RatingAnswer (io.gravitee.rest.api.portal.rest.model.RatingAnswer)2 RatingInput (io.gravitee.rest.api.portal.rest.model.RatingInput)2 User (io.gravitee.rest.api.portal.rest.model.User)2 Arrays (java.util.Arrays)2 Collections.singletonList (java.util.Collections.singletonList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Entity (javax.ws.rs.client.Entity)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2