Skip to content

Commit

Permalink
♻️ Refactor: 공통 응답 생성 팩토리 메소드 분리 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
loveysuby committed Jul 5, 2024
1 parent d80061e commit f29d722
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
12 changes: 0 additions & 12 deletions src/main/java/slvtwn/khu/toyouserver/common/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,4 @@ public class ApiResponse {
this.message = message;
this.data = data;
}

public static ApiResponse success(SuccessType successType, Object data) {
return new ApiResponse(successType.getCode(), successType.getMessage(), data);
}

public static ApiResponse error(ErrorType errorType) {
return new ApiResponse(errorType.code(), errorType.message());
}

public static ApiResponse error(ErrorType errorType, Object data) {
return new ApiResponse(errorType.code(), errorType.message(), data);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package slvtwn.khu.toyouserver.common;


import static slvtwn.khu.toyouserver.common.ApiResponseFactory.error;
import static slvtwn.khu.toyouserver.common.ApiResponseFactory.success;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -36,12 +39,12 @@ public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType

private Object createResponseByHttpStatus(HttpStatus status, Object body) {
if (status.is2xxSuccessful()) {
return ApiResponse.success(SuccessType.OK, body);
return success(SuccessType.OK, body);
} else if (status.is4xxClientError()) {
return ApiResponse.error(ErrorType.BAD_REQUEST);
return error(ErrorType.BAD_REQUEST);
} else if (status.is5xxServerError()) {
return ApiResponse.error(ErrorType.INTERNAL_SERVER_ERROR);
return error(ErrorType.INTERNAL_SERVER_ERROR);
}
return ApiResponse.error(ErrorType.RESPONSE_FORMAT_ERROR, body);
return error(ErrorType.RESPONSE_FORMAT_ERROR, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package slvtwn.khu.toyouserver.common;

class ApiResponseFactory {

static ApiResponse success(SuccessType successType, Object data) {
return new ApiResponse(successType.getCode(), successType.getMessage(), data);
}

static ApiResponse error(ErrorType errorType) {
return new ApiResponse(errorType.code(), errorType.message());
}

static ApiResponse error(ErrorType errorType, Object data) {
return new ApiResponse(errorType.code(), errorType.message(), data);
}
}

0 comments on commit f29d722

Please sign in to comment.