Skip to content

Commit

Permalink
refactored UserController and UserService for Adminpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Handke committed Jan 8, 2024
1 parent e1c19ac commit b94b013
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ User updateUser(@RequestBody User newUser, @PathVariable Long id) {
return userService.findById(id).map(User -> {
User.setUserName(newUser.getUserName());
User.setRoles(newUser.getRoles());
return userService.save(User);
return userService.update(User);

}).orElseGet(() -> {
newUser.setId(id);
return userService.save(newUser);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/example/kickevent/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public User save(User user) {

}

public User update(User user){
if (userRepository.findByUserName(user.getUserName()).isPresent() && !Objects.equals(userRepository.findByUserName(user.getUserName()).get().getId(), user.getId())) throw new UsernameAlreadyTakenException(user.getUserName());
return userRepository.save(user);
}

@Transactional
public User delete(User user) {
user.setRoles(null);
Expand Down

0 comments on commit b94b013

Please sign in to comment.