Skip to content
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
13 changes: 5 additions & 8 deletions src/main/java/com/catchtable/global/common/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
public class ApiResponse<T> {

private int status;
private String code;
private String message;
private T data;
Comment on lines 11 to 14

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

API 응답을 표현하는 ApiResponse 클래스는 불변(Immutable) 객체로 설계하는 것이 안전합니다. 현재 필드들이 final로 선언되어 있지 않아 외부에서 변경될 여지가 있습니다. 필드들을 final로 선언하여 객체의 불변성을 보장하고 안정성을 높이는 것을 권장합니다.

Suggested change
private int status;
private String code;
private String message;
private T data;
private final int status;
private final String code;
private final String message;
private final T data;


// 성공 (데이터 포함)
public static <T> ApiResponse<T> success(SuccessCode code, T data) {
return new ApiResponse<>(code.getHttpStatus().value(), code.getMessage(), data);
return new ApiResponse<>(code.getHttpStatus().value(), code.getCode(), code.getMessage(), data);
}

// 성공 (데이터 없음)
public static ApiResponse<Void> success(SuccessCode code) {
return new ApiResponse<>(code.getHttpStatus().value(), code.getMessage(), null);
return new ApiResponse<>(code.getHttpStatus().value(), code.getCode(), code.getMessage(), null);
}

// 실패 (데이터 없음)
public static ApiResponse<Void> error(ErrorCode code) {
return new ApiResponse<>(code.getHttpStatus().value(), code.getMessage(), null);
return new ApiResponse<>(code.getHttpStatus().value(), code.getCode(), code.getMessage(), null);
}

// 실패 (커스텀 메시지)
public static ApiResponse<Void> error(ErrorCode code, String message) {
return new ApiResponse<>(code.getHttpStatus().value(), message, null);
return new ApiResponse<>(code.getHttpStatus().value(), code.getCode(), message, null);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/catchtable/global/common/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
public interface ResponseCode {
HttpStatus getHttpStatus();
String getMessage();

default String getCode() {
return (this instanceof Enum<?> e) ? e.name() : getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ void generateTokens() throws IOException {

for (int i = 1; i <= userCount; i++) {
String googleId = GOOGLE_ID_PREFIX + i;
String email = EMAIL_PREFIX + i + EMAIL_DOMAIN;
String nickname = NICKNAME_PREFIX + i;
User user = userRepository.findByGoogleId(googleId)
.orElseGet(() -> userRepository.save(User.builder()
.googleId(googleId)
.email(EMAIL_PREFIX + i + EMAIL_DOMAIN)
.nickname(NICKNAME_PREFIX + i)
.email(email)
.nickname(nickname)
.profileImage(null)
.role(UserRole.USER)
.status(UserStatus.ACTIVE)
Expand Down
Loading