27.3.1. Modified endpoint update user - use userId instead of current username - backend (#27)

This commit is contained in:
Art
2021-09-28 14:37:01 +03:00
parent 76cd81a731
commit 1046f352ab
5 changed files with 34 additions and 21 deletions

View File

@ -65,10 +65,10 @@ public class UserResource {
return userService.addNewUser(userDto);
}
@PutMapping("{currentUsername}")
public User updateUser(@PathVariable String currentUsername, @Valid UserDto userDto) {
@PutMapping("{userId}")
public User updateUser(@PathVariable UUID userId, @Valid UserDto userDto) {
log.debug("User DTO: {}", userDto);
return userService.updateUser(currentUsername, userDto);
return userService.updateUser(userId, userDto);
}
@GetMapping("{username}")

View File

@ -20,9 +20,11 @@ public interface UserService extends UserDetailsService {
User findByEmail(String email);
User findByUserId(UUID userId);
User addNewUser(UserDto userDto);
User updateUser(String username, UserDto userDto);
User updateUser(UUID userId, UserDto userDto);
void deleteUser(UUID userId);

View File

@ -145,6 +145,13 @@ public class UserServiceImpl implements UserService {
.orElseThrow(() -> new EmailNotFoundException(String.format(EMAIL_NOT_FOUND_MSG, email)));
}
@Override
public User findByUserId(UUID userId) {
return userRepository
.findByUserId(userId)
.orElseThrow(() -> new UserNotFoundException(USER_NOT_FOUND_MSG));
}
@Override
public User addNewUser(UserDto userDto) {
@ -210,12 +217,12 @@ public class UserServiceImpl implements UserService {
}
@Override
public User updateUser(String username, UserDto userDto) {
public User updateUser(UUID userId, UserDto userDto) {
String newUsername = userDto.getUsername();
String email = userDto.getEmail();
User user = validateUpdateUsernameAndEmail(username, newUsername, email);
User user = validateUpdateUsernameAndEmail(userId, newUsername, email);
user.setFirstName(userDto.getFirstName());
user.setLastName(userDto.getLastName());
@ -303,13 +310,13 @@ public class UserServiceImpl implements UserService {
throwEmailExistsException(email);
}
private User validateUpdateUsernameAndEmail(String currentUsername, String username, String email) {
private User validateUpdateUsernameAndEmail(UUID userId, String username, String email) {
Objects.requireNonNull(currentUsername);
Objects.requireNonNull(userId);
User currentUser = findByUsername(currentUsername);
User currentUser = findByUserId(userId);
if (!Objects.equals(currentUsername, username) && userRepository.existsByUsername(username))
if (!Objects.equals(currentUser.getUsername(), username) && userRepository.existsByUsername(username))
throwUsernameExistsException(username);
if (!Objects.equals(currentUser.getEmail(), email) && userRepository.existsByEmail(email))