Skip to content

Commit

Permalink
refactor(backend): align with Google Java Coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
markkovari committed Jul 9, 2024
1 parent 76bd186 commit 919fd58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.greenfoxacademy.backend.controller;

import com.greenfoxacademy.backend.dtos.RegisterUserDto;
import com.greenfoxacademy.backend.services.UserService;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import com.greenfoxacademy.backend.dtos.RegisterUserDto;
import com.greenfoxacademy.backend.services.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,7 +26,14 @@
@RequiredArgsConstructor
public class UserController {
private final UserService userService;


/**
* This method registers a new user.
*
* @param registerUserDto 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) {
Expand All @@ -38,7 +44,14 @@ public ResponseEntity<?> registerUser(@Validated @RequestBody RegisterUserDto re
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}


/**
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

/**
* RegisterResponseDto represents a registration response.
*
* @param id of the created user
*
*/

public record RegisterResponseDto(
Integer id
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.ArrayList;


Expand All @@ -15,8 +14,11 @@ public class PasswordConstraintValidator implements ConstraintValidator<ValidPas
@Override
public boolean isValid(String password, ConstraintValidatorContext constraintValidatorContext) {
if (password == null) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate("Password should be added").addConstraintViolation();
constraintValidatorContext
.disableDefaultConstraintViolation();
constraintValidatorContext
.buildConstraintViolationWithTemplate("Password should be added")
.addConstraintViolation();
return false;
}
boolean isValid = true;
Expand All @@ -43,7 +45,11 @@ public boolean isValid(String password, ConstraintValidatorContext constraintVal
}
if (!isValid) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate("Invalid password: " + String.join(", ", errorMessages)).addConstraintViolation();
constraintValidatorContext
.buildConstraintViolationWithTemplate(
"Invalid password: " + String.join(", ", errorMessages)
)
.addConstraintViolation();
}
return isValid;
}
Expand Down

0 comments on commit 919fd58

Please sign in to comment.