Search in sources :

Example 1 with Rating

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

the class RatingServiceImpl method findSummaryByApi.

@Override
public RatingSummaryEntity findSummaryByApi(final String api) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final List<Rating> ratings = ratingRepository.findByApi(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.management.service.exceptions.ApiRatingUnavailableException) OptionalDouble(java.util.OptionalDouble) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 2 with Rating

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

the class RatingServiceImpl method createAnswer.

@Override
public RatingEntity createAnswer(final NewRatingAnswerEntity answerEntity) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findById(answerEntity.getRatingId());
        final RatingAnswer ratingAnswer = new RatingAnswer();
        ratingAnswer.setId(UUID.toString(UUID.random()));
        ratingAnswer.setRating(answerEntity.getRatingId());
        ratingAnswer.setUser(getAuthenticatedUsername());
        ratingAnswer.setComment(answerEntity.getComment());
        ratingAnswer.setCreatedAt(new Date());
        ratingAnswerRepository.create(ratingAnswer);
        auditService.createApiAuditLog(rating.getApi(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_CREATED, ratingAnswer.getCreatedAt(), null, ratingAnswer);
        return convert(rating);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to create a rating answer on rating {}", answerEntity.getRatingId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to create a rating answer on rating" + answerEntity.getRatingId(), ex);
    }
}
Also used : RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with Rating

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

the class RatingServiceImpl method delete.

@Override
public void delete(final String id) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        Rating rating = findById(id);
        ratingRepository.delete(id);
        auditService.createApiAuditLog(rating.getApi(), 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.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 4 with Rating

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

the class RatingServiceImpl method create.

@Override
public RatingEntity create(final NewRatingEntity ratingEntity) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Optional<Rating> ratingOptional = ratingRepository.findByApiAndUser(ratingEntity.getApi(), getAuthenticatedUsername());
        if (ratingOptional.isPresent()) {
            throw new RatingAlreadyExistsException(ratingEntity.getApi(), getAuthenticatedUsername());
        }
        Rating rating = ratingRepository.create(convert(ratingEntity));
        auditService.createApiAuditLog(rating.getApi(), null, Rating.RatingEvent.RATING_CREATED, rating.getCreatedAt(), null, rating);
        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.management.service.exceptions.RatingAlreadyExistsException) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 5 with Rating

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

the class RatingServiceImpl method update.

@Override
public RatingEntity update(final UpdateRatingEntity ratingEntity) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Rating rating = findById(ratingEntity.getId());
        final Rating oldRating = new Rating(rating);
        if (!rating.getApi().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.getApi(), 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.management.service.exceptions.RatingNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

Rating (io.gravitee.repository.management.model.Rating)12 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)8 Date (java.util.Date)7 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)3 RatingAnswer (io.gravitee.repository.management.model.RatingAnswer)3 Test (org.junit.Test)3 Page (io.gravitee.common.data.domain.Page)2 RatingAlreadyExistsException (io.gravitee.management.service.exceptions.RatingAlreadyExistsException)2 RatingNotFoundException (io.gravitee.management.service.exceptions.RatingNotFoundException)2 Pageable (io.gravitee.repository.management.api.search.Pageable)2 OptionalDouble (java.util.OptionalDouble)2 UUID (io.gravitee.common.utils.UUID)1 io.gravitee.management.model (io.gravitee.management.model)1 AuditService (io.gravitee.management.service.AuditService)1 RatingService (io.gravitee.management.service.RatingService)1 UserService (io.gravitee.management.service.UserService)1 RatingAnswerRepository (io.gravitee.repository.management.api.RatingAnswerRepository)1 RatingRepository (io.gravitee.repository.management.api.RatingRepository)1