Search in sources :

Example 21 with ApiQuery

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

Example 22 with ApiQuery

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

Example 23 with ApiQuery

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);
    }
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) CountQuery(io.gravitee.rest.api.model.analytics.query.CountQuery)

Example 24 with ApiQuery

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;
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with ApiQuery

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());
}
Also used : Response(javax.ws.rs.core.Response) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) ApiLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) Response(javax.ws.rs.core.Response) Assert(org.junit.Assert) Mockito.doReturn(org.mockito.Mockito.doReturn) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Aggregations

ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)26 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)19 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)17 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)8 Produces (javax.ws.rs.Produces)7 Response (javax.ws.rs.core.Response)7 RatingEntity (io.gravitee.rest.api.model.RatingEntity)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 MediaType (io.gravitee.common.http.MediaType)5 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)5 GET (javax.ws.rs.GET)5 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)4 Collections (java.util.Collections)4 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)3 java.util (java.util)3 List (java.util.List)3 Context (javax.ws.rs.core.Context)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 NewRatingEntity (io.gravitee.rest.api.model.NewRatingEntity)2