Search in sources :

Example 1 with RatingAnswer

use of io.gravitee.repository.management.model.RatingAnswer 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 2 with RatingAnswer

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

the class RatingServiceTest method shouldCreateAnswer.

@Test
public void shouldCreateAnswer() throws TechnicalException {
    final Rating updatedRating = mock(Rating.class);
    when(updatedRating.getApi()).thenReturn(API_ID);
    when(updatedRating.getTitle()).thenReturn(TITLE);
    when(updatedRating.getComment()).thenReturn(COMMENT);
    when(updatedRating.getRate()).thenReturn(RATE);
    when(updatedRating.getUser()).thenReturn(USER);
    when(ratingAnswer.getUser()).thenReturn(USER);
    when(ratingAnswer.getComment()).thenReturn(ANSWER);
    when(ratingAnswer.getCreatedAt()).thenReturn(new Date());
    when(ratingAnswerRepository.findByRating(RATING_ID)).thenReturn(singletonList(ratingAnswer));
    when(newRatingAnswerEntity.getRatingId()).thenReturn(RATING_ID);
    when(newRatingAnswerEntity.getComment()).thenReturn(COMMENT);
    when(ratingRepository.findById(RATING_ID)).thenReturn(of(rating));
    final RatingEntity ratingEntity = ratingService.createAnswer(newRatingAnswerEntity);
    verify(ratingAnswerRepository).create(any(RatingAnswer.class));
    assertEquals(USER, ratingEntity.getUser());
    assertEquals(API_ID, ratingEntity.getApi());
    assertEquals(TITLE, ratingEntity.getTitle());
    assertEquals(COMMENT, ratingEntity.getComment());
    assertEquals(RATE, ratingEntity.getRate(), 0);
    assertEquals(ratingEntity.getCreatedAt(), ratingEntity.getUpdatedAt());
    assertEquals(ANSWER, ratingEntity.getAnswers().get(0).getComment());
    assertEquals(USER, ratingEntity.getAnswers().get(0).getUser());
    assertNotNull(ratingEntity.getAnswers().get(0).getCreatedAt());
}
Also used : RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) Rating(io.gravitee.repository.management.model.Rating) Date(java.util.Date) Test(org.junit.Test)

Example 3 with RatingAnswer

use of io.gravitee.repository.management.model.RatingAnswer 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)

Aggregations

Rating (io.gravitee.repository.management.model.Rating)3 RatingAnswer (io.gravitee.repository.management.model.RatingAnswer)3 Date (java.util.Date)3 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)2 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Page (io.gravitee.common.data.domain.Page)1 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 RatingAlreadyExistsException (io.gravitee.management.service.exceptions.RatingAlreadyExistsException)1 RatingNotFoundException (io.gravitee.management.service.exceptions.RatingNotFoundException)1 RatingAnswerRepository (io.gravitee.repository.management.api.RatingAnswerRepository)1 RatingRepository (io.gravitee.repository.management.api.RatingRepository)1 Pageable (io.gravitee.repository.management.api.search.Pageable)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 Comparator.comparing (java.util.Comparator.comparing)1 Comparator.reverseOrder (java.util.Comparator.reverseOrder)1