Skip to content

Commit

Permalink
Tht server 189 회원가입 시 추가정보 기입 (#193)
Browse files Browse the repository at this point in the history
* test : UserFrequency Enum converter test

* add : UserFrequencyConverter Exception class

* add & test: UserReligion enum class

* add : user frequency & user religion converter class

* remove : unused import

* move : test package out

* fix : UserSignUpRequest 멤버변수 추가

* remove : unused import

* docs : 추가 파라미터 문서화

* remove : unused import

* feat : 유저 회원가입 추가정보 파라미터 추가

* test : test 수정
  • Loading branch information
thalals authored Mar 11, 2024
1 parent a0c3ef9 commit db44450
Show file tree
Hide file tree
Showing 86 changed files with 541 additions and 276 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/tht/api/app/entity/enums/EnumModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tht.api.app.entity.enums;

public interface EnumModel {

String getKey();

String getValue();

}
39 changes: 39 additions & 0 deletions src/main/java/com/tht/api/app/entity/enums/UserFrequency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tht.api.app.entity.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.tht.api.exception.custom.EnumStateNotFoundException;
import lombok.AllArgsConstructor;

import java.util.Arrays;

@AllArgsConstructor
public enum UserFrequency implements EnumModel{

NONE("NONE"),
SOMETIMES("SOMETIMES"),
FREQUENTLY("FREQUENTLY");

private final String value;

@JsonCreator
public static UserFrequency toConverter(final String name) {
return Arrays.stream(UserFrequency.values())
.filter(userFrequency -> userFrequency.getValue().equals(name))
.findFirst()
.orElseThrow(
() -> EnumStateNotFoundException.ofUserFrequency(name)
);
}


@Override
public String getKey() {
return this.name();
}

@Override
public String getValue() {
return value;
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/tht/api/app/entity/enums/UserReligion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.tht.api.app.entity.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.tht.api.exception.custom.EnumStateNotFoundException;
import lombok.AllArgsConstructor;

import java.util.Arrays;

@AllArgsConstructor
public enum UserReligion implements EnumModel{

NONE("NONE"),
CHRISTIAN("CHRISTIAN"),
CATHOLICISM("CATHOLICISM"),
BUDDHISM("BUDDHISM"),
WON_BUDDHISM("WON_BUDDHISM"),
OTHER("OTHER");

private final String value;

@JsonCreator
public static UserReligion toConverter(final String name) {
return Arrays.stream(UserReligion.values())
.filter(userReligion -> userReligion.getValue().equals(name))
.findFirst()
.orElseThrow(
() -> EnumStateNotFoundException.ofUserReligion(name)
);
}

@Override
public String getKey() {
return this.name();
}

@Override
public String getValue() {
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tht.api.app.entity.enums.converter;

import com.tht.api.app.entity.enums.UserFrequency;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.Objects;

@Converter
public class UserFrequencyConverter implements AttributeConverter<UserFrequency, String> {
@Override
public String convertToDatabaseColumn(UserFrequency userFrequency) {
if (Objects.isNull(userFrequency)) {
throw new NullPointerException("Enum Converting String - UserRole is null");
}

return userFrequency.name(); }

@Override
public UserFrequency convertToEntityAttribute(String dbData) {
return UserFrequency.toConverter(dbData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tht.api.app.entity.enums.converter;

import com.tht.api.app.entity.enums.UserReligion;
import jakarta.persistence.AttributeConverter;

import java.util.Objects;

public class UserReligionConverter implements AttributeConverter<UserReligion, String> {


@Override
public String convertToDatabaseColumn(UserReligion userReligion) {

if (Objects.isNull(userReligion)) {
throw new NullPointerException("Enum Converting String - UserRole is null");
}

return userReligion.name();
}

@Override
public UserReligion convertToEntityAttribute(String dbData) {

return UserReligion.toConverter(dbData);
}
}
101 changes: 58 additions & 43 deletions src/main/java/com/tht/api/app/entity/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@

import com.tht.api.app.config.utils.LogWriteUtils;
import com.tht.api.app.entity.Auditable;
import com.tht.api.app.entity.enums.EntityState;
import com.tht.api.app.entity.enums.Gender;
import com.tht.api.app.entity.enums.UserRole;
import com.tht.api.app.entity.enums.*;
import com.tht.api.app.entity.enums.converter.GenderConverter;
import com.tht.api.app.entity.enums.converter.UserFrequencyConverter;
import com.tht.api.app.entity.enums.converter.UserReligionConverter;
import com.tht.api.app.entity.enums.converter.UserRoleConverter;
import com.tht.api.exception.custom.UserCustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;

@Entity
@Table(name = "user")
Expand All @@ -37,49 +27,65 @@ public class User extends Auditable {

private static final Pattern PHONE_NUMBER_FORMAT = Pattern.compile("^\\d{9,11}$");
private static final Pattern EMAIL_FORMAT = Pattern.compile(
"^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$");
"^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$");

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idx")
@Column
private Long idx;

@Column(name = "user_uuid")
@Column
private String userUuid;

@Column(name = "username")
@Column
private String username;

@Column(name = "birth_day")
@Column
private LocalDate birthDay;

@Column(name = "phone_number")
@Column
private String phoneNumber;

@Column(name = "email")
@Column
private String email;

@Column(name = "introduction")
@Column
@Lob
private String introduction;

@Column(name = "gender")
@Column
@Convert(converter = GenderConverter.class)
private Gender gender;

@Column(name = "prefer_gender")
@Column
@Convert(converter = GenderConverter.class)
private Gender preferGender;

@Column(name = "user_role")
@Column
@Convert(converter = UserRoleConverter.class)
private UserRole userRole;

@Column
private int tall;

@Column
@Convert(converter = UserFrequencyConverter.class)
private UserFrequency smoking;

@Column
@Convert(converter = UserFrequencyConverter.class)
private UserFrequency drinking;

@Column
@Convert(converter = UserReligionConverter.class)
private UserReligion religion;

@Builder(access = AccessLevel.PRIVATE)
public User(final Long idx, final String userUuid, final String username,
final LocalDate birthDay, final String phoneNumber,
final String email, final String introduction, final Gender gender,
final Gender preferGender, final UserRole userRole) {
final LocalDate birthDay, final String phoneNumber,
final String email, final String introduction, final Gender gender,
final Gender preferGender, final UserRole userRole, final int tall, final UserFrequency smoking,
final UserFrequency drinking, final UserReligion religion) {

this.idx = idx;
this.userUuid = userUuid;
Expand All @@ -91,26 +97,35 @@ public User(final Long idx, final String userUuid, final String username,
this.gender = gender;
this.preferGender = preferGender;
this.userRole = userRole;
this.tall = tall;
this.drinking = drinking;
this.smoking = smoking;
this.religion = religion;
}

public static User createNewUser(final String username, final LocalDate birthDay,
final String phoneNumber, final String email, final String introduction,
final Gender gender, final Gender preferGender) {
final String phoneNumber, final String email, final String introduction,
final Gender gender, final Gender preferGender, final int tall,
final UserFrequency drinking, final UserFrequency smoking, final UserReligion religion) {

validPhoneNumberFormat(phoneNumber);
validEmailFormat(email);

final User user = User.builder()
.username(username)
.userUuid(generateUuid())
.birthDay(birthDay)
.phoneNumber(phoneNumber)
.email(email)
.introduction(introduction)
.gender(gender)
.preferGender(preferGender)
.userRole(UserRole.NORMAL)
.build();
.username(username)
.userUuid(generateUuid())
.birthDay(birthDay)
.phoneNumber(phoneNumber)
.email(email)
.introduction(introduction)
.gender(gender)
.preferGender(preferGender)
.userRole(UserRole.NORMAL)
.tall(tall)
.drinking(drinking)
.smoking(smoking)
.religion(religion)
.build();

LogWriteUtils.createInfo(user);

Expand Down
Loading

0 comments on commit db44450

Please sign in to comment.