Skip to content

Commit

Permalink
♻️ Refactor: Api 응답 객체 리팩토링 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 15, 2023
1 parent f3c862c commit 4a889e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
16 changes: 0 additions & 16 deletions src/main/java/com/diareat/diareat/util/api/ApiBody.java

This file was deleted.

13 changes: 5 additions & 8 deletions src/main/java/com/diareat/diareat/util/api/ApiHeader.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.diareat.diareat.util.api;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ApiHeader {

private int code;
private String message;

public ApiHeader(int code, String message) {
this.code = code;
this.message = message;
}

public ApiHeader() {
}

public int getCode() {
return code;
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/diareat/diareat/util/api/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
public class ApiResponse<T> {

private ApiHeader header;
private ApiBody body;
private T data;
private String msg;

private static final int SUCCESS = 200;

public ApiResponse(ApiHeader header, ApiBody body) {
this.header = header;
this.body = body;
}

public ApiResponse(ApiHeader header) {
private ApiResponse(ApiHeader header, T data, String msg) {
this.header = header;
this.data = data;
this.msg = msg;
}

public static <T> ApiResponse<T> success(T data, String message) {
return new ApiResponse<T>(new ApiHeader(SUCCESS, "SUCCESS"), new ApiBody(data, message));
return new ApiResponse<T>(new ApiHeader(SUCCESS, "SUCCESS"), data, message);
}

public static <T> ApiResponse<T> fail(ResponseCode responseCode) {
return new ApiResponse(new ApiHeader(responseCode.getHttpStatusCode(), responseCode.getMessage()), new ApiBody(null, responseCode.getMessage()));
return new ApiResponse<T>(new ApiHeader(responseCode.getHttpStatusCode(), responseCode.getMessage()), null, responseCode.getMessage());
}
}

0 comments on commit 4a889e3

Please sign in to comment.