Search in sources :

Example 1 with ApiRatingUnavailableException

use of io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findByApi.

@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Page<Rating> pageRating = ratingRepository.findByReferenceIdAndReferenceTypePageable(api, RatingReferenceType.API, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
        final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
        return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) Page(io.gravitee.common.data.domain.Page) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with ApiRatingUnavailableException

use of io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method update.

@Override
public RatingEntity update(final UpdateRatingEntity ratingEntity) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findModelById(ratingEntity.getId());
        final Rating oldRating = new Rating(rating);
        if (!rating.getReferenceId().equals(ratingEntity.getApi())) {
            throw new RatingNotFoundException(ratingEntity.getId(), ratingEntity.getApi());
        }
        final Date now = new Date();
        rating.setUpdatedAt(now);
        rating.setRate(ratingEntity.getRate());
        // we can save a title/comment only once
        if (isBlank(rating.getTitle())) {
            rating.setTitle(ratingEntity.getTitle());
        }
        if (isBlank(rating.getComment())) {
            rating.setComment(ratingEntity.getComment());
        }
        Rating updatedRating = ratingRepository.update(rating);
        auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_UPDATED, updatedRating.getUpdatedAt(), oldRating, updatedRating);
        return convert(updatedRating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to update rating {}", ratingEntity.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to update rating " + ratingEntity.getId(), ex);
    }
}
Also used : RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with ApiRatingUnavailableException

use of io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method delete.

@Override
public void delete(final String id) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        Rating rating = findModelById(id);
        ratingRepository.delete(id);
        auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_DELETED, new Date(), rating, null);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete rating {}", id, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete rating " + id, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with ApiRatingUnavailableException

use of io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findSummaryByApi.

@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByReferenceIdAndReferenceType(api, RatingReferenceType.API);
        final RatingSummaryEntity ratingSummary = new RatingSummaryEntity();
        ratingSummary.setApi(api);
        ratingSummary.setNumberOfRatings(ratings.size());
        final OptionalDouble optionalAvg = ratings.stream().mapToInt(Rating::getRate).average();
        if (optionalAvg.isPresent()) {
            ratingSummary.setAverageRate(optionalAvg.getAsDouble());
        }
        ratingSummary.setNumberOfRatingsByRate(ratings.stream().collect(groupingBy(Rating::getRate, counting())));
        return ratingSummary;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find summary rating for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find summary rating for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) OptionalDouble(java.util.OptionalDouble) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with ApiRatingUnavailableException

use of io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method create.

@Override
public RatingEntity create(final NewRatingEntity ratingEntity) {
    if (!isEnabled()) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Optional<Rating> ratingOptional = ratingRepository.findByReferenceIdAndReferenceTypeAndUser(ratingEntity.getApi(), RatingReferenceType.API, getAuthenticatedUsername());
        if (ratingOptional.isPresent()) {
            throw new RatingAlreadyExistsException(ratingEntity.getApi(), getAuthenticatedUsername());
        }
        Rating rating = ratingRepository.create(convert(ratingEntity));
        auditService.createApiAuditLog(rating.getReferenceId(), null, Rating.RatingEvent.RATING_CREATED, rating.getCreatedAt(), null, rating);
        notifierService.trigger(ApiHook.NEW_RATING, rating.getReferenceId(), new NotificationParamsBuilder().api(apiService.findById(rating.getReferenceId())).build());
        return convert(rating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create rating on api {}", ratingEntity.getApi(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create rating on api " + ratingEntity.getApi(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) RatingAlreadyExistsException(io.gravitee.rest.api.service.exceptions.RatingAlreadyExistsException) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)8 Rating (io.gravitee.repository.management.model.Rating)8 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)8 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)8 Date (java.util.Date)4 NotificationParamsBuilder (io.gravitee.rest.api.service.notification.NotificationParamsBuilder)2 Page (io.gravitee.common.data.domain.Page)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 RatingAnswer (io.gravitee.repository.management.model.RatingAnswer)1 RatingAlreadyExistsException (io.gravitee.rest.api.service.exceptions.RatingAlreadyExistsException)1 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)1 OptionalDouble (java.util.OptionalDouble)1