Skip to content

Commit

Permalink
Merge pull request #9 from fingersdanny/feat-member
Browse files Browse the repository at this point in the history
[refactor] controller 수정 및 커스텀 코드를 위한 ServiceStatus 및 모든 RepsonseEntity 생성 통일을 위한 ResponseEntityFactory 생성
  • Loading branch information
hodadako authored Dec 21, 2023
2 parents 43d4633 + 0c32829 commit e5073e6
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import com.kernel360.kernelsquare.domain.member.dto.UpdateMemberRequest;
import com.kernel360.kernelsquare.domain.member.service.MemberService;
import com.kernel360.kernelsquare.global.common_response.ApiResponse;
import com.kernel360.kernelsquare.global.common_response.ResponseEntityFactory;

import lombok.RequiredArgsConstructor;

// todo : data가 존재하지 않는 응답일 때 data에 어떤 것을 넣을지? 혹은 어떻게 리팩토링을 통해서 data가 없는 응답을 만들어낼지..

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
Expand All @@ -30,25 +29,25 @@ public class MemberController {
public ResponseEntity<ApiResponse> updateMember(@PathVariable Long memberId,
@RequestBody UpdateMemberRequest updateMemberRequest) {
memberService.updateMember(memberId, updateMemberRequest);
return ResponseEntity.ok(ApiResponse.of(MEMBER_INFO_UPDATED));
return ResponseEntityFactory.toResponseEntity(MEMBER_INFO_UPDATED);
}

@PutMapping("/members/{memberId}/password")
public ResponseEntity<ApiResponse> updateMemberPassword(@PathVariable Long memberId,
@RequestBody String password) {
memberService.updateMemberPassword(memberId, password);
return ResponseEntity.ok(ApiResponse.of(MEMBER_PASSWORD_UPDATED));
return ResponseEntityFactory.toResponseEntity(MEMBER_PASSWORD_UPDATED);
}

@GetMapping("/members/{memberId}")
public ResponseEntity<ApiResponse<FindMemberResponse>> findMember(@PathVariable Long memberId) {
FindMemberResponse findMemberResponse = memberService.findMember(memberId);
return ResponseEntity.ok(ApiResponse.of(MEMBER_FOUND, findMemberResponse));
return ResponseEntityFactory.toResponseEntity(MEMBER_FOUND, findMemberResponse);
}

@DeleteMapping("/members/{memberId}")
public ResponseEntity<ApiResponse> deleteMember(@PathVariable Long memberId) {
memberService.deleteMember(memberId);
return ResponseEntity.ok(ApiResponse.of(MEMBER_DELETED));
return ResponseEntityFactory.toResponseEntity(MEMBER_DELETED);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void deleteMember(Long id) {

private Member getMemberById(Long id) {
return memberRepository.findById(id)
.orElseThrow(() -> new BusinessException(MemberErrorCode.NOT_FOUND_MEMBER));
.orElseThrow(() -> new BusinessException(MemberErrorCode.MEMBER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static <T> ApiResponse<T> of(
StatusCode statusCode,
T data) {
return new ApiResponse(
statusCode.getStatus().value(),
statusCode.getCode(),
statusCode.getMsg(),
data
);
Expand All @@ -23,7 +23,7 @@ public static <T> ApiResponse<T> of(
StatusCode statusCode
) {
return new ApiResponse<>(
statusCode.getStatus().value(),
statusCode.getCode(),
statusCode.getMsg(),
null
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kernel360.kernelsquare.global.common_response;

import org.springframework.http.ResponseEntity;

public class ResponseEntityFactory {
public static ResponseEntity<ApiResponse> toResponseEntity(StatusCode statusCode) {
return ResponseEntity.status(statusCode.getStatus()).body(ApiResponse.of(statusCode));
}

public static <T> ResponseEntity<ApiResponse<T>> toResponseEntity(StatusCode statusCode, T data) {
return ResponseEntity.status(statusCode.getStatus()).body(ApiResponse.of(statusCode, data));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
public interface StatusCode {
HttpStatus getStatus();

Integer getCode();

String getMsg();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.kernel360.kernelsquare.global.common_response.error.dto.ErrorResponse;
import com.kernel360.kernelsquare.global.common_response.ApiResponse;
import com.kernel360.kernelsquare.global.common_response.error.exception.BusinessException;

import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -13,9 +13,9 @@
public class GlobalExceptionHandler {

@ExceptionHandler(BusinessException.class)
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e,
public ResponseEntity<ApiResponse> handleBusinessException(BusinessException e,
HttpServletRequest request) {
return ResponseEntity.status(e.getErrorCode().getStatus())
.body(ErrorResponse.of(e.getErrorCode()));
.body(ApiResponse.of(e.getErrorCode()));
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.kernel360.kernelsquare.global.common_response.error.code;

import org.springframework.http.HttpStatus;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum AuthErrorCode implements ErrorCode {
INVALID_ACCOUNT(HttpStatus.UNAUTHORIZED, "계정정보가 일치하지 않습니다."),
INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."),
ALREADY_SAVED_NICKNAME(HttpStatus.CONFLICT, "사용 중인 닉네임입니다."),
ALREADY_SAVED_EMAIL(HttpStatus.CONFLICT, "사용 중인 이메일입니다."),
ALREADY_SOCIAL_LOGIN(HttpStatus.CONFLICT, "소셜로 가입한 회원입니다. 소셜로 로그인 해주세요.");

private final HttpStatus code;
private final String msg;

@Override
public HttpStatus getStatus() {
return code;
}

@Override
public String getMsg() {
return msg;
}
}

// package com.kernel360.kernelsquare.global.common_response.error.code;
//
// import org.springframework.http.HttpStatus;
//
// import com.kernel360.kernelsquare.global.common_response.service.code.ServiceStatus;
//
// import lombok.RequiredArgsConstructor;
//
// @RequiredArgsConstructor
// public enum AuthErrorCode implements ErrorCode {
// INVALID_ACCOUNT(HttpStatus.UNAUTHORIZED, "계정정보가 일치하지 않습니다."),
// INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."),
// ALREADY_SAVED_NICKNAME(HttpStatus.CONFLICT, "사용 중인 닉네임입니다."),
// ALREADY_SAVED_EMAIL(HttpStatus.CONFLICT, "사용 중인 이메일입니다."),
// ALREADY_SOCIAL_LOGIN(HttpStatus.CONFLICT, "소셜로 가입한 회원입니다. 소셜로 로그인 해주세요.");
//
// private final HttpStatus code;
// private final ServiceStatus serviceStatus;
// private final String msg;
//
// @Override
// public HttpStatus getStatus() {
// return code;
// }
//
// @Override
// public Integer getCode() {
// return serviceStatus.getServiceStatus();
// }
//
// @Override
// public String getMsg() {
// return msg;
// }
// }
//
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@

import org.springframework.http.HttpStatus;

import com.kernel360.kernelsquare.global.common_response.service.code.MemberServiceStatus;
import com.kernel360.kernelsquare.global.common_response.service.code.ServiceStatus;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum MemberErrorCode implements ErrorCode {
NOT_FOUND_MEMBER(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다.");
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, MemberServiceStatus.MEMBER_NOT_FOUND, "존재하지 않는 회원입니다.");

private final HttpStatus code;
private final ServiceStatus serviceStatus;
private final String msg;

@Override
public HttpStatus getStatus() {
return code;
}

@Override
public Integer getCode() {
return serviceStatus.getServiceStatus();
}

@Override
public String getMsg() {
return msg;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import org.springframework.http.HttpStatus;

import com.kernel360.kernelsquare.global.common_response.service.code.MemberServiceStatus;
import com.kernel360.kernelsquare.global.common_response.service.code.ServiceStatus;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum MemberResponseCode implements ResponseCode {
MEMBER_FOUND(HttpStatus.OK, "회원 정보 조회 성공"),
MEMBER_PASSWORD_UPDATED(HttpStatus.OK, "비밀번호 수정 완료"),
MEMBER_INFO_UPDATED(HttpStatus.OK, "회원 정보 수정 성공"),
MEMBER_DELETED(HttpStatus.OK, "회원 탈퇴 성공");
MEMBER_FOUND(HttpStatus.OK, MemberServiceStatus.MEMBER_FOUND, "회원 정보 조회 성공"),
MEMBER_PASSWORD_UPDATED(HttpStatus.OK, MemberServiceStatus.MEMBER_PASSWORD_UPDATED, "비밀번호 수정 성공"),
MEMBER_INFO_UPDATED(HttpStatus.OK, MemberServiceStatus.MEMBER_INFO_UPDATED, "회원 정보 수정 성공"),
MEMBER_DELETED(HttpStatus.OK, MemberServiceStatus.MEMBER_DELETED, "회원 탈퇴 성공");

private final HttpStatus code;
private final ServiceStatus serviceStatus;
private final String msg;

@Override
public HttpStatus getStatus() {
return code;
}

@Override
public Integer getCode() {
return serviceStatus.getServiceStatus();
}

@Override
public String getMsg() {
return msg;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kernel360.kernelsquare.global.common_response.service.code;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum MemberServiceStatus implements ServiceStatus {
MEMBER_NOT_FOUND(1201),
MEMBER_FOUND(1240),
MEMBER_PASSWORD_UPDATED(1241),
MEMBER_INFO_UPDATED(1242),
MEMBER_DELETED(1243);

private final Integer code;

@Override
public Integer getServiceStatus() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kernel360.kernelsquare.global.common_response.service.code;

public interface ServiceStatus {
Integer getServiceStatus();
}
Loading

0 comments on commit e5073e6

Please sign in to comment.