Skip to content

Commit

Permalink
Merge pull request #272 from ita-social-projects/user-score
Browse files Browse the repository at this point in the history
[My news] Endpoints to get and edit user points
  • Loading branch information
ospodaryk authored Sep 8, 2023
2 parents a0a1bb3 + be651f1 commit 2f1823d
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 2 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/greencity/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}

Expand Down
48 changes: 48 additions & 0 deletions core/src/main/java/greencity/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1148,4 +1149,51 @@ public ResponseEntity<HttpStatus> 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<UserRatingDto> 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<HttpStatus> updateUserRating(@Valid @RequestBody UserRatingDto userRatingDto) {
userService.updateUserRating(userRatingDto);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
41 changes: 40 additions & 1 deletion core/src/test/java/greencity/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

}
16 changes: 16 additions & 0 deletions service-api/src/main/java/greencity/dto/user/UserRatingDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions service-api/src/main/java/greencity/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions service/src/main/java/greencity/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down
1 change: 1 addition & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ private static User createUser() {
.email("test@mail.com")
.userStatus(UserStatus.CREATED)
.role(Role.ROLE_USER)
.rating(100D)
.build();
}

Expand Down
20 changes: 20 additions & 0 deletions service/src/test/java/greencity/service/UserServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1045,4 +1045,24 @@ private static Stream<Arguments> 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);
}
}

0 comments on commit 2f1823d

Please sign in to comment.