User service changes:
// What I do in a Service class is that I put this annotation @Transactional(readOnly = true) so that every method can read data // By doing this, it reduce the number of annotations, but do as you think is best / readable for you // When I need to update data in a method, I add specifically @Transaction annotation to this method @Transactional(readOnly = true)
UserMapper class
@Transactional(readOnly = true) on readonly methods
UserService change:
// I think this can be done with one method, something like this "userRepository.existsByEmailOrUserName(request.getEmail(), request.getUserName())" if (userRepository.existsByEmail(request.getEmail()) || userRepository.existsByUsername(request.getUsername())) { throw new UserAlreadyExistsException(ErrorMessages.USER_ALREADY_EXISTS);
updateUserById in UserService: Unhappy path first in the if-condition
UserController changes:
`// in this case, I would have done it this way because, most of the time, in front end, you won't care about if it's a 200 or a 201
// @PostMapping
// public UserResponse createUser(@Valid @RequestBody CreateOrUpdateUserRequest request) {
// return userService.saveUser(request);
// }
// if you want your URI to look like this '/api/v1/public/users/1', you only need "/{id}"
// @GetMapping("{id}")
@GetMapping("/id={id}")
@GetMapping("/id={id}")`
Changing ResponseEntity<?> to ResponseDto or List in all methods (This is a violent change)
// What I do in a Service class is that I put this annotation @Transactional(readOnly = true) so that every method can read data // By doing this, it reduce the number of annotations, but do as you think is best / readable for you // When I need to update data in a method, I add specifically @Transaction annotation to this method @Transactional(readOnly = true)
// I think this can be done with one method, something like this "userRepository.existsByEmailOrUserName(request.getEmail(), request.getUserName())" if (userRepository.existsByEmail(request.getEmail()) || userRepository.existsByUsername(request.getUsername())) { throw new UserAlreadyExistsException(ErrorMessages.USER_ALREADY_EXISTS);
UserController changes: `// in this case, I would have done it this way because, most of the time, in front end, you won't care about if it's a 200 or a 201 // @PostMapping // public UserResponse createUser(@Valid @RequestBody CreateOrUpdateUserRequest request) { // return userService.saveUser(request); // }
// if you want your URI to look like this '/api/v1/public/users/1', you only need "/{id}" // @GetMapping("{id}") @GetMapping("/id={id}") @GetMapping("/id={id}")`