diff --git a/build.gradle b/build.gradle index d42fe04..a432ad1 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'io.jsonwebtoken:jjwt:0.9.1' implementation 'javax.xml.bind:jaxb-api:2.3.1' + implementation 'org.springframework.boot:spring-boot-starter-data-redis' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/hatcher/haemo/common/BaseException.java b/src/main/java/com/hatcher/haemo/common/BaseException.java deleted file mode 100644 index 4555284..0000000 --- a/src/main/java/com/hatcher/haemo/common/BaseException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.hatcher.haemo.common; - -import com.hatcher.haemo.common.enums.BaseResponseStatus; -import org.springframework.web.server.ResponseStatusException; - -public class BaseException extends ResponseStatusException { - private final boolean success; - private final Object data; - - public BaseException(BaseResponseStatus status) { - super(status.getHttpStatus(), status.getMessage()); - this.success = false; - this.data = null; - } -} diff --git a/src/main/java/com/hatcher/haemo/common/BaseResponse.java b/src/main/java/com/hatcher/haemo/common/BaseResponse.java index 50b4ea5..e636c68 100644 --- a/src/main/java/com/hatcher/haemo/common/BaseResponse.java +++ b/src/main/java/com/hatcher/haemo/common/BaseResponse.java @@ -14,37 +14,33 @@ @JsonPropertyOrder({"isSuccess", "code", "message", "result"}) public class BaseResponse { - private final int code; - @JsonProperty("isSuccess") private final Boolean isSuccess; + private final String message; + private final int code; @JsonInclude(JsonInclude.Include.NON_NULL) private T result; - - public static BaseResponse success() { - return new BaseResponse<>( - SUCCESS.getCode(), - SUCCESS.isSuccess(), - SUCCESS.getMessage(), - null); + public BaseResponse(T result) { + this.isSuccess = SUCCESS.isSuccess(); + this.message = SUCCESS.getMessage(); + this.code = SUCCESS.getCode(); + this.result = result; } - public static BaseResponse success(T result) { - return new BaseResponse<>( - SUCCESS.getCode(), - SUCCESS.isSuccess(), - SUCCESS.getMessage(), - result); + public BaseResponse(BaseResponseStatus status) { + this.isSuccess = status.isSuccess(); + this.message = status.getMessage(); + this.code = status.getCode(); } - public static BaseResponse failure(BaseResponseStatus status) { - return new BaseResponse<>( - status.getCode(), - status.isSuccess(), - status.getMessage(), - null); + // 요청은 성공했지만 데이터에 이상이 있는 경우 + public BaseResponse(T result, BaseResponseStatus status) { + this.isSuccess = status.isSuccess(); + this.message = status.getMessage(); + this.code = status.getCode(); + this.result = result; } -} +} \ No newline at end of file diff --git a/src/main/java/com/hatcher/haemo/common/enums/BaseResponseStatus.java b/src/main/java/com/hatcher/haemo/common/enums/BaseResponseStatus.java index 377e4ed..3edbbfd 100644 --- a/src/main/java/com/hatcher/haemo/common/enums/BaseResponseStatus.java +++ b/src/main/java/com/hatcher/haemo/common/enums/BaseResponseStatus.java @@ -20,9 +20,11 @@ public enum BaseResponseStatus { INVALID_ACCESS_TOKEN(false, HttpStatus.BAD_REQUEST, "잘못된 access token 입니다."), NULL_ACCESS_TOKEN(false, HttpStatus.BAD_REQUEST, "access token을 입력해주세요."), INVALID_JWT_SIGNATURE(false, HttpStatus.BAD_REQUEST, "유효하지 않은 JWT 시그니처입니다."), - EXPIRED_ACCESS_TOKEN(false, HttpStatus.BAD_REQUEST, "만료된 토큰입니다."), + EXPIRED_ACCESS_TOKEN(false, HttpStatus.BAD_REQUEST, "만료된 access token 입니다."), + EXPIRED_REFRESH_TOKEN(false, HttpStatus.BAD_REQUEST, "만료된 refresh token 입니다."), EMPTY_JWT_CLAIM(false, HttpStatus.BAD_REQUEST, "JWT claims string이 비었습니다."), UNSUPPORTED_JWT_TOKEN(false, HttpStatus.BAD_REQUEST, "지원하지 않는 JWT 토큰 형식입니다."), + INVALID_REFRESH_TOKEN(false, HttpStatus.BAD_REQUEST, "유효하지 않은 refresh token 입니다."), ACCESS_DENIED(false, HttpStatus.FORBIDDEN, "접근 권한이 없습니다."), diff --git a/src/main/java/com/hatcher/haemo/common/enums/RecruitType.java b/src/main/java/com/hatcher/haemo/common/enums/RecruitType.java index 544b19d..201abb8 100644 --- a/src/main/java/com/hatcher/haemo/common/enums/RecruitType.java +++ b/src/main/java/com/hatcher/haemo/common/enums/RecruitType.java @@ -1,7 +1,6 @@ package com.hatcher.haemo.common.enums; -import com.hatcher.haemo.common.BaseException; - +import com.hatcher.haemo.common.exception.BaseException; import java.util.Arrays; import static com.hatcher.haemo.common.enums.BaseResponseStatus.WRONG_RECRUIT_TYPE; @@ -16,12 +15,21 @@ public enum RecruitType { COIN_KARAOKE("코인노래방"), DELIVERY_FOOD("배달음식"); - RecruitType(String name) {} + private final String description; // 설명 필드 추가 + + RecruitType(String description) { + this.description = description; + } + + public String getDescription() { // 설명을 반환하는 메소드 + return description; + } - public static RecruitType getEnumByName(String name) throws BaseException { + public static RecruitType getEnumByName(String name) throws BaseException{ return Arrays.stream(RecruitType.values()) - .filter(recruitType -> recruitType.name().equalsIgnoreCase(name)) + .filter(recruitType -> recruitType.getDescription().equalsIgnoreCase(name)) .findFirst() + //.orElseThrow(() -> new IllegalArgumentException("찾을 수 없습니다.")); .orElseThrow(() -> new BaseException(WRONG_RECRUIT_TYPE)); } } diff --git a/src/main/java/com/hatcher/haemo/common/exception/BaseException.java b/src/main/java/com/hatcher/haemo/common/exception/BaseException.java new file mode 100644 index 0000000..83dec33 --- /dev/null +++ b/src/main/java/com/hatcher/haemo/common/exception/BaseException.java @@ -0,0 +1,13 @@ +package com.hatcher.haemo.common.exception; + +import com.hatcher.haemo.common.enums.BaseResponseStatus; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class BaseException extends Exception { + private BaseResponseStatus status; +} diff --git a/src/main/java/com/hatcher/haemo/recruitment/application/RecruitmentService.java b/src/main/java/com/hatcher/haemo/recruitment/application/RecruitmentService.java index 50ba942..7fca929 100644 --- a/src/main/java/com/hatcher/haemo/recruitment/application/RecruitmentService.java +++ b/src/main/java/com/hatcher/haemo/recruitment/application/RecruitmentService.java @@ -1,6 +1,7 @@ package com.hatcher.haemo.recruitment.application; -import com.hatcher.haemo.common.BaseException; +import com.hatcher.haemo.common.BaseResponse; +import com.hatcher.haemo.common.exception.BaseException; import com.hatcher.haemo.common.enums.RecruitType; import com.hatcher.haemo.recruitment.domain.Recruitment; import com.hatcher.haemo.recruitment.dto.RecruitmentPostRequest; @@ -13,8 +14,7 @@ import org.springframework.transaction.annotation.Transactional; import static com.hatcher.haemo.common.constants.Constant.Recruitment.RECRUITING; -import static com.hatcher.haemo.common.enums.BaseResponseStatus.INTERNAL_SERVER_ERROR; -import static com.hatcher.haemo.common.enums.BaseResponseStatus.INVALID_USER_IDX; +import static com.hatcher.haemo.common.enums.BaseResponseStatus.*; @Service @RequiredArgsConstructor @@ -26,7 +26,7 @@ public class RecruitmentService { // 모집글 등록 @Transactional(rollbackFor = Exception.class) - public void postRecruitment(RecruitmentPostRequest recruitmentPostRequest) throws BaseException { + public BaseResponse postRecruitment(RecruitmentPostRequest recruitmentPostRequest) throws BaseException{ try { User leader = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX)); @@ -34,6 +34,7 @@ public void postRecruitment(RecruitmentPostRequest recruitmentPostRequest) throw recruitmentPostRequest.participantLimit(), recruitmentPostRequest.contactUrl(), recruitmentPostRequest.description()); recruitment.setStatus(RECRUITING); recruitmentRepository.save(recruitment); + return new BaseResponse<>(SUCCESS); } catch (BaseException e) { throw e; } catch (Exception e) { diff --git a/src/main/java/com/hatcher/haemo/recruitment/presentation/RecruitmentController.java b/src/main/java/com/hatcher/haemo/recruitment/presentation/RecruitmentController.java index 7fbaf7a..f4accb5 100644 --- a/src/main/java/com/hatcher/haemo/recruitment/presentation/RecruitmentController.java +++ b/src/main/java/com/hatcher/haemo/recruitment/presentation/RecruitmentController.java @@ -1,9 +1,8 @@ package com.hatcher.haemo.recruitment.presentation; -import com.hatcher.haemo.common.BaseException; +import com.hatcher.haemo.common.exception.BaseException; import com.hatcher.haemo.common.BaseResponse; import com.hatcher.haemo.recruitment.application.RecruitmentService; -import com.hatcher.haemo.recruitment.dto.RecruitmentListResponse; import com.hatcher.haemo.recruitment.dto.RecruitmentPostRequest; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -20,8 +19,11 @@ public class RecruitmentController { // 모집글 등록 @PostMapping("") public BaseResponse postRecruitment(@RequestBody RecruitmentPostRequest recruitmentPostRequest) { - recruitmentService.postRecruitment(recruitmentPostRequest); - return BaseResponse.success(); + try { + return recruitmentService.postRecruitment(recruitmentPostRequest); + } catch(BaseException e) { + return new BaseResponse<>(e.getStatus()); + } } // // 모집글 목록 조회 diff --git a/src/main/java/com/hatcher/haemo/user/application/UserService.java b/src/main/java/com/hatcher/haemo/user/application/UserService.java index 4b9772f..379a7e6 100644 --- a/src/main/java/com/hatcher/haemo/user/application/UserService.java +++ b/src/main/java/com/hatcher/haemo/user/application/UserService.java @@ -1,10 +1,8 @@ package com.hatcher.haemo.user.application; -import com.hatcher.haemo.common.BaseException; +import com.hatcher.haemo.common.exception.*; import com.hatcher.haemo.user.domain.User; -import com.hatcher.haemo.user.dto.LoginRequest; -import com.hatcher.haemo.user.dto.SignupRequest; -import com.hatcher.haemo.user.dto.TokenResponse; +import com.hatcher.haemo.user.dto.*; import com.hatcher.haemo.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -37,7 +35,7 @@ public TokenResponse signup(SignupRequest signupRequest) throws BaseException { // 로그인 @Transactional(rollbackFor = Exception.class) - public TokenResponse login(LoginRequest loginRequest) throws BaseException { + public TokenResponse login(LoginRequest loginRequest) throws BaseException{ try { User user = userRepository.findByLoginId(loginRequest.loginId()).orElseThrow(() -> new BaseException(LOGIN_ID_NOT_FOUND)); if(!encoder.matches(loginRequest.password(), user.getPassword())) throw new BaseException(WRONG_PASSWORD); @@ -67,11 +65,12 @@ public void validateLoginId(String loginId) throws BaseException { public User getUserByUserIdx(Long userIdx) { if(userIdx == null) return null; else { - Optional user = userRepository.findById(userIdx); + Optional user = userRepository.findByUserIdx(userIdx); return user.orElse(null); } } + // 회원만 public Long getUserIdxWithValidation() throws BaseException { Long userIdx = authService.getUserIdx(); if (userIdx == null) throw new BaseException(NULL_ACCESS_TOKEN); diff --git a/src/main/java/com/hatcher/haemo/user/presentation/UserController.java b/src/main/java/com/hatcher/haemo/user/presentation/UserController.java index 564be33..a3e7a25 100644 --- a/src/main/java/com/hatcher/haemo/user/presentation/UserController.java +++ b/src/main/java/com/hatcher/haemo/user/presentation/UserController.java @@ -1,6 +1,6 @@ package com.hatcher.haemo.user.presentation; -import com.hatcher.haemo.common.BaseException; +import com.hatcher.haemo.common.exception.BaseException; import com.hatcher.haemo.common.BaseResponse; import com.hatcher.haemo.user.application.UserService; import com.hatcher.haemo.user.dto.*; @@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*; import static com.hatcher.haemo.common.constants.RequestURI.user; +import static com.hatcher.haemo.common.enums.BaseResponseStatus.SUCCESS; @RestController @RequiredArgsConstructor @@ -18,26 +19,42 @@ public class UserController { // 회원가입 @PostMapping(value = "/signup") public BaseResponse signup(@RequestBody SignupRequest signupRequest) { - return BaseResponse.success(userService.signup(signupRequest)); + try { + return new BaseResponse<>(userService.signup(signupRequest)); + } catch(BaseException e) { + return new BaseResponse<>(e.getStatus()); + } } // 로그인 @PostMapping("/login") public BaseResponse login(@RequestBody LoginRequest loginRequest) { - return BaseResponse.success(userService.login(loginRequest)); + try { + return new BaseResponse<>(userService.login(loginRequest)); + } catch(BaseException e) { + return new BaseResponse<>(e.getStatus()); + } } // 닉네임 중복 체크 @PostMapping("/nickname") public BaseResponse validateNickname(@RequestBody NicknameRequest nicknameRequest) { - userService.validateNickname(nicknameRequest.nickname()); - return BaseResponse.success(); + try { + userService.validateNickname(nicknameRequest.nickname()); + return new BaseResponse<>(SUCCESS); + } catch(BaseException e) { + return new BaseResponse<>(e.getStatus()); + } } // 아이디 중복 체크 @PostMapping("/loginId") public BaseResponse validateLoginId(@RequestBody LoginIdRequest loginIdRequest) { - userService.validateLoginId(loginIdRequest.loginId()); - return BaseResponse.success(); + try { + userService.validateLoginId(loginIdRequest.loginId()); + return new BaseResponse<>(SUCCESS); + } catch(BaseException e) { + return new BaseResponse<>(e.getStatus()); + } } } diff --git a/src/main/java/com/hatcher/haemo/user/repository/UserRepository.java b/src/main/java/com/hatcher/haemo/user/repository/UserRepository.java index 1ded60e..58ffb46 100644 --- a/src/main/java/com/hatcher/haemo/user/repository/UserRepository.java +++ b/src/main/java/com/hatcher/haemo/user/repository/UserRepository.java @@ -10,4 +10,6 @@ public interface UserRepository extends JpaRepository { Optional findByUserIdxAndStatusEquals(Long userIdx, String status); boolean existsByNickname(String nickname); boolean existsByLoginId(String loginId); + Optional findByUserIdx(Long userIdx); + Optional findByLoginIdAndStatusEquals(String loginId, String status); }