Skip to content

Commit

Permalink
fix: common response type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Aug 10, 2024
1 parent 55440fa commit db719aa
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Backend CD # actions 이름

on:
push:
branches: [ feature/#8 ]
branches: [ feature/#18 ]

jobs:
deploy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class CuisineController {
public ApplicationResponse<List<IngredientInfo>> findIngredientList(@UserResolver User user) {

final List<IngredientInfo> ingredientInfo = cuisineFindUseCase.findIngredientInfo(user);
return ApplicationResponse.ok(ingredientInfo);
return ApplicationResponse.onSuccess(ingredientInfo);
}

// 식재료 추가하기
Expand All @@ -66,7 +66,7 @@ public ApplicationResponse<String> addIngredientList(@UserResolver User user,
@RequestPart(value = "file") MultipartFile multipartFile) {

cuisineSaveUseCase.saveIngredient(addReq, multipartFile, user);
return ApplicationResponse.ok("식재료 리스트 추가되었습니다.");
return ApplicationResponse.onSuccess("식재료 리스트 추가되었습니다.");
}

// 식재료 삭제하기
Expand All @@ -84,7 +84,7 @@ public ApplicationResponse<String> addIngredientList(@UserResolver User user,
public ApplicationResponse<String> deleteIngredient(@PathVariable Long ingredientId) {

cuisineSaveUseCase.deleteIngredient(ingredientId);
return ApplicationResponse.ok("식재료 리스트가 삭제되었습니다.");
return ApplicationResponse.onSuccess("식재료 리스트가 삭제되었습니다.");
}

// 테스트 조회
Expand All @@ -104,7 +104,7 @@ public ApplicationResponse<TestResultType> findTestResult(
) {

TestResultType type = cuisineFindUseCase.findTestResult(user);
return ApplicationResponse.ok(type);
return ApplicationResponse.onSuccess(type);
}

// 테스트 결과
Expand All @@ -124,7 +124,7 @@ public ApplicationResponse<String> saveTestResult(
@RequestBody TestResultType resultType) {

cuisineSaveUseCase.saveCuisineTest(user, resultType);
return ApplicationResponse.ok("결과가 저장되었습니다.");
return ApplicationResponse.onSuccess("결과가 저장되었습니다.");
}

// 조리법 결과 가져오기
Expand All @@ -143,7 +143,7 @@ public ApplicationResponse<List<RecipeDTO.RecipeDetail>> findRecipeResult(
@RequestBody RecipeDTO.Request request) throws URISyntaxException {

final List<RecipeDTO.RecipeDetail> recipeResult = cuisineFindUseCase.findRecipeResult(request);
return ApplicationResponse.ok(recipeResult);
return ApplicationResponse.onSuccess(recipeResult);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.foodgo.commonmodule.common.ApiResponse;
import com.foodgo.commonmodule.common.ApplicationResponse;
import com.foodgo.coremodule.ingredient.dto.request.IngredientGetRequest;
import com.foodgo.coremodule.ingredient.dto.response.IngredientGetResponse;
import com.foodgo.coremodule.security.annotation.UserResolver;
Expand All @@ -27,10 +27,10 @@ public class IngredientController {
private final IngredientFindUseCase ingredientFindUseCase;

@PostMapping("")
public ApiResponse<IngredientGetResponse.Row> getIngredientInfo(
public ApplicationResponse<IngredientGetResponse.Row> getIngredientInfo(
@UserResolver User user,
@RequestBody @Valid IngredientGetRequest request
) throws URISyntaxException {
return ApiResponse.onSuccess(ingredientFindUseCase.getIngredient(user, request));
return ApplicationResponse.onSuccess(ingredientFindUseCase.getIngredient(user, request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.foodgo.coremodule.user.domain.User;
import com.foodgo.coremodule.user.dto.request.UserRegisterRequest;
import com.foodgo.coremodule.user.dto.response.UserRegisterResponse;
import com.foodgo.commonmodule.common.ApiResponse;
import com.foodgo.commonmodule.common.ApplicationResponse;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -43,53 +43,53 @@ public class UserController {
private final UserQueryService userQueryService;

@PostMapping(value = "/join", consumes = "multipart/form-data")
public ApiResponse<UserRegisterResponse> register(
public ApplicationResponse<UserRegisterResponse> register(
@RequestPart(value = "request") @Valid UserRegisterRequest request,
@RequestPart(name = "profileImage", required = false) MultipartFile file
) {
return ApiResponse.onSuccess(userService.register(request, file));
return ApplicationResponse.onSuccess(userService.register(request, file));
}

@GetMapping("/username")
public ApiResponse<Boolean> checkUsername(@RequestParam String username) {
return ApiResponse.onSuccess(userQueryService.checkUsername(username));
public ApplicationResponse<Boolean> checkUsername(@RequestParam String username) {
return ApplicationResponse.onSuccess(userQueryService.checkUsername(username));
}

@GetMapping("/nickname")
public ApiResponse<Boolean> checkNickname(@RequestParam String nickname) {
return ApiResponse.onSuccess(userQueryService.checkNickname(nickname));
public ApplicationResponse<Boolean> checkNickname(@RequestParam String nickname) {
return ApplicationResponse.onSuccess(userQueryService.checkNickname(nickname));
}

@GetMapping("/reissue")
public ApiResponse<JwtDto> reissueToken(@RequestHeader("RefreshToken") String refreshToken) {
return ApiResponse.onSuccess(userService.reissueToken(refreshToken));
public ApplicationResponse<JwtDto> reissueToken(@RequestHeader("RefreshToken") String refreshToken) {
return ApplicationResponse.onSuccess(userService.reissueToken(refreshToken));
}

@PatchMapping(value = "/password")
public ApiResponse<String> updatePassword(
public ApplicationResponse<String> updatePassword(
@UserResolver User user,
@RequestBody @Valid PasswordUpdateRequest request
) {
userService.updatePassword(user, request);
return ApiResponse.onSuccess("비밀번호 변경 성공");
return ApplicationResponse.onSuccess("비밀번호 변경 성공");
}

@GetMapping("/me")
public ApiResponse<UserDetailGetResponse> getMyUser(@UserResolver User authUser) {
return ApiResponse.onSuccess(UserDetailGetResponse.from(authUser));
public ApplicationResponse<UserDetailGetResponse> getMyUser(@UserResolver User authUser) {
return ApplicationResponse.onSuccess(UserDetailGetResponse.from(authUser));
}

@PatchMapping(value = "/me", consumes = "multipart/form-data")
public ApiResponse<UserUpdateResponse> updateMyUser(
public ApplicationResponse<UserUpdateResponse> updateMyUser(
@UserResolver User user,
@RequestPart @Valid UserUpdateRequest request,
@RequestPart(value = "profileImage", required = false) MultipartFile file) {
return ApiResponse.onSuccess(userService.updateMyUser(user, request, file));
return ApplicationResponse.onSuccess(userService.updateMyUser(user, request, file));
}

@DeleteMapping("/me")
public ApiResponse<String> deleteUser(@UserResolver User user) {
public ApplicationResponse<String> deleteUser(@UserResolver User user) {
userService.deactivate(user);
return ApiResponse.onSuccess("탈퇴 성공");
return ApplicationResponse.onSuccess("탈퇴 성공");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,108 +1,53 @@
package com.foodgo.commonmodule.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.jackson.Jacksonized;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.http.HttpStatus;

@Data
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Jacksonized
@JsonPropertyOrder({"statusCode", "message", "content"})
public class ApplicationResponse<T> {
private ApplicationResult result;
private T payload;

public static <T> ApplicationResponse<T> ok(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.OK.value())
.message("API 호출 성공")
.build())
.payload(payload)
.build();
}

public static <T> ApplicationResponse<T> ok(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.OK.value())
.message(message)
.build())
.payload(payload)
.build();
}
// @JsonProperty("isSuccess")
// private final Boolean isSuccess;

public static <T> ApplicationResponse<T> badRequest(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message("잘못된 요청입니다.")
.build())
.payload(payload)
.build();
}
@JsonProperty("statusCode")
@NonNull
private final String statusCode;

public static <T> ApplicationResponse<T> badRequest(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message(message)
.build())
.payload(payload)
.build();
}
@JsonProperty("message")
@NonNull
private final String message;

public static <T> ApplicationResponse<T> notAuthenticated(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message("잘못된 접근입니다.")
.build())
.payload(payload)
.build();
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("content")
private T content;

public static <T> ApplicationResponse<T> notAuthenticated(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.UNAUTHORIZED.value())
.message(message)
.build())
.payload(payload)
.build();
// 성공한 경우 응답 생성
public static <T> ApplicationResponse<T> onSuccess(T content) {
// return new ApiResponse<>(true, code.getReasonHttpStatus().getCode(), code.getReasonHttpStatus().getMessage(), result);
return new ApplicationResponse<>(HttpStatus.OK.name(), HttpStatus.OK.getReasonPhrase(), content);
}

public static <T> ApplicationResponse<T> server(T payload) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message("API 호출 실패")
.build())
.payload(payload)
.build();
// 실패한 경우 응답 생성
public static <T> ApplicationResponse<T> onFailure(String statusCode, String message) {
// return new ApiResponse<>(false, code, message, data);
return new ApplicationResponse<>(statusCode, message, null);
}

public static <T> ApplicationResponse<T> server(T payload, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(HttpStatus.INTERNAL_SERVER_ERROR.value())
.message(message)
.build())
.payload(payload)
.build();
public static <T> ApplicationResponse<T> onFailure(String statusCode, String message, T content) {
return new ApplicationResponse<>(statusCode, message, content);
}

public static <T> ApplicationResponse<T> custom(T payload, Integer code, String message) {
return ApplicationResponse.<T>builder()
.result(ApplicationResult.builder()
.code(code)
.message(message)
.build())
.payload(payload)
.build();
// Json serialize
public String toJsonString() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(this);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface BaseErrorCode {

String getMessage();

ApiResponse<Void> getErrorResponse();
ApplicationResponse<Void> getErrorResponse();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public enum GlobalErrorCode implements BaseErrorCode {
private final String message;

@Override
public ApiResponse<Void> getErrorResponse() {
return ApiResponse.onFailure(code, message);
public ApplicationResponse<Void> getErrorResponse() {
return ApplicationResponse.onFailure(code, message);
}

}
Loading

0 comments on commit db719aa

Please sign in to comment.