Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

โšก Feat : Define common api response #9

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/common/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package slvtwn.khu.toyouserver.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Getter;

@Getter
@JsonPropertyOrder({"code", "message", "data", "pageInfo"})
public class ApiResponse<T> {

private final String code;

private final String message;

@JsonInclude(Include.NON_NULL)
private final T data;

private final PageInfoResponse pageInfo;

ApiResponse(String code, String message) {
this(code, message, null, null);
}

ApiResponse(String code, String message, T data) {
this(code, message, data, null);
}

ApiResponse(String code, String message, T data, PageInfoResponse pageInfo) {
this.code = code;
this.message = message;
this.data = data;
this.pageInfo = pageInfo;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ์„ฑ์ž ์ฒด์ด๋‹์œผ๋กœ ๋‘๋ฉด ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!


public static <T> ApiResponse<T> success(SuccessType successType) {
return new ApiResponse<>(successType.getCode(), successType.getMessage());
}

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

public static <T> ApiResponse<T> success(SuccessType successType, T data, PageInfoResponse pageInfo) {
return new ApiResponse<>(successType.getCode(), successType.getMessage(), data, pageInfo);
}

public static ApiResponse<?> error(ErrorType errorType) {
return new ApiResponse<>(errorType.getCode(), errorType.getMessage());
}

public static <T> ApiResponse<T> error(ErrorType errorType, T data) {
return new ApiResponse<>(errorType.getCode(), errorType.getMessage(), data);
}

public static ApiResponse<?> error(ErrorType errorType, String message) {
return new ApiResponse<>(errorType.getCode(), message);
}

public static <T> ApiResponse<T> error(ErrorType errorType, String message, T data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—ฌ๊ธฐ๋„์š”!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

success, error ์ผ€์ด์Šค๊ฐ€ ๋„ˆ๋ฌด ๋‚œ์žกํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์‘๋‹ต ์ฒ˜๋ฆฌ๋ฅผ ApiResponseAdvice์—์„œ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿค”

return new ApiResponse<>(errorType.getCode(), message, data);
}

public static <T> ApiResponse<Exception> error(ErrorType errorType, Exception e) {
return new ApiResponse<>(errorType.getCode(), errorType.getMessage(), e);
}

}
17 changes: 17 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/common/ErrorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package slvtwn.khu.toyouserver.common;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum ErrorType {

// 404 (Not Found)
NOT_FOUND("TYU404", "Not Found");

private final String code;

private final String message;
}
19 changes: 19 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/common/PageInfoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package slvtwn.khu.toyouserver.common;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PageInfoResponse {
private final int currentPage;

private final int totalPage;

private final int totalElements;

public static PageInfoResponse of(int currentPage, int totalPage, int totalElements) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ชจ๋“  ์š”์ฒญ์ด paging์ด ๋“ค์–ด๊ฐ€์ง€๋Š” ์•Š์•„์„œ, PageInfoResponse๋ฅผ ๊ณตํ†ต ์‘๋‹ต์— ํ•„๋“œ๋กœ ๋‘๋Š” ๊ฒƒ๋ณด๋‹ค data์˜ ํ•˜์œ„๋กœ ๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ๋งŒ PageInfoResponse ๋“ฑ์œผ๋กœ ๋‹ด์•„์„œ ์ฃผ๋Š” ๋ฐฉ์‹๋„ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์•„ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

์‘๋‹ต ํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์–ด๋– ์‹ค๊นŒ์š” ? ๐Ÿค”

{
    "timestamp": "2024.07.06 00:35:58",
    "code": "TYU-200",
    "message": "์‘๋‹ต ์„ฑ๊ณต",
    "data": {
        "id": 1,
        "name": "health",
        "pageInfo": {
            "currentPage": 1,
            "totalPage": 1,
            "totalElements": 1
        }
    }
}

return new PageInfoResponse(currentPage, totalPage, totalElements);
}
}
16 changes: 16 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/common/SuccessType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package slvtwn.khu.toyouserver.common;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum SuccessType {

// 200 (OK)
OK("TYU200", "success");

private final String code;
private final String message;
}