Skip to content

Commit

Permalink
Merge pull request #71 from kduoh99/refactor/#58
Browse files Browse the repository at this point in the history
Refactor: 응닡 ν…œν”Œλ¦Ώ κ°œμ„ , 둜직 κ°„μ†Œν™”
  • Loading branch information
kduoh99 authored Aug 18, 2024
2 parents 5e4a139 + 200fccb commit f79059f
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ public ApiResponseTemplate<AuthResDto> signUpOrLogin(String googleAccessToken) {
String refreshToken = tokenProvider.createRefreshToken(member);

tokenRenewService.saveRefreshToken(refreshToken, member.getMemberId());
AuthResDto resDto = AuthResDto.of(accessToken, refreshToken);

return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, AuthResDto.of(accessToken, refreshToken));
}

public MemberInfo getMemberInfo(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ public ApiResponseTemplate<AuthResDto> signUpOrLogin(String kakaoAccessToken) {
String refreshToken = tokenProvider.createRefreshToken(member);

tokenRenewService.saveRefreshToken(refreshToken, member.getMemberId());
AuthResDto resDto = AuthResDto.of(accessToken, refreshToken);

return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, AuthResDto.of(accessToken, refreshToken));
}

public MemberInfo getMemberInfo(String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public ApiResponseTemplate<AuthResDto> login(LoginReqDto loginReqDto) {
}

tokenRenewService.saveRefreshToken(refreshToken, member.getMemberId());
AuthResDto resDto = AuthResDto.of(accessToken, refreshToken);

return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.LOGIN_MEMBER_SUCCESS, AuthResDto.of(accessToken, refreshToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public ApiResponseTemplate<AuthResDto> signUp(SignUpReqDto signUpReqDto) {
String refreshToken = tokenProvider.createRefreshToken(member);

tokenRenewService.saveRefreshToken(refreshToken, member.getMemberId());
AuthResDto resDto = AuthResDto.of(accessToken, refreshToken);

return ApiResponseTemplate.success(SuccessCode.CREATE_MEMBER_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.CREATE_MEMBER_SUCCESS, AuthResDto.of(accessToken, refreshToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public ApiResponseTemplate<AuthResDto> renewAccessToken(String refreshToken) {
ErrorCode.NOT_FOUND_ID_EXCEPTION.getMessage()));

String renewAccessToken = tokenProvider.createAccessToken(member);
AuthResDto resDto = AuthResDto.of(renewAccessToken, refreshToken);

return ApiResponseTemplate.success(SuccessCode.GET_TOKEN_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.RENEW_TOKEN_SUCCESS, AuthResDto.of(renewAccessToken, refreshToken));
}

public void saveRefreshToken(String refreshToken, Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class FriendService {
private final FriendRepository friendRepository;

public ApiResponseTemplate<Void> sendFriendRequest(
FriendReqDto friendReqDto,
Principal principal) {
FriendReqDto friendReqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member requester = memberRepository.findById(memberId)
Expand All @@ -54,8 +53,7 @@ public ApiResponseTemplate<Void> sendFriendRequest(
}

public ApiResponseTemplate<Void> acceptFriendRequest(
String friendEmail,
Principal principal) {
String friendEmail, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member currentMember = memberRepository.findById(memberId)
Expand All @@ -78,8 +76,7 @@ public ApiResponseTemplate<Void> acceptFriendRequest(
}

public ApiResponseTemplate<Void> deleteFriend(
String friendEmail,
Principal principal) {
String friendEmail, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member currentMember = memberRepository.findById(memberId)
Expand All @@ -96,7 +93,7 @@ public ApiResponseTemplate<Void> deleteFriend(
friendInFriendList.ifPresent(friendRepository::delete);
return ApiResponseTemplate.success(SuccessCode.DELETE_FRIEND_SUCCESS, null);
} else {
return ApiResponseTemplate.error(ErrorCode.NOT_FOUND_FRIEND_REQUEST_EXCEPTION, ErrorCode.NOT_FOUND_FRIEND_REQUEST_EXCEPTION.getMessage());
return ApiResponseTemplate.error(ErrorCode.NOT_FOUND_FRIEND_REQUEST_EXCEPTION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public class AttendanceService {

private static final int MILEAGE_INCREMENT = 3;

private final AttendanceRepository attendanceRepository;
private final MemberRepository memberRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class ObjectStatusService {

@Transactional
public ApiResponseTemplate<ObjectResDto> updateObjectStatus(
UpdateObjectStatusReqDto reqDto,
Principal principal) {
UpdateObjectStatusReqDto reqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class PurchaseService {

@Transactional
public ApiResponseTemplate<ObjectResDto> purchaseObject(
PurchaseReqDto reqDto,
Principal principal) {
PurchaseReqDto reqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class OrderDisplayService {
private final OrderRepository orderRepository;
private final MemberRepository memberRepository;

public ApiResponseTemplate<List<OrderResDto>> getOrderHistory(Principal principal) {
Long memberId = Long.parseLong(principal.getName());
public ApiResponseTemplate<List<OrderResDto>> getOrderHistory(
Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION, ErrorCode.NOT_FOUND_MEMBER_EXCEPTION.getMessage()));


List<Order> orders = orderRepository.findByMember(member);

List<OrderResDto> resDtos = orders.stream()
Expand All @@ -42,9 +42,10 @@ public ApiResponseTemplate<List<OrderResDto>> getOrderHistory(Principal principa
return ApiResponseTemplate.success(SuccessCode.GET_ALL_ORDERS_SUCCESS, resDtos);
}

public ApiResponseTemplate<OrderResDto> getOrderDetail(Long orderId, Principal principal) {
Long memberId = Long.parseLong(principal.getName());
public ApiResponseTemplate<OrderResDto> getOrderDetail(
Long orderId, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION, ErrorCode.NOT_FOUND_MEMBER_EXCEPTION.getMessage()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public IamportResponse<Payment> cancelPayment(String imp_uid) {
}
}

public ApiResponseTemplate<OrderResDto> saveOrder(OrderReqDto reqDto, Principal principal) {
public ApiResponseTemplate<OrderResDto> saveOrder(
OrderReqDto reqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_MEMBER_EXCEPTION, ErrorCode.NOT_FOUND_MEMBER_EXCEPTION.getMessage()));

Expand All @@ -64,15 +64,6 @@ public ApiResponseTemplate<OrderResDto> saveOrder(OrderReqDto reqDto, Principal
throw new CustomException(ErrorCode.FAILED_ORDER_SAVE_EXCEPTION, ErrorCode.FAILED_ORDER_SAVE_EXCEPTION.getMessage());
}

OrderResDto resDto = OrderResDto.builder()
.orderId(order.getOrderId())
.productName(order.getProductName())
.price(order.getPrice())
.impUid(order.getImpUid())
.merchantUid(order.getMerchantUid())
.createdAt(order.getCreatedAt())
.build();

return ApiResponseTemplate.success(SuccessCode.ORDER_SAVE_SUCCESS, resDto);
return ApiResponseTemplate.success(SuccessCode.ORDER_SAVE_SUCCESS, OrderResDto.of(order));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class DiaryAIDisplayService {
private final MemberRepository memberRepository;

public ApiResponseTemplate<DiaryAIResDto> getDiarySummaryByCreatedAt(
LocalDate createdAt,
Principal principal) {
LocalDate createdAt, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class DiaryCreateService {

@Transactional
public ApiResponseTemplate<DiaryResDto> createDiary(
DiaryCreateReqDto reqDto,
Principal principal) {
DiaryCreateReqDto reqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public ApiResponseTemplate<DiaryResDto> getDiaryById(
return ApiResponseTemplate.success(SuccessCode.GET_DIARY_SUCCESS, DiaryResDto.of(diary));
}

public ApiResponseTemplate<List<DiaryResDto>> getAllDiariesByMember(Principal principal) {
public ApiResponseTemplate<List<DiaryResDto>> getAllDiariesByMember(
Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class DiaryModifyService {

@Transactional
public ApiResponseTemplate<DiaryResDto> updateDiary(
DiaryUpdateReqDto reqDto,
Principal principal) {
DiaryUpdateReqDto reqDto, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand All @@ -48,8 +47,7 @@ public ApiResponseTemplate<DiaryResDto> updateDiary(

@Transactional
public ApiResponseTemplate<Void> deleteDiary(
Long diaryId,
Principal principal) {
Long diaryId, Principal principal) {

Long memberId = Long.parseLong(principal.getName());
Member member = memberRepository.findById(memberId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ControllerExceptionAdvice {
@ExceptionHandler(CustomException.class)
protected ResponseEntity<ApiResponseTemplate<String>> handleCustomException(CustomException e) {
return ResponseEntity.status(e.getErrorCode().getHttpStatus())
.body(ApiResponseTemplate.error(e.getErrorCode(), e.getMessage()));
.body(ApiResponseTemplate.error(e.getErrorCode()));
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
Expand All @@ -31,8 +31,11 @@ public ResponseEntity<ApiResponseTemplate<String>> handleValidationExceptions(Me
Map<String, String> errorMap = e.getBindingResult().getFieldErrors().stream()
.collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage, (existing, replacement) -> existing));

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponseTemplate.error(ErrorCode.VALIDATION_EXCEPTION, convertMapToString(errorMap)));
return ResponseEntity.status(ErrorCode.VALIDATION_EXCEPTION.getHttpStatus())
.body(ApiResponseTemplate.<String>error(ErrorCode.VALIDATION_EXCEPTION)
.toBuilder()
.data(convertMapToString(errorMap))
.build());
}

@ExceptionHandler(HttpMessageNotReadableException.class)
Expand All @@ -45,12 +48,15 @@ public ResponseEntity<ApiResponseTemplate<String>> handleHttpMessageNotReadableE
String enumValues = getEnumValues(ife.getTargetType());
String message = String.format("ν—ˆμš©λ˜λŠ” κ°’: [%s]", enumValues);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponseTemplate.error(ErrorCode.INVALID_ENUM_VALUE, message));
.body(ApiResponseTemplate.<String>error(ErrorCode.INVALID_ENUM_VALUE)
.toBuilder()
.data(message)
.build());
}
}

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponseTemplate.error(ErrorCode.JSON_SYNTAX_ERROR, ErrorCode.JSON_SYNTAX_ERROR.getMessage()));
.body(ApiResponseTemplate.error(ErrorCode.JSON_SYNTAX_ERROR));
}

private static String getEnumValues(Class<?> enumClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.skhu.moodfriend.global.exception;

import com.skhu.moodfriend.global.exception.code.ErrorCode;
import lombok.Getter;

@Getter
public class CustomException extends RuntimeException {

private final ErrorCode errorCode;
Expand All @@ -10,8 +12,4 @@ public CustomException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public enum ErrorCode {
NOT_FOUND_EMAIL_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή μ΄λ©”μΌμ˜ μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_MEMBER_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή μ‚¬μš©μžλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_DIARY_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή 일기λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_FRIEND_REQUEST_EXCEPTION(HttpStatus.NOT_FOUND, "친ꡬ μš”μ²­μ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_ORDER_EXCEPTION(HttpStatus.NOT_FOUND, "μ£Όλ¬Έ 내역을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_OBJECT_EXCEPTION(HttpStatus.NOT_FOUND, "였브제λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_FRIEND_REQUEST_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή 친ꡬλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_ORDER_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή μ£Όλ¬Έ 내역을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
NOT_FOUND_OBJECT_EXCEPTION(HttpStatus.NOT_FOUND, "ν•΄λ‹Ή 였브제λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),

// 409 Conflict
ALREADY_EXIST_MEMBER_EXCEPTION(HttpStatus.CONFLICT, "이미 νšŒμ›κ°€μž…μ΄ μ™„λ£Œλœ μ‚¬μš©μžμž…λ‹ˆλ‹€."),
Expand All @@ -59,8 +59,4 @@ public enum ErrorCode {

private final HttpStatus httpStatus;
private final String message;

public int getHttpStatusCode() {
return httpStatus.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum SuccessCode {

// 200 OK
GET_TOKEN_SUCCESS(HttpStatus.OK, "Access 토큰을 μ„±κ³΅μ μœΌλ‘œ κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€."),
RENEW_TOKEN_SUCCESS(HttpStatus.OK, "Access 토큰을 μ„±κ³΅μ μœΌλ‘œ μž¬λ°œκΈ‰ν–ˆμŠ΅λ‹ˆλ‹€."),
LOGIN_MEMBER_SUCCESS(HttpStatus.OK, "λ‘œκ·ΈμΈμ— μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€."),
LOGOUT_MEMBER_SUCCESS(HttpStatus.OK, "λ‘œκ·Έμ•„μ›ƒμ— μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€."),
WITHDRAW_MEMBER_SUCCESS(HttpStatus.OK, "νšŒμ›νƒˆν‡΄μ— μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€."),
Expand Down Expand Up @@ -48,8 +49,4 @@ public enum SuccessCode {

private final HttpStatus httpStatus;
private final String message;

public int getHttpStatusCode() {
return httpStatus.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Builder(toBuilder = true)
public class ApiResponseTemplate<T> {

private final int status;
private final boolean success;
private final int status;
private final String message;
private T data;
private ErrorCode errorCode;

public static <T> ApiResponseTemplate<T> success(SuccessCode successCode, T data) {
return ApiResponseTemplate.<T>builder()
.status(successCode.getHttpStatus().value())
.success(true)
.status(successCode.getHttpStatus().value())
.message(successCode.getMessage())
.data(data)
.build();
}

public static <T> ApiResponseTemplate<T> error(ErrorCode errorCode, String message) {
public static <T> ApiResponseTemplate<T> error(ErrorCode errorCode) {
return ApiResponseTemplate.<T>builder()
.status(errorCode.getHttpStatus().value())
.success(false)
.message(message)
.errorCode(errorCode)
.status(errorCode.getHttpStatus().value())
.message(errorCode.getMessage())
.build();
}
}

0 comments on commit f79059f

Please sign in to comment.