use of io.gravitee.rest.api.model.api.ApiQuery 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);
}
use of io.gravitee.rest.api.model.api.ApiQuery 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);
}
use of io.gravitee.rest.api.model.api.ApiQuery in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResource method executeCount.
private Analytics executeCount(AnalyticsParam analyticsParam, String extraFilter) {
CountQuery query = new CountQuery();
query.setFrom(analyticsParam.getFrom());
query.setTo(analyticsParam.getTo());
query.setInterval(analyticsParam.getInterval());
query.setQuery(analyticsParam.getQuery());
addExtraFilter(query, extraFilter);
switch(analyticsParam.getField()) {
case API_FIELD:
if (isAdmin()) {
return buildCountStat(apiService.search(new ApiQuery()).size());
} else {
return buildCountStat(apiService.findByUser(getAuthenticatedUser(), new ApiQuery(), false).size());
}
case APPLICATION_FIELD:
if (isAdmin()) {
return buildCountStat(applicationService.findAll().size());
} else {
return buildCountStat(applicationService.findByUser(getAuthenticatedUser()).size());
}
default:
return analyticsService.execute(query);
}
}
use of io.gravitee.rest.api.model.api.ApiQuery in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResource method getTopHitsAnalytics.
@NotNull
private TopHitsAnalytics getTopHitsAnalytics(Function<ApiEntity, String> groupingByFunction) {
Set<ApiEntity> apis = isAdmin() ? new HashSet<>(apiService.search(new ApiQuery())) : apiService.findByUser(getAuthenticatedUser(), new ApiQuery(), false);
Map<String, Long> collect = apis.stream().collect(Collectors.groupingBy(groupingByFunction, Collectors.counting()));
TopHitsAnalytics topHitsAnalytics = new TopHitsAnalytics();
topHitsAnalytics.setValues(collect);
return topHitsAnalytics;
}
use of io.gravitee.rest.api.model.api.ApiQuery in project gravitee-management-rest-api by gravitee-io.
the class ApisResourceTest method shouldGetPublishedApi.
@Test
public void shouldGetPublishedApi() {
final Response response = target().queryParam("context-path", "context-path").queryParam("label", "label").queryParam("version", "version").queryParam("name", "name").queryParam("tag", "tag").request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
ArgumentCaptor<ApiQuery> queryCaptor = ArgumentCaptor.forClass(ApiQuery.class);
Mockito.verify(apiService).findPublishedByUser(eq(USER_NAME), queryCaptor.capture());
final ApiQuery query = queryCaptor.getValue();
assertEquals("context-path", query.getContextPath());
assertEquals("label", query.getLabel());
assertEquals("version", query.getVersion());
assertEquals("name", query.getName());
assertEquals("tag", query.getTag());
ArgumentCaptor<String> basePathCaptor = ArgumentCaptor.forClass(String.class);
Mockito.verify(apiMapper, Mockito.times(5)).computeApiLinks(basePathCaptor.capture(), ArgumentCaptor.forClass(Date.class).capture());
final String expectedBasePath = target().getUri().toString();
final List<String> bastPathList = basePathCaptor.getAllValues();
assertTrue(bastPathList.contains(expectedBasePath + "/1"));
assertTrue(bastPathList.contains(expectedBasePath + "/3"));
assertTrue(bastPathList.contains(expectedBasePath + "/4"));
assertTrue(bastPathList.contains(expectedBasePath + "/5"));
assertTrue(bastPathList.contains(expectedBasePath + "/6"));
ArgumentCaptor<ApiEntity> apiEntityCaptor = ArgumentCaptor.forClass(ApiEntity.class);
Mockito.verify(apiMapper, Mockito.times(5)).convert(apiEntityCaptor.capture());
final List<String> allNameValues = apiEntityCaptor.getAllValues().stream().map(a -> a.getName()).collect(Collectors.toList());
assertEquals(5, allNameValues.size());
assertTrue(allNameValues.containsAll(Arrays.asList("1", "3", "4", "5", "6")));
ApisResponse apiResponse = response.readEntity(ApisResponse.class);
assertEquals(5, apiResponse.getData().size());
}
Aggregations