-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Princess-in-silvertown/feat/6
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/slvtwn/khu/toyouserver/common/ApiResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/slvtwn/khu/toyouserver/common/ErrorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/slvtwn/khu/toyouserver/common/PageInfoResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/main/java/slvtwn/khu/toyouserver/common/SuccessType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |