use of io.gravitee.rest.api.service.exceptions.RatingAlreadyExistsException 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);
}
}
Aggregations