Skip to content

Commit

Permalink
refactor: Age, Gender enum 한글 매핑
Browse files Browse the repository at this point in the history
기존에 Integer로 저장하던 age를 Age enum으로 저장할 수 있게 함.
Age, Gender enum의 value를 name(한글)과 매핑하였음.
직렬화, 역직렬화 시에 value->name, name->value로 자동으로 변환될 수 있게
함.
  • Loading branch information
chaeyoungeee committed Jul 20, 2024
1 parent d2f0876 commit edbdb2a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 14 deletions.
32 changes: 32 additions & 0 deletions src/main/java/likelion/MZConnent/domain/member/Age.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package likelion.MZConnent.domain.member;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public enum Age {
FOURTH_GRADE("4학년"),
FIFTH_GRADE("5학년"),
SIXTH_GRADE("6학년 이상");

private final String name;

@JsonValue
public String getName() {
return name;
}

@JsonCreator
public static Age fromName(String name) {
for (Age age : Age.values()) {
if (age.getName().equals(name)) {
return age;
}
}
log.info("존재하지 않는 enum type(Age): {}", name);
throw new IllegalArgumentException(name +"은 존재하지 않는 enum type입니다.(Age)");
}
}
29 changes: 28 additions & 1 deletion src/main/java/likelion/MZConnent/domain/member/Gender.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package likelion.MZConnent.domain.member;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public enum Gender {
MALE, FEMALE
MALE("남성"),
FEMALE("여성");

private final String name;

@JsonValue
public String getName() {
return name;
}

@JsonCreator
public static Gender fromName(String name) {
for (Gender gender : Gender.values()) {
if (gender.getName().equals(name)) {
return gender;
}
}
log.info("존재하지 않는 enum type(Gender): {}", name);
throw new IllegalArgumentException(name +"은 존재하지 않는 enum type입니다.(Gender)");
}

}
5 changes: 3 additions & 2 deletions src/main/java/likelion/MZConnent/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public class Member {
@Enumerated(EnumType.STRING)
private Gender gender;

private Integer age;
@Enumerated(EnumType.STRING)
private Age age;

@Builder
public Member(Long id, String email, String password, String realname, String username, Role role, Gender gender, Integer age) {
public Member(Long id, String email, String password, String realname, String username, Role role, Gender gender, Age age) {
this.id = id;
this.email = email;
this.password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import likelion.MZConnent.domain.member.Age;
import likelion.MZConnent.domain.member.Gender;
import likelion.MZConnent.domain.member.Role;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -37,5 +37,6 @@ public class CreateMemberRequest {
private Gender gender;

@NotNull
private Integer age;
@Enumerated(EnumType.STRING)
private Age age;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package likelion.MZConnent.dto.member;

import likelion.MZConnent.domain.member.Age;
import likelion.MZConnent.domain.member.Gender;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.member.Role;
Expand All @@ -14,10 +15,10 @@ public class MemberInfoDto {
private String username;
private Role role;
private Gender gender;
private Integer age;
private Age age;

@Builder
public MemberInfoDto(String email, String realname, String username, Role role, Gender gender, Integer age) {
public MemberInfoDto(String email, String realname, String username, Role role, Gender gender, Age age) {
this.email = email;
this.realname = realname;
this.username = username;
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package likelion.MZConnent.exception;


import com.fasterxml.jackson.core.JsonParseException;
import likelion.MZConnent.dto.ApiResponseJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -17,17 +19,29 @@
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ApiResponseJson> handleRuntimeException(RuntimeException e) {
return new ResponseEntity<>( new ApiResponseJson(HttpStatus.INTERNAL_SERVER_ERROR, "서버에 오류가 발생했습니다."),
HttpStatus.INTERNAL_SERVER_ERROR);
}


@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class})
public ResponseEntity<ApiResponseJson> handleBadRequestException(Exception e) {
return new ResponseEntity<>( new ApiResponseJson(HttpStatus.BAD_REQUEST, e.getMessage()), HttpStatus.BAD_REQUEST);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ApiResponseJson> handleHttpMessageNotReadableException(Exception e) {

String message = e.getMessage();
int startIndex = message.indexOf("problem: ") + "problem: ".length();
String errorMessage = message.substring(startIndex);

return new ResponseEntity<>( new ApiResponseJson(HttpStatus.BAD_REQUEST, errorMessage), HttpStatus.BAD_REQUEST);
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ApiResponseJson> handleRuntimeException(RuntimeException e) {
return new ResponseEntity<>( new ApiResponseJson(HttpStatus.INTERNAL_SERVER_ERROR, "서버에 오류가 발생했습니다."),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package likelion.MZConnent.jwt.token;

import likelion.MZConnent.domain.member.Age;
import likelion.MZConnent.domain.member.Gender;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.member.Role;
import likelion.MZConnent.jwt.blacklist.AccessTokenBlackList;
Expand Down Expand Up @@ -44,7 +46,8 @@ private Member getMember() {
.realname("테스트")
.username("test")
.role(Role.USER)
.age(40)
.age(Age.FOURTH_GRADE)
.gender(Gender.FEMALE)
.build();
}
}

0 comments on commit edbdb2a

Please sign in to comment.