From 6bb085b749a4b7a8cc74884a921fd201805c7a17 Mon Sep 17 00:00:00 2001 From: Ramon Trekovanicz Date: Sat, 20 Jul 2024 09:19:41 +0200 Subject: [PATCH] refactor: conventional changes due to Marks advice --- .../config/ResponseEntityErrorHandler.java | 24 +++++++++- .../backend/controller/UserController.java | 44 +++++-------------- ...erUserDto.java => RegisterRequestDto.java} | 2 +- .../backend/services/UserService.java | 4 +- .../backend/services/UserServiceImpl.java | 6 +-- .../controller/UserRegistrationTest.java | 4 +- .../backend/controller/UserServiceTest.java | 16 +++---- 7 files changed, 50 insertions(+), 50 deletions(-) rename backend/src/main/java/com/greenfoxacademy/backend/dtos/{RegisterUserDto.java => RegisterRequestDto.java} (94%) diff --git a/backend/src/main/java/com/greenfoxacademy/backend/config/ResponseEntityErrorHandler.java b/backend/src/main/java/com/greenfoxacademy/backend/config/ResponseEntityErrorHandler.java index 98eda46f..d359e02b 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/config/ResponseEntityErrorHandler.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/config/ResponseEntityErrorHandler.java @@ -2,8 +2,12 @@ import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import java.util.HashMap; +import java.util.Map; + import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; @@ -25,6 +29,24 @@ public class ResponseEntityErrorHandler { handleUserAlreadyExistsError(UserAlreadyExistsError ex) { HashMap errors = new HashMap<>(); errors.put("error", "Email is already taken!"); - return ResponseEntity.badRequest().body(errors); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors); + } + + /** + * This method handles validation exceptions. + * + * @param ex the exception to be handled + * @return a map with the field name and the error message + */ + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + public Map handleValidationExceptions(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + return errors; } } diff --git a/backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java b/backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java index 65421f58..e25b9b84 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java @@ -2,23 +2,19 @@ import com.greenfoxacademy.backend.dtos.LoginRequestDto; import com.greenfoxacademy.backend.dtos.LoginResponseDto; -import com.greenfoxacademy.backend.dtos.RegisterUserDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import com.greenfoxacademy.backend.services.UserService; + import java.net.URI; -import java.util.HashMap; -import java.util.Map; + import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.validation.FieldError; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -33,15 +29,15 @@ public class UserController { /** * This method registers a new user. * - * @param registerUserDto the user to be registered + * @param registerRequestDto the user to be registered * @return a response entity with the status code and the location of the new user */ @CrossOrigin(origins = "http://localhost:5173") @PostMapping("/register") - public ResponseEntity registerUser(@Validated @RequestBody RegisterUserDto registerUserDto) - throws UserAlreadyExistsError { - URI uri = URI.create("/users/" + userService.register(registerUserDto).id()); - return ResponseEntity.created(uri).build(); + public ResponseEntity registerUser(@Validated @RequestBody RegisterRequestDto registerRequestDto) + throws UserAlreadyExistsError { + URI uri = URI.create("/users/" + userService.register(registerRequestDto).id()); + return ResponseEntity.status(HttpStatus.CREATED).body(uri); } /** @@ -50,37 +46,19 @@ public ResponseEntity registerUser(@Validated @RequestBody RegisterUserDto re * - If the user is not found, return a 401 status code. * - If the user is found, return a 200 status code and the token. * - * @param registerUserDto the user to be logged in + * @param loginRequestDto the user to be logged in * @return a response entity with the status code and the token */ //TODO: add validation for the LoginRequestDto after that re-add the @Validated annotation @CrossOrigin(origins = "http://localhost:5173") @PostMapping("/login") public ResponseEntity loginUser( - @RequestBody LoginRequestDto registerUserDto + @RequestBody LoginRequestDto loginRequestDto ) { try { - return ResponseEntity.ok(userService.login(registerUserDto)); + return ResponseEntity.status(HttpStatus.OK).body(userService.login(loginRequestDto)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } } - - /** - * This method handles validation exceptions. - * - * @param ex the exception to be handled - * @return a map with the field name and the error message - */ - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(MethodArgumentNotValidException.class) - public Map handleValidationExceptions(MethodArgumentNotValidException ex) { - Map errors = new HashMap<>(); - ex.getBindingResult().getAllErrors().forEach((error) -> { - String fieldName = ((FieldError) error).getField(); - String errorMessage = error.getDefaultMessage(); - errors.put(fieldName, errorMessage); - }); - return errors; - } } \ No newline at end of file diff --git a/backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterUserDto.java b/backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterRequestDto.java similarity index 94% rename from backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterUserDto.java rename to backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterRequestDto.java index f50b7e13..a3b93a32 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterUserDto.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/dtos/RegisterRequestDto.java @@ -15,7 +15,7 @@ @NoArgsConstructor @Getter @Setter -public class RegisterUserDto { +public class RegisterRequestDto { @NotBlank private String firstName; @NotBlank diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/UserService.java b/backend/src/main/java/com/greenfoxacademy/backend/services/UserService.java index f403c347..24987dab 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/UserService.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/UserService.java @@ -3,7 +3,7 @@ import com.greenfoxacademy.backend.dtos.LoginRequestDto; import com.greenfoxacademy.backend.dtos.LoginResponseDto; import com.greenfoxacademy.backend.dtos.RegisterResponseDto; -import com.greenfoxacademy.backend.dtos.RegisterUserDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import com.greenfoxacademy.backend.models.User; import org.springframework.stereotype.Service; @@ -13,7 +13,7 @@ */ @Service public interface UserService { - RegisterResponseDto register(RegisterUserDto userDto) throws UserAlreadyExistsError; + RegisterResponseDto register(RegisterRequestDto userDto) throws UserAlreadyExistsError; LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception; } diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/UserServiceImpl.java b/backend/src/main/java/com/greenfoxacademy/backend/services/UserServiceImpl.java index b7d3383e..e58889d2 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/UserServiceImpl.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/UserServiceImpl.java @@ -3,7 +3,7 @@ import com.greenfoxacademy.backend.dtos.LoginRequestDto; import com.greenfoxacademy.backend.dtos.LoginResponseDto; import com.greenfoxacademy.backend.dtos.RegisterResponseDto; -import com.greenfoxacademy.backend.dtos.RegisterUserDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import com.greenfoxacademy.backend.models.User; import com.greenfoxacademy.backend.repositories.UserRepository; @@ -25,7 +25,7 @@ public class UserServiceImpl implements UserService { private final JwtUtil jwtUtil; @Override - public RegisterResponseDto register(RegisterUserDto userDto) throws UserAlreadyExistsError { + public RegisterResponseDto register(RegisterRequestDto userDto) throws UserAlreadyExistsError { User user = mapToEntity(userDto); user.setPassword(passwordEncoder.encode(userDto.getPassword())); try { @@ -39,7 +39,7 @@ private RegisterResponseDto mapToRegisterResponseDto(User user) { return new RegisterResponseDto(user.getId()); } - private User mapToEntity(RegisterUserDto userDto) { + private User mapToEntity(RegisterRequestDto userDto) { return modelMapper.map(userDto, User.class); } diff --git a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java index 41c8a9e6..18eec1df 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java @@ -2,7 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; -import com.greenfoxacademy.backend.dtos.RegisterUserDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import com.greenfoxacademy.backend.repositories.UserRepository; import com.greenfoxacademy.backend.services.UserService; @@ -26,7 +26,7 @@ public class UserRegistrationTest { @Test public void userIsSuccessfulRegisteredInDatabase() throws Exception, UserAlreadyExistsError { - RegisterUserDto newUser = new RegisterUserDto(); + RegisterRequestDto newUser = new RegisterRequestDto(); newUser.setFirstName("John"); newUser.setLastName("Doe"); newUser.setEmail("john.doe@example.com"); diff --git a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java index 787afcce..90772d67 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java @@ -3,7 +3,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import com.greenfoxacademy.backend.dtos.RegisterUserDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; import com.greenfoxacademy.backend.repositories.UserRepository; import com.greenfoxacademy.backend.services.UserService; @@ -29,15 +29,15 @@ public class UserServiceTest { @Test public void registerMethodIsSuccessfullyCalled() throws Exception, UserAlreadyExistsError { - RegisterUserDto registerUserDto = new RegisterUserDto(); - registerUserDto.setFirstName("John"); - registerUserDto.setLastName("Doe"); - registerUserDto.setEmail("john.doe@gmail.com"); - registerUserDto.setPassword("password"); + RegisterRequestDto registerRequestDto = new RegisterRequestDto(); + registerRequestDto.setFirstName("John"); + registerRequestDto.setLastName("Doe"); + registerRequestDto.setEmail("john.doe@gmail.com"); + registerRequestDto.setPassword("password"); - userService.register(registerUserDto); + userService.register(registerRequestDto); - verify(userService, times(1)).register(registerUserDto); + verify(userService, times(1)).register(registerRequestDto); }