Skip to content

Commit

Permalink
Merge pull request #6551 from ita-social-projects/user-ratings
Browse files Browse the repository at this point in the history
[GreenCity] Status code 500 when we try to add comment to eco news or events #6550
  • Loading branch information
ABbondar authored Oct 11, 2023
2 parents 6985249 + 29cf5c0 commit af0de45
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import greencity.dto.eventcomment.AddEventCommentDtoRequest;
import greencity.dto.eventcomment.AddEventCommentDtoResponse;
import greencity.dto.eventcomment.EventCommentDto;
import greencity.dto.eventcomment.EventCommentVO;
import greencity.dto.user.UserVO;
import greencity.service.EventCommentService;
import io.swagger.annotations.ApiOperation;
Expand All @@ -34,12 +35,11 @@ public class EventCommentController {
private final EventCommentService eventCommentService;

/**
* Method for creating {@link greencity.dto.eventcomment.EventCommentVO}.
* Method for creating {@link EventCommentVO}.
*
* @param eventId id of {@link greencity.dto.event.EventVO} to add comment to.
* @param request - dto for {@link greencity.dto.eventcomment.EventCommentVO}
* entity.
* @return dto {@link greencity.dto.eventcomment.AddEventCommentDtoResponse}
* @param eventId id of {@link EventVO} to add comment to.
* @param request dto for {@link EventCommentVO} entity.
* @return dto {@link AddEventCommentDtoResponse}
*/
@ApiOperation(value = "Add comment.")
@ResponseStatus(value = HttpStatus.CREATED)
Expand Down
12 changes: 12 additions & 0 deletions dao/src/main/java/greencity/repository/UserRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,16 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
+ " SELECT friend_id AS id FROM users_friends WHERE user_id = :userId AND status = 'FRIEND' "
+ ") AND LOWER(u.name) LIKE LOWER(CONCAT('%', :filteringName, '%'))")
Page<User> findAllFriendsOfUser(Long userId, String filteringName, Pageable pageable);

/**
* Method that update user's rating.
*
* @param userId current user's id.
* @param rating rating.
*
* @author Anton Bondar.
*/
@Modifying
@Query(nativeQuery = true, value = "UPDATE users SET rating = :rating WHERE id = :userId")
void updateUserRating(Long userId, Double rating);
}
10 changes: 10 additions & 0 deletions service-api/src/main/java/greencity/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,14 @@ public interface UserService {
* @param criteria value which we used to filter users.
*/
PageableDto<UserManagementVO> getAllUsersByCriteria(String criteria, String role, String status, Pageable pageable);

/**
* Method that update user's rating.
*
* @param userId current user's id.
* @param rating rating.
*
* @author Anton Bondar.
*/
void updateUserRating(Long userId, Double rating);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RatingCalculation {
public void ratingCalculation(RatingCalculationEnum rating, UserVO userVo) {
User user = modelMapper.map(userVo, User.class);
userVo.setRating(userVo.getRating() + rating.getRatingPoints());
userService.save(userVo);
userService.updateUserRating(user.getId(), userVo.getRating());
RatingStatistics ratingStatistics = RatingStatistics
.builder()
.rating(userVo.getRating())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import greencity.constant.CacheConstants;
import greencity.constant.ErrorMessage;
import greencity.dto.PageableAdvancedDto;
import greencity.dto.achievement.*;
import greencity.dto.achievement.AchievementManagementDto;
import greencity.dto.achievement.AchievementPostDto;
import greencity.dto.achievement.AchievementVO;
import greencity.dto.achievement.UserAchievementVO;
import greencity.dto.achievementcategory.AchievementCategoryVO;
import greencity.dto.user.UserVO;
import greencity.dto.useraction.UserActionVO;
Expand Down Expand Up @@ -75,7 +78,7 @@ public AchievementVO save(AchievementPostDto achievementPostDto) {
}
userVO.getUserAchievements().add(userAchievementVO);
userAchievementVO.setUser(userVO);
userService.save(userVO);
userService.updateUserRating(userVO.getId(), userVO.getRating());
});
return achievementVO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public Set<EventAttenderDto> getAllEventAttenders(Long eventId) {
@Override
public EventVO findById(Long eventId) {
Event event = eventRepo.findById(eventId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.ECO_NEWS_NOT_FOUND_BY_ID + eventId));
.orElseThrow(() -> new NotFoundException(ErrorMessage.EVENT_NOT_FOUND_BY_ID + eventId));
return modelMapper.map(event, EventVO.class);
}

Expand Down
15 changes: 14 additions & 1 deletion service/src/main/java/greencity/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import greencity.constant.LogMessage;
import greencity.dto.PageableDto;
import greencity.dto.filter.UserFilterDto;
import greencity.dto.user.*;
import greencity.dto.user.UserManagementVO;
import greencity.dto.user.UserRoleDto;
import greencity.dto.user.UserStatusDto;
import greencity.dto.user.UserVO;
import greencity.entity.User;
import greencity.enums.Role;
import greencity.enums.UserStatus;
import greencity.exception.exceptions.BadUpdateRequestException;
import greencity.exception.exceptions.LowRoleLevelException;
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.WrongEmailException;
import greencity.exception.exceptions.WrongIdException;
import greencity.repository.UserRepo;
Expand Down Expand Up @@ -235,6 +239,15 @@ public PageableDto<UserManagementVO> getAllUsersByCriteria(String criteria, Stri
listOfUsers.getTotalPages());
}

@Override
@Transactional
public void updateUserRating(Long userId, Double rating) {
if (userRepo.findById(userId).isEmpty()) {
throw new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_ID + userId);
}
userRepo.updateUserRating(userId, rating);
}

private UserFilterDto createUserFilterDto(String criteria, String role, String status) {
if (status != null) {
status = status.equals("all") ? null : status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import greencity.ModelUtils;
import greencity.enums.RatingCalculationEnum;
import greencity.client.RestClient;
import greencity.dto.ratingstatistics.RatingStatisticsVO;
import greencity.dto.user.UserVO;
import greencity.entity.RatingStatistics;
Expand All @@ -18,9 +17,9 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.modelmapper.ModelMapper;

import javax.servlet.http.HttpServletRequest;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class RatingCalculationTest {
Expand All @@ -36,7 +35,6 @@ class RatingCalculationTest {

@Test
void ratingCalculation() {

RatingCalculationEnum rating = RatingCalculationEnum.COMMENT_OR_REPLY;
User user = ModelUtils.getUser();
user.setRating(1D);
Expand All @@ -60,11 +58,15 @@ void ratingCalculation() {
.pointsChanged(rating.getRatingPoints())
.build();
when(modelMapper.map(userVO, User.class)).thenReturn(user);
doNothing().when(userService).save(userVO);
doNothing().when(userService).updateUserRating(1L, 6.0d);
when(modelMapper.map(ratingStatistics, RatingStatisticsVO.class)).thenReturn(ratingStatisticsVO);
when(ratingStatisticsService.save(ratingStatisticsVO)).thenReturn(ratingStatisticsVO);

ratingCalculation.ratingCalculation(RatingCalculationEnum.COMMENT_OR_REPLY, userVO);

verify(modelMapper).map(userVO, User.class);
verify(userService).updateUserRating(1L, 6.0d);
verify(modelMapper).map(ratingStatistics, RatingStatisticsVO.class);
verify(ratingStatisticsService).save(ratingStatisticsVO);
verify(userService).save(userVO);
}
}
46 changes: 42 additions & 4 deletions service/src/test/java/greencity/service/UserServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import greencity.enums.EmailNotification;
import greencity.enums.Role;
import greencity.exception.exceptions.BadUpdateRequestException;
import greencity.exception.exceptions.LowRoleLevelException;
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.WrongEmailException;
import greencity.exception.exceptions.WrongIdException;
import greencity.enums.UserStatus;
import greencity.exception.exceptions.*;
import greencity.repository.UserRepo;
import greencity.repository.options.UserFilter;
import org.junit.jupiter.api.Test;
Expand All @@ -36,12 +38,29 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static greencity.ModelUtils.*;
import static greencity.ModelUtils.TEST_EMAIL;
import static greencity.ModelUtils.TEST_EMAIL_2;
import static greencity.ModelUtils.TEST_USER;
import static greencity.ModelUtils.TEST_USER_ROLE_USER;
import static greencity.ModelUtils.TEST_USER_STATUS_DTO;
import static greencity.ModelUtils.TEST_USER_VO;
import static greencity.ModelUtils.TEST_USER_VO_ROLE_USER;
import static greencity.enums.UserStatus.ACTIVATED;
import static greencity.enums.UserStatus.CREATED;
import static org.junit.jupiter.api.Assertions.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
class UserServiceImplTest {
Expand Down Expand Up @@ -277,4 +296,23 @@ void getAllUsersByCriteriaTest() {
assertTrue(allUsersByCriteria.getPage().contains(userManagementVO));
verify(userRepo, times(1)).findAllManagementVo(any(UserFilter.class), eq(pageable));
}

@Test
void updateUserRatingTest() {
User user = ModelUtils.getUser();
when(userRepo.findById(1L)).thenReturn(Optional.of(user));
doNothing().when(userRepo).updateUserRating(1L, 6.0d);

userService.updateUserRating(1L, 6.0d);

verify(userRepo).findById(1L);
verify(userRepo).updateUserRating(1L, 6.0d);
}

@Test
void updateUserRatingsThrowsNotFoundExceptionTest() {
when(userRepo.findById(1L)).thenReturn(Optional.empty());
assertThrows(NotFoundException.class, () -> userService.updateUserRating(1L, 6.0d));
verify(userRepo).findById(1L);
}
}

0 comments on commit af0de45

Please sign in to comment.