Skip to content

Commit

Permalink
Add Personal Info for Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-w1lde committed Nov 10, 2024
1 parent 8ed4cde commit 4630c35
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.furizon.backend.feature.authentication.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.authentication.dto.LoginRequest;
import net.furizon.backend.feature.authentication.dto.LoginResponse;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class AuthenticationController {

@PostMapping("/login")
public LoginResponse loginUser(
@RequestBody final LoginRequest loginRequest,
@Valid @RequestBody final LoginRequest loginRequest,
@RequestHeader(value = HttpHeaders.USER_AGENT, required = false) @Nullable String userAgent,
HttpServletRequest httpServletRequest
) {
Expand Down Expand Up @@ -60,7 +61,7 @@ public LogoutUserResponse logoutUser(

@PostMapping("/register")
public RegisterUserResponse registerUser(
@RequestBody final RegisterUserRequest registerUserRequest
@Valid @RequestBody final RegisterUserRequest registerUserRequest
) {
executor.execute(
RegisterUserUseCase.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package net.furizon.backend.feature.authentication.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

import static net.furizon.backend.feature.authentication.Const.EMAIL_REGEX;

@Data
public class LoginRequest {
@NotNull
@NotEmpty
@Email(regexp = EMAIL_REGEX)
private final String email;

@NotNull
@NotEmpty
@Size(min = 6)
private final String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.furizon.backend.feature.authentication.dto;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PastOrPresent;
import jakarta.validation.constraints.Size;
import lombok.Data;

import java.time.LocalDate;

@Data
public class PersonalUserInformation {
@NotNull
@NotEmpty
@Size(min = 2)
private final String firstName;

@NotNull
@NotEmpty
@Size(min = 2)
private final String lastName;

@NotNull
@PastOrPresent
private final LocalDate birthday;

@NotNull
@NotEmpty
@Size(min = 2)
private final String birthCity;

@NotNull
@NotEmpty
@Size(min = 2)
private final String birthProvince;

@NotNull
@NotEmpty
@Size(min = 2)
private final String birthCountry;

@NotNull
@NotEmpty
@Size(min = 2)
private final String currentAddress;

@NotNull
@NotEmpty
@Size(min = 2)
private final String phoneNumber;

@Nullable
private final String socialSecurityNumber;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package net.furizon.backend.feature.authentication.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static net.furizon.backend.feature.authentication.Const.EMAIL_REGEX;

@Data
public class RegisterUserRequest {
@NotNull
@NotEmpty
@Email(regexp = EMAIL_REGEX)
private final String email;

@NotNull
@NotEmpty
@Size(min = 6)
private final String password;

@Nullable
private final String fursuitName;
@NotNull
@Pattern(regexp = "^[\\p{L}\\p{N}\\p{M}_\\- ]{3,20}$")
private final String fursonaName;

@NotNull
@Valid
private final PersonalUserInformation personalUserInformation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.furizon.backend.feature.authentication.action.createAuthentication.CreateAuthenticationAction;
import net.furizon.backend.feature.authentication.dto.RegisterUserRequest;
import net.furizon.backend.feature.authentication.validation.RegisterUserValidation;
import net.furizon.backend.feature.membership.action.addMembershipInfo.AddMembershipInfoAction;
import net.furizon.backend.feature.user.User;
import net.furizon.backend.feature.user.action.createUser.CreateUserAction;
import net.furizon.backend.infrastructure.usecase.UseCase;
Expand All @@ -20,16 +21,22 @@ public class RegisterUserUseCase implements UseCase<RegisterUserRequest, User> {

private final CreateAuthenticationAction createAuthenticationAction;

private final AddMembershipInfoAction addMembershipInfoAction;

@Transactional
@Override
public @NotNull User executor(@NotNull RegisterUserRequest input) {
validation.validate(input);
final var user = createUserAction.invoke(input.getFursuitName());
final var user = createUserAction.invoke(input.getFursonaName());
createAuthenticationAction.invoke(
user.getId(),
input.getEmail(),
input.getPassword()
);
addMembershipInfoAction.invoke(
user.getId(),
input.getPersonalUserInformation()
);

return user;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class CreateLoginSessionValidation {
private final PasswordEncoder passwordEncoder;

public long validateAndGetUserId(@NotNull LoginUserUseCase.Input input) {
AuthenticationException.validateEmailOrThrow(input.email());

final var authentication = authenticationFinder.findByEmail(input.email());
if (authentication == null) {
throw new ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class RegisterUserValidation {
private final AuthenticationFinder authenticationFinder;

public void validate(@NotNull RegisterUserRequest input) {
AuthenticationException.validateEmailOrThrow(input.getEmail());

final var authentication = authenticationFinder.findByEmail(input.getEmail());
if (authentication != null) {
throw new ApiException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.furizon.backend.feature.membership.action.addMembershipInfo;

import net.furizon.backend.feature.authentication.dto.PersonalUserInformation;
import org.jetbrains.annotations.NotNull;

public interface AddMembershipInfoAction {
void invoke(long userId, @NotNull final PersonalUserInformation personalUserInformation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.furizon.backend.feature.membership.action.addMembershipInfo;

import lombok.RequiredArgsConstructor;
import net.furizon.backend.feature.authentication.dto.PersonalUserInformation;
import net.furizon.jooq.infrastructure.command.SqlCommand;
import org.jetbrains.annotations.NotNull;
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

import static net.furizon.jooq.generated.Tables.MEMBERSHIP_INFO;

@Component
@RequiredArgsConstructor
public class JooqAddMembershipInfoAction implements AddMembershipInfoAction {
private final SqlCommand sqlCommand;

@Override
public void invoke(long userId, @NotNull PersonalUserInformation personalUserInformation) {
sqlCommand.execute(
PostgresDSL
.insertInto(
MEMBERSHIP_INFO,
MEMBERSHIP_INFO.INFO_FIRST_NAME,
MEMBERSHIP_INFO.INFO_LAST_NAME,
MEMBERSHIP_INFO.INFO_BIRTH_CITY,
MEMBERSHIP_INFO.INFO_BIRTH_REGION,
MEMBERSHIP_INFO.INFO_BIRTH_COUNTRY,
MEMBERSHIP_INFO.INFO_ADDRESS,
MEMBERSHIP_INFO.INFO_PHONE,
MEMBERSHIP_INFO.USER_ID
)
.values(
personalUserInformation.getFirstName(),
personalUserInformation.getLastName(),
personalUserInformation.getBirthCity(),
personalUserInformation.getBirthProvince(),
personalUserInformation.getBirthCountry(),
personalUserInformation.getCurrentAddress(),
personalUserInformation.getPhoneNumber(),
userId
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import net.furizon.backend.feature.user.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface CreateUserAction {
@NotNull
User invoke(@Nullable String fursonaName);
User invoke(@NotNull String fursonaName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.furizon.backend.feature.user.mapper.JooqUserMapper;
import net.furizon.jooq.infrastructure.command.SqlCommand;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jooq.util.postgres.PostgresDSL;
import org.springframework.stereotype.Component;

Expand All @@ -19,7 +18,7 @@ public class JooqCreateUserAction implements CreateUserAction {
private final SqlCommand sqlCommand;

@Override
public @NotNull User invoke(@Nullable String fursonaName) {
public @NotNull User invoke(@NotNull String fursonaName) {
return sqlCommand.executeResult(
PostgresDSL.insertInto(
USERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum ApiCommonErrorCode {
UNKNOWN,
UNAUTHENTICATED,
SESSION_NOT_FOUND,
INVALID_INPUT,
;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -35,28 +37,11 @@ ResponseEntity<HttpErrorResponse> handleMethodArgumentNotValidException(
@NotNull MethodArgumentNotValidException ex,
@NotNull HttpServletRequest request
) {
//.forEach(
//(error) -> {
//if (error instanceof FieldError fieldError) {
//String fieldName = fieldError.getField();
//String errorMessage = fieldError.getDefaultMessage();
//errors.put(fieldName, errorMessage);
//} else {
//generalErrors.add(error.getDefaultMessage());
//}
//}
//);
final var errors = ex
.getBindingResult()
.getAllErrors()
.stream()
.map(
(err) ->
new ApiError(
err.getDefaultMessage(),
""
)
)
.map(this::matchObjectError)
.toList();
return ResponseEntity
.status(HttpStatus.UNPROCESSABLE_ENTITY)
Expand All @@ -67,4 +52,21 @@ ResponseEntity<HttpErrorResponse> handleMethodArgumentNotValidException(
.build()
);
}

@NotNull
private ApiError matchObjectError(@NotNull ObjectError error) {
if (error instanceof FieldError fieldError) {
return new ApiError(
"Field '%s' %s; (value '%s' is invalid)".formatted(
fieldError.getField(),
error.getDefaultMessage(),
fieldError.getRejectedValue()
),
ApiCommonErrorCode.INVALID_INPUT.toString()
);
}

final var message = error.getDefaultMessage();
return new ApiError(message != null ? message : "Unknown error", ApiCommonErrorCode.UNKNOWN.toString());
}
}

0 comments on commit 4630c35

Please sign in to comment.