-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#25 - Refactor: AuthLevel Enum 도입, AuthLevelConvert 로 Enum <-> int 구현
- Loading branch information
Showing
4 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.mutbooks.app.member.entity; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Arrays; | ||
|
||
@Getter | ||
public enum AuthLevel { | ||
USER(3, "USER"), | ||
ADMIN(7, "ADMIN"); | ||
|
||
private final int code; | ||
private final String value; | ||
|
||
AuthLevel(int code, String value) { | ||
this.code = code; | ||
this.value = value; | ||
} | ||
|
||
// Enum 에서 code 값으로 해당 Enum 을 찾는 메서드 | ||
public static AuthLevel ofCode(Integer code) { | ||
return Arrays.stream(AuthLevel.values()) | ||
.filter(v -> v.getCode() == code) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException(String.format("code=[%d]가 존재하지 않습니다.", code))); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ion/mutbooks/src/main/java/com/example/mutbooks/app/member/entity/AuthLevelConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.mutbooks.app.member.entity; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
|
||
/** | ||
* AttributeConverter X, Y | ||
* X : 엔티티의 속성에 대응하는 타입 | ||
* Y : DB 에 대응하는 타입 | ||
*/ | ||
@Converter | ||
public class AuthLevelConverter implements AttributeConverter<AuthLevel, Integer> { | ||
|
||
// Enum -> db 데이터(code:Integer) | ||
@Override | ||
public Integer convertToDatabaseColumn(AuthLevel attribute) { | ||
if(attribute == null) | ||
return null; | ||
|
||
return attribute.getCode(); | ||
} | ||
|
||
// db 데이터(code:Integer) -> Enum | ||
@Override | ||
public AuthLevel convertToEntityAttribute(Integer dbData) { | ||
return AuthLevel.ofCode(dbData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters