Search in sources :

Example 11 with Rating

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

the class RatingServiceImpl method convert.

private RatingEntity convert(final Rating rating) {
    final RatingEntity ratingEntity = new RatingEntity();
    final UserEntity user = userService.findById(rating.getUser());
    ratingEntity.setUser(user.getId());
    if (user.getFirstname() != null && user.getLastname() != null) {
        ratingEntity.setUserDisplayName(user.getFirstname() + ' ' + user.getLastname());
    } else {
        ratingEntity.setUserDisplayName(user.getUsername());
    }
    ratingEntity.setId(rating.getId());
    ratingEntity.setApi(rating.getApi());
    ratingEntity.setTitle(rating.getTitle());
    ratingEntity.setComment(rating.getComment());
    ratingEntity.setRate(rating.getRate());
    ratingEntity.setCreatedAt(rating.getCreatedAt());
    ratingEntity.setUpdatedAt(rating.getUpdatedAt());
    try {
        final List<RatingAnswer> ratingAnswers = ratingAnswerRepository.findByRating(rating.getId());
        if (ratingAnswers != null) {
            ratingEntity.setAnswers(ratingAnswers.stream().map(ratingAnswer -> {
                final RatingAnswerEntity ratingAnswerEntity = new RatingAnswerEntity();
                ratingAnswerEntity.setId(ratingAnswer.getId());
                final UserEntity userAnswer = userService.findById(ratingAnswer.getUser());
                ratingAnswerEntity.setUser(userAnswer.getId());
                if (userAnswer.getFirstname() != null && userAnswer.getLastname() != null) {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getFirstname() + ' ' + userAnswer.getLastname());
                } else {
                    ratingAnswerEntity.setUserDisplayName(userAnswer.getUsername());
                }
                ratingAnswerEntity.setComment(ratingAnswer.getComment());
                ratingAnswerEntity.setCreatedAt(ratingAnswer.getCreatedAt());
                return ratingAnswerEntity;
            }).sorted(comparing(RatingAnswerEntity::getCreatedAt, reverseOrder())).collect(toList()));
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find rating answers by rating id {}", rating.getId(), ex);
        throw new TechnicalManagementException("An error occurred while trying to find rating answers by rating id " + rating.getId(), ex);
    }
    return ratingEntity;
}
Also used : Rating(io.gravitee.repository.management.model.Rating) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) RatingAlreadyExistsException(io.gravitee.management.service.exceptions.RatingAlreadyExistsException) Date(java.util.Date) Page(io.gravitee.common.data.domain.Page) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) OptionalDouble(java.util.OptionalDouble) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.management.service.AuditService) Value(org.springframework.beans.factory.annotation.Value) UUID(io.gravitee.common.utils.UUID) io.gravitee.management.model(io.gravitee.management.model) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) RatingAnswerRepository(io.gravitee.repository.management.api.RatingAnswerRepository) Comparator.comparing(java.util.Comparator.comparing) Logger(org.slf4j.Logger) RatingService(io.gravitee.management.service.RatingService) RatingRepository(io.gravitee.repository.management.api.RatingRepository) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Pageable(io.gravitee.repository.management.api.search.Pageable) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) UserService(io.gravitee.management.service.UserService) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Optional(java.util.Optional) Comparator.reverseOrder(java.util.Comparator.reverseOrder) RatingNotFoundException(io.gravitee.management.service.exceptions.RatingNotFoundException) RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 12 with Rating

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

the class RatingServiceImpl method convert.

private Rating convert(final NewRatingEntity ratingEntity) {
    final Rating rating = new Rating();
    rating.setId(UUID.toString(UUID.random()));
    rating.setApi(ratingEntity.getApi());
    rating.setRate(ratingEntity.getRate());
    rating.setTitle(ratingEntity.getTitle());
    rating.setComment(ratingEntity.getComment());
    rating.setUser(getAuthenticatedUsername());
    final Date now = new Date();
    rating.setCreatedAt(now);
    rating.setUpdatedAt(now);
    return rating;
}
Also used : Rating(io.gravitee.repository.management.model.Rating) Date(java.util.Date)

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