diff --git a/core/src/main/java/greencity/config/SecurityConfig.java b/core/src/main/java/greencity/config/SecurityConfig.java index 59766630e..45ea83cb1 100644 --- a/core/src/main/java/greencity/config/SecurityConfig.java +++ b/core/src/main/java/greencity/config/SecurityConfig.java @@ -104,7 +104,8 @@ protected void configure(HttpSecurity http) throws Exception { "/token", "/socket/**", "/user/findAllByEmailNotification", - "/user/checkByUuid") + "/user/checkByUuid", + "/user/get-user-rating") .permitAll() .antMatchers(HttpMethod.POST, "/ownSecurity/signUp", @@ -199,6 +200,9 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/css/**", "/img/**") .permitAll() + .antMatchers(HttpMethod.PUT, + "/user/user-rating") + .hasAnyRole(ADMIN, MODERATOR, EMPLOYEE, UBS_EMPLOYEE) .anyRequest().hasAnyRole(ADMIN); } diff --git a/core/src/main/java/greencity/controller/UserController.java b/core/src/main/java/greencity/controller/UserController.java index 19a6c6886..7cad645b1 100644 --- a/core/src/main/java/greencity/controller/UserController.java +++ b/core/src/main/java/greencity/controller/UserController.java @@ -15,6 +15,7 @@ import greencity.dto.shoppinglist.CustomShoppingListItemResponseDto; import greencity.dto.ubs.UbsTableCreationDto; import greencity.dto.user.RoleDto; +import greencity.dto.user.UserRatingDto; import greencity.dto.user.UserActivationDto; import greencity.dto.user.UserAllFriendsDto; import greencity.dto.user.UserAndAllFriendsWithOnlineStatusDto; @@ -1148,4 +1149,51 @@ public ResponseEntity deactivateEmployee(@RequestParam String uuid) userService.markUserAsDeactivated(uuid); return ResponseEntity.status(HttpStatus.OK).build(); } + + /** + * Retrieves an employee's rating information by their email. + * + * @param id The id of the employee for whom the rating information is + * requested. + * @return A ResponseEntity containing the UserRatingDto with the employee's + * rating information. + * + * @author Oksana Spodaryk. + */ + @ApiOperation(value = "Get an employee's rating information by their id.") + @ApiResponses(value = { + @ApiResponse(code = 200, message = HttpStatuses.OK), + @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), + @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), + @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), + @ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND) + }) + @GetMapping("/get-user-rating") + public ResponseEntity getUserRating(@RequestParam Long id) { + return ResponseEntity.status(HttpStatus.OK).body(userService.getUserRating(id)); + } + + /** + * Updates an employee's rating information. + * + * @param userRatingDto The UserRatingDto containing the updated rating + * information. + * @return A ResponseEntity with HTTP status indicating the success of the + * update operation. + * + * @author Oksana Spodaryk. + */ + @ApiOperation(value = "Update an employee's rating information.") + @ApiResponses(value = { + @ApiResponse(code = 200, message = HttpStatuses.OK), + @ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST), + @ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED), + @ApiResponse(code = 403, message = HttpStatuses.FORBIDDEN), + @ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND) + }) + @PutMapping("/user-rating") + public ResponseEntity updateUserRating(@Valid @RequestBody UserRatingDto userRatingDto) { + userService.updateUserRating(userRatingDto); + return ResponseEntity.status(HttpStatus.OK).build(); + } } diff --git a/core/src/test/java/greencity/controller/UserControllerTest.java b/core/src/test/java/greencity/controller/UserControllerTest.java index 1f9d50ce2..dc734bede 100644 --- a/core/src/test/java/greencity/controller/UserControllerTest.java +++ b/core/src/test/java/greencity/controller/UserControllerTest.java @@ -25,6 +25,7 @@ import greencity.dto.user.UserStatusDto; import greencity.dto.user.UserUpdateDto; import greencity.dto.user.UserVO; +import greencity.dto.user.UserRatingDto; import greencity.enums.EmailNotification; import greencity.enums.Role; import greencity.repository.UserRepo; @@ -47,6 +48,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -68,7 +71,6 @@ import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -814,4 +816,41 @@ void checkIfUserExistsByUuidTest() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$").value(true)); } + + @Test + void getUserRatingTest() throws Exception { + Principal principal = mock(Principal.class); + when(principal.getName()).thenReturn("testmail@gmail.com"); + + mockMvc.perform(get(userLink + "/get-user-rating?id=1") + .principal(principal) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(principal.getName()))) + .andExpect(status().isOk()); + + verify(userService).getUserRating(anyLong()); + } + + @Test + void editUserRatingTest() throws Exception { + Principal principal = mock(Principal.class); + when(principal.getName()).thenReturn("testmail@gmail.com"); + + UserRatingDto dto = UserRatingDto.builder() + .id(1L) + .rating(10.0) + .build(); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(dto); + + mockMvc.perform(put(userLink + "/user-rating") + .principal(principal) + .content(json) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + verify(userService).updateUserRating(dto); + } + } diff --git a/service-api/src/main/java/greencity/dto/user/UserRatingDto.java b/service-api/src/main/java/greencity/dto/user/UserRatingDto.java new file mode 100644 index 000000000..c9e37f423 --- /dev/null +++ b/service-api/src/main/java/greencity/dto/user/UserRatingDto.java @@ -0,0 +1,16 @@ +package greencity.dto.user; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +@Builder +public class UserRatingDto { + private Long id; + + private Double rating; +} diff --git a/service-api/src/main/java/greencity/service/UserService.java b/service-api/src/main/java/greencity/service/UserService.java index 589625ee9..696c5c7db 100644 --- a/service-api/src/main/java/greencity/service/UserService.java +++ b/service-api/src/main/java/greencity/service/UserService.java @@ -7,6 +7,7 @@ import greencity.dto.filter.FilterUserDto; import greencity.dto.shoppinglist.CustomShoppingListItemResponseDto; import greencity.dto.ubs.UbsTableCreationDto; +import greencity.dto.user.UserRatingDto; import greencity.dto.user.RegistrationStatisticsDtoResponse; import greencity.dto.user.RoleDto; import greencity.dto.user.UserActivationDto; @@ -44,6 +45,23 @@ * @version 1.0 */ public interface UserService { + /** + * Retrieves an employee's rating information based on their email. + * + * @param id The id of the employee for whom the rating information is + * requested. + * @return A UserRatingDto containing the rating information of the employee. + */ + UserRatingDto getUserRating(Long id); + + /** + * Updates an employee's rating information using the provided UserRatingDto. + * + * @param userRatingDto The UserRatingDto containing the updated rating + * information. + */ + void updateUserRating(UserRatingDto userRatingDto); + /** * Find all {@link User}'s with {@link EmailNotification} type. * diff --git a/service/src/main/java/greencity/service/UserServiceImpl.java b/service/src/main/java/greencity/service/UserServiceImpl.java index a77261204..ba7eeb6d5 100644 --- a/service/src/main/java/greencity/service/UserServiceImpl.java +++ b/service/src/main/java/greencity/service/UserServiceImpl.java @@ -12,6 +12,7 @@ import greencity.dto.shoppinglist.CustomShoppingListItemResponseDto; import greencity.dto.ubs.UbsTableCreationDto; import greencity.dto.user.RoleDto; +import greencity.dto.user.UserRatingDto; import greencity.dto.user.UserActivationDto; import greencity.dto.user.UserAllFriendsDto; import greencity.dto.user.UserAndAllFriendsWithOnlineStatusDto; @@ -106,6 +107,30 @@ public UserVO save(UserVO userVO) { return modelMapper.map(userRepo.save(user), UserVO.class); } + /** + * {@inheritDoc} + */ + @Override + public UserRatingDto getUserRating(Long id) { + var user = userRepo.findById(id) + .orElseThrow(() -> new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_EMAIL)); + return UserRatingDto.builder() + .id(user.getId()) + .rating(user.getRating()) + .build(); + } + + /** + * {@inheritDoc} + */ + @Override + public void updateUserRating(UserRatingDto userRatingDto) { + var user = userRepo.findById(userRatingDto.getId()) + .orElseThrow(() -> new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_EMAIL)); + user.setRating(userRatingDto.getRating()); + userRepo.save(user); + } + /** * {@inheritDoc} */ diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index 95cdea023..353386d73 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -457,6 +457,7 @@ private static User createUser() { .email("test@mail.com") .userStatus(UserStatus.CREATED) .role(Role.ROLE_USER) + .rating(100D) .build(); } diff --git a/service/src/test/java/greencity/service/UserServiceImplTest.java b/service/src/test/java/greencity/service/UserServiceImplTest.java index 77ab6445e..8f1664748 100644 --- a/service/src/test/java/greencity/service/UserServiceImplTest.java +++ b/service/src/test/java/greencity/service/UserServiceImplTest.java @@ -1045,4 +1045,24 @@ private static Stream provideUuidOptionalUserResultForCheckIfUserExis Arguments.of("444e66e8-8daa-4cb0-8269-a8d856e7dd15", Optional.of(getUser()), true), Arguments.of("uuid", Optional.empty(), false)); } + + @Test + void getUserRatingTest() { + when(userRepo.findById(anyLong())).thenReturn(Optional.ofNullable(TEST_USER)); + UserRatingDto userRatingDto = userService.getUserRating(1L); + assertEquals(100D, userRatingDto.getRating()); + } + + @Test + void editUserRatingTest() { + when(userRepo.findById(anyLong())).thenReturn(Optional.ofNullable(TEST_USER)); + UserRatingDto userRatingDto = UserRatingDto.builder() + .id(1L) + .rating(200D) + .build(); + + userService.updateUserRating(userRatingDto); + + verify(userRepo).save(TEST_USER); + } }