Repository navigation
[FEAT] social_auth, usage_limit 테이블 엔티티 및 에러 선언 (#8) #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6e40de7
e392993
73dc4a0
f83d813
b9dcb21
6187ac2
6e0d412
de6a46c
f797669
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.mr.domain.auth.entity; | ||
|
|
||
| import com.mr.domain.auth.entity.enums.AuthErrorStatus; | ||
| import com.mr.domain.auth.entity.enums.SocialType; | ||
| import com.mr.global.apipayload.code.CommonStatus; | ||
| import com.mr.global.apipayload.exception.GeneralException; | ||
| import com.mr.global.entity.BaseCreatedEntity; | ||
| import jakarta.persistence.*; | ||
| import java.time.LocalDateTime; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Entity | ||
| // TODO: 추후 User 도메인 완성 시 단방향/양방향 인덱스 추가 | ||
| @Table( | ||
| name = "social_auth", | ||
| indexes = { | ||
| @Index(name = "idx_social_auth_token_hash", columnList = "refresh_token_hash") | ||
| }) | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class SocialAuth extends BaseCreatedEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "social_auth_id") | ||
| private Long id; | ||
|
|
||
| // TODO: User 엔티티 연관관계 연결 예정 | ||
| @Column(name = "user_id", nullable = false) | ||
| private Long userId; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(name = "social_type", nullable = false, length = 20) | ||
| private SocialType socialType; | ||
|
|
||
| @Column(name = "social_id", nullable = false, length = 100) | ||
| private String socialId; | ||
|
|
||
| @Column(name = "refresh_token", length = 1000) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 길이 제한이 1000으로 되어 있는데, 혹시 TEXT 타입을 쓰는 방법도 가능한가요? 제가 토큰의 길이가 어느 정도 인지 잘 몰라서 여쭈어봅니다!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PostgreSQL에서는 사용 중인 OAuth 제공자의 안내는 다음과 같습니다.
현재 카카오와 Google 토큰을 저장하는 용도라면 VARCHAR(1000)으로도 충분해 보이지만, 특별히 길이를 제한해야 하는 요구사항이 없다면 TEXT로 두어 향후 토큰 형식 변경에 유연하게 대응하는 것도 괜찮겠네용 [참고한 공식 답변/문서] |
||
| private String refreshToken; | ||
|
|
||
| @Column(name = "refresh_token_hash", length = 64, unique = true) | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
| private String refreshTokenHash; | ||
|
|
||
| @Column(name = "expired_at") | ||
| private LocalDateTime expiredAt; | ||
|
|
||
| @Column(name = "device_info", length = 255) | ||
| private String deviceInfo; | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private SocialAuth(Long userId, SocialType socialType, String socialId, String refreshToken, | ||
| LocalDateTime expiredAt, String deviceInfo) { | ||
|
|
||
| validateRequiredField(userId); | ||
| validateRequiredField(socialType); | ||
| validateRequiredField(socialId); | ||
|
|
||
| this.userId = userId; | ||
| this.socialType = socialType; | ||
| this.socialId = socialId; | ||
| this.refreshToken = refreshToken; | ||
| this.expiredAt = expiredAt; | ||
| this.deviceInfo = deviceInfo; | ||
| } | ||
|
|
||
| public static SocialAuth create(Long userId, SocialType socialType, String socialId, | ||
| String refreshToken, LocalDateTime expiredAt, String deviceInfo) { | ||
| return SocialAuth.builder() | ||
| .userId(userId) | ||
| .socialType(socialType) | ||
| .socialId(socialId) | ||
| .refreshToken(refreshToken) | ||
| .expiredAt(expiredAt) | ||
| .deviceInfo(deviceInfo) | ||
| .build(); | ||
| } | ||
| private static void validateRequiredField(Object value) { | ||
| if (value == null || (value instanceof String && ((String) value).trim().isEmpty())) { | ||
| throw new GeneralException(AuthErrorStatus.INVALID_AUTH_REQUEST); | ||
| } | ||
| } | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
|
|
||
| public void updateRefreshToken(String encryptedToken, String tokenHash, LocalDateTime newExpiredAt, String deviceInfo) { | ||
| validateRequiredField(encryptedToken); | ||
| validateRequiredField(tokenHash); | ||
|
|
||
| if (newExpiredAt == null || newExpiredAt.isBefore(LocalDateTime.now())) { | ||
| throw new GeneralException(AuthErrorStatus.INVALID_TOKEN_EXPIRY); | ||
| } | ||
| this.refreshToken = encryptedToken; | ||
| this.refreshTokenHash = tokenHash; | ||
| this.expiredAt = newExpiredAt; | ||
| this.deviceInfo = deviceInfo; | ||
| } | ||
|
|
||
| public void expireToken() { | ||
| this.refreshToken = null; | ||
| this.expiredAt = null; | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.mr.domain.auth.entity.enums; | ||
|
|
||
| public enum SocialType { | ||
| KAKAO, | ||
| } |
|
kimyw1018 marked this conversation as resolved.
Resolved
kimyw1018 marked this conversation as resolved.
Resolved
kimyw1018 marked this conversation as resolved.
Resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package com.mr.domain.user.entity; | ||
|
|
||
| import com.mr.domain.user.entity.enums.UserUsageErrorStatus; | ||
| import com.mr.global.apipayload.exception.GeneralException; | ||
| import com.mr.global.entity.BaseCreatedEntity; | ||
| import jakarta.persistence.*; | ||
|
ownue marked this conversation as resolved.
Resolved
|
||
| import java.time.LocalDate; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Entity | ||
| @Table( | ||
| name = "usage_limit", | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
| indexes = { | ||
| @Index(name = "idx_usage_limit_user_date", columnList = "user_id, limit_date", unique = true) | ||
| }) | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class UsageLimit extends BaseCreatedEntity { | ||
|
|
||
| private static final int DEFAULT_MAX_FREE_COUNT = 3; // Free 3회 제한 | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "usage_limit_id") | ||
| private Long id; | ||
|
|
||
| // TODO: User 엔티티 연관관계 연결 예정 | ||
| @Column(name = "user_id", nullable = false) | ||
| private Long userId; | ||
|
|
||
| @Column(name = "limit_date", nullable = false) | ||
| private LocalDate limitDate; | ||
|
|
||
| @Column(name = "remaining_count", nullable = false) | ||
| private Integer remainingCount; | ||
|
|
||
| @Column(name = "max_count", nullable = false) | ||
| private Integer maxCount; | ||
|
|
||
| @Builder(access = AccessLevel.PRIVATE) | ||
| private UsageLimit(Long userId, LocalDate limitDate, Integer remainingCount, Integer maxCount) { | ||
| validateRequired(userId, "userId"); | ||
| validateRequired(limitDate, "limitDate"); | ||
| validatePositiveOrZero(remainingCount, "remainingCount"); | ||
| validatePositiveOrZero(maxCount, "maxCount"); | ||
|
|
||
| this.userId = userId; | ||
| this.limitDate = limitDate; | ||
| this.remainingCount = remainingCount; | ||
| this.maxCount = maxCount; | ||
| } | ||
|
|
||
| public static UsageLimit createDefault(Long userId, LocalDate limitDate) { | ||
| return UsageLimit.builder() | ||
| .userId(userId) | ||
| .limitDate(limitDate) | ||
| .maxCount(DEFAULT_MAX_FREE_COUNT) | ||
| .remainingCount(DEFAULT_MAX_FREE_COUNT) | ||
| .build(); | ||
| } | ||
|
|
||
| public static void validateRequired(Object value, String fieldName) { | ||
| if (value == null) { | ||
| throw new GeneralException(UserUsageErrorStatus.INVALID_USAGE_COUNT); | ||
| } | ||
| } | ||
|
|
||
| private static void validatePositiveOrZero(Integer value, String fieldName) { | ||
| if (value == null || value < 0) { | ||
| throw new GeneralException(UserUsageErrorStatus.INVALID_USAGE_COUNT); | ||
| } | ||
| } | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
kimyw1018 marked this conversation as resolved.
Resolved
|
||
|
|
||
| // 분석 요청 시 카운트 로직 | ||
| public void consume() { | ||
|
kimyw1018 marked this conversation as resolved.
Resolved
|
||
| if (this.remainingCount <= 0) { | ||
| throw new GeneralException(UserUsageErrorStatus.USAGE_LIMIT_EXCEEDED); | ||
| } | ||
| this.remainingCount--; | ||
| } | ||
|
|
||
| // 관리자 기능으로 잔여 횟수 수동 조절할 때 사용 | ||
| public void updateRemainingCount(Integer newCount) { | ||
| validatePositiveOrZero(newCount, "newCount"); | ||
| if (newCount > this.maxCount) { | ||
| throw new GeneralException(UserUsageErrorStatus.INVALID_USAGE_COUNT); | ||
| } | ||
| this.remainingCount = newCount; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.mr.domain.user.entity.enums; | ||
|
|
||
| import com.mr.global.apipayload.code.BaseCode; | ||
| import org.springframework.http.HttpStatus; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum UserUsageErrorStatus implements BaseCode { | ||
|
|
||
| // 잔여 횟수 소진 에러 | ||
| USAGE_LIMIT_EXCEEDED(HttpStatus.FORBIDDEN, "USAGE_403_01", "일일 잔여 분석 횟수를 모두 소진하였습니다."), | ||
|
|
||
| // 유효하지 않은 카운트 수정 요청 | ||
| INVALID_USAGE_COUNT(HttpStatus.BAD_REQUEST, "USAGE_400_01", "잔여 횟수는 최대 제한 횟수를 초과할 수 없습니다."), | ||
|
|
||
| // 해당 날짜의 이용 제한 데이터가 존재하지 않을 때 | ||
| USAGE_RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "USAGE_404_01", "해당 날짜의 이용 제한 기록을 찾을 수 없습니다."); | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.