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);
}
Aggregations