Skip to content

Commit

Permalink
refactor: conventional changes due to Marks advice
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesz6 committed Jul 20, 2024
1 parent de49ce2 commit 6bb085b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +29,24 @@ public class ResponseEntityErrorHandler {
handleUserAlreadyExistsError(UserAlreadyExistsError ex) {
HashMap<String, String> 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<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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);
}

/**
Expand All @@ -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<LoginResponseDto> 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<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@NoArgsConstructor
@Getter
@Setter
public class RegisterUserDto {
public class RegisterRequestDto {
@NotBlank
private String firstName;
@NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}

Expand Down

0 comments on commit 6bb085b

Please sign in to comment.