Skip to content

Commit

Permalink
refactor - #111 UserError, UserException, UserSuccess 분
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh committed Mar 16, 2024
1 parent 6c61130 commit a127e30
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public enum AuthError implements ErrorBase {

// 403 FORBIDDEN
NOT_SIGNUP_USER(HttpStatus.FORBIDDEN, "회원가입된 유저가 아닙니다. 회원가입을 진행해주세요."),

// 404 NOT FOUND
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다.");
;

private final HttpStatus status;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public enum AuthSuccess implements SuccessBase {
// 200 OK
LOGIN_SUCCESS(HttpStatus.OK, "로그인에 성공했습니다."),
REISSUE_SUCCESS(HttpStatus.OK, "토큰 재발급에 성공했습니다."),
LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공했습니다."),
GET_USER_INFO_SUCCESS(HttpStatus.OK, "유저의 정보를 불러오는데에 성공했습니다."),
GET_USER_POINT_SUCCESS(HttpStatus.OK, "유저의 포인트 정보를 불러오는데에 성공했습니다."),
WITHDRAW_SUCCESS(HttpStatus.OK, "회원 탈퇴를 성공하였습니다."),
GET_SOCIAL_ACCESS_TOKEN_SUCCESS(HttpStatus.OK, "소셜 액세스 토큰 발급을 성공했습니다."),

// 201 CREATED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sopt.org.hmh.domain.auth.exception.AuthSuccess;
import sopt.org.hmh.domain.users.domain.exception.UserSuccess;
import sopt.org.hmh.domain.users.service.UserService;
import sopt.org.hmh.global.auth.UserId;
import sopt.org.hmh.global.common.response.BaseResponse;
Expand All @@ -25,32 +25,32 @@ public class UserController implements UserApi{
public ResponseEntity<BaseResponse<?>> orderLogout(@UserId final Long userId) {
userService.logout(userId);
return ResponseEntity
.status(AuthSuccess.LOGOUT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.LOGOUT_SUCCESS, new EmptyJsonResponse()));
.status(UserSuccess.LOGOUT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.LOGOUT_SUCCESS, new EmptyJsonResponse()));
}

@GetMapping
@Override
public ResponseEntity<BaseResponse<?>> orderGetUserInfo(@UserId final Long userId) {
return ResponseEntity
.status(AuthSuccess.GET_USER_INFO_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.GET_USER_INFO_SUCCESS, userService.getUserInfo(userId)));
.status(UserSuccess.GET_USER_INFO_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.GET_USER_INFO_SUCCESS, userService.getUserInfo(userId)));
}

@GetMapping("/point")
@Override
public ResponseEntity<BaseResponse<?>> orderGetUserPoint(@UserId final Long userId) {
return ResponseEntity
.status(AuthSuccess.GET_USER_POINT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.GET_USER_POINT_SUCCESS, userService.getUserInfo(userId).point()));
.status(UserSuccess.GET_USER_POINT_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.GET_USER_POINT_SUCCESS, userService.getUserInfo(userId).point()));
}

@DeleteMapping
public ResponseEntity<BaseResponse<?>> orderWithdraw(@UserId final Long userId) {
userService.withdraw(userId);
return ResponseEntity
.status(AuthSuccess.WITHDRAW_SUCCESS.getHttpStatus())
.body(BaseResponse.success(AuthSuccess.WITHDRAW_SUCCESS, new EmptyJsonResponse()));
.status(UserSuccess.WITHDRAW_SUCCESS.getHttpStatus())
.body(BaseResponse.success(UserSuccess.WITHDRAW_SUCCESS, new EmptyJsonResponse()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sopt.org.hmh.domain.users.domain.exception;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import sopt.org.hmh.global.common.exception.base.ErrorBase;

@AllArgsConstructor
public enum UserError implements ErrorBase {

// 404 NOT FOUND
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."),
;

private final HttpStatus status;
private final String errorMessage;

@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getErrorMessage() {
return this.errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.org.hmh.domain.users.domain.exception;

import sopt.org.hmh.global.common.exception.base.ExceptionBase;

public class UserException extends ExceptionBase {

public UserException(UserError errorBase) {
super(errorBase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sopt.org.hmh.domain.users.domain.exception;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import sopt.org.hmh.global.common.exception.base.SuccessBase;

@AllArgsConstructor
public enum UserSuccess implements SuccessBase {

// 200 OK
GET_USER_INFO_SUCCESS(HttpStatus.OK, "유저의 정보를 불러오는데에 성공했습니다."),
GET_USER_POINT_SUCCESS(HttpStatus.OK, "유저의 포인트 정보를 불러오는데에 성공했습니다."),
LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공했습니다."),
WITHDRAW_SUCCESS(HttpStatus.OK, "회원 탈퇴를 성공하였습니다."),
;

private final HttpStatus status;
private final String successMessage;

@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getSuccessMessage() {
return this.successMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sopt.org.hmh.domain.users.domain.User;
import sopt.org.hmh.domain.auth.exception.AuthError;
import sopt.org.hmh.domain.auth.exception.AuthException;
import sopt.org.hmh.domain.users.domain.exception.UserError;
import sopt.org.hmh.domain.users.domain.exception.UserException;
import sopt.org.hmh.global.auth.social.SocialPlatform;

public interface UserRepository extends JpaRepository<User, Long> {
Expand All @@ -19,8 +21,8 @@ default User findBySocialPlatformAndSocialIdOrThrowException(SocialPlatform soci
}

default User findByIdOrThrowException(Long userId) {
return findById(userId).orElseThrow(() -> new AuthException(
AuthError.NOT_FOUND_USER));
return findById(userId).orElseThrow(() -> new UserException(
UserError.NOT_FOUND_USER));
}

@Query("SELECT u.id FROM User u WHERE u.deletedAt < :now AND u.isDeleted = true")
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/sopt/org/hmh/global/auth/jwt/TokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class TokenService {

@Transactional
public ReissueResponse reissueToken(String refreshToken) {
refreshToken = parseTokenString(refreshToken);
Long userId = jwtProvider.getSubject(refreshToken);
jwtValidator.validateRefreshToken(refreshToken);
String parsedRefreshToken = parseTokenString(refreshToken);
Long userId = jwtProvider.getSubject(parsedRefreshToken);
jwtValidator.validateRefreshToken(parsedRefreshToken);
redisManagerService.deleteRefreshToken(userId);
return ReissueResponse.of(jwtProvider.issueToken(userId));
}
Expand Down

0 comments on commit a127e30

Please sign in to comment.