Skip to content

Commit

Permalink
Merge pull request #9 from Princess-in-silvertown/feat/6
Browse files Browse the repository at this point in the history
  • Loading branch information
woosung1223 authored Jul 1, 2024
2 parents 390671b + 5e277bb commit 7d07867
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
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;
}

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) {
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) {
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;
}

0 comments on commit 7d07867

Please sign in to comment.