Skip to content

Commit

Permalink
#9 feat: 레시피 수정 화면 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Feb 25, 2024
1 parent c45d353 commit 65b276e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.kkobugi.puremarket.common.gcs.GCSService;
import com.kkobugi.puremarket.ingredient.domain.entity.Ingredient;
import com.kkobugi.puremarket.ingredient.repository.IngredientRepository;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeListResponse;
import com.kkobugi.puremarket.recipe.domain.dto.RecipePostRequest;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeResponse;
import com.kkobugi.puremarket.recipe.domain.dto.*;
import com.kkobugi.puremarket.recipe.domain.entity.Recipe;
import com.kkobugi.puremarket.recipe.domain.entity.RecipeDescription;
import com.kkobugi.puremarket.recipe.repository.RecipeDescriptionRepository;
Expand Down Expand Up @@ -69,23 +67,9 @@ public RecipeResponse getRecipe(Long recipeIdx) throws BaseException {
isWriter = userIdx.equals(recipe.getUser().getUserIdx());
}

// 재료 리스트
List<RecipeResponse.IngredientDto> ingredientList = ingredientRepository.findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(recipe, INGREDIENT, ACTIVE).stream()
.map(ingredient -> new RecipeResponse.IngredientDto(
ingredient.getName(),
ingredient.getQuantity())).toList();

// 양념 리스트
List<RecipeResponse.SauceDto> sauceList = ingredientRepository.findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(recipe, SAUCE, ACTIVE).stream()
.map(sauce -> new RecipeResponse.SauceDto(
sauce.getName(),
sauce.getQuantity())).toList();

// 레시피 상세 리스트(조리 순서)
List<RecipeResponse.RecipeDescriptionDto> recipeDescriptionList = recipeDescriptionRepository.findByRecipeAndStatusEqualsOrderByCreatedDateDesc(recipe, ACTIVE).stream()
.map(description -> new RecipeResponse.RecipeDescriptionDto(
description.getOrderNumber(),
description.getDescription())).toList();
List<IngredientDto> ingredientList = getIngredientList(recipe);
List<SauceDto> sauceList = getSauceList(recipe);
List<RecipeDescriptionDto> recipeDescriptionList = getRecipeDescriptionList(recipe);

return new RecipeResponse(recipe.getRecipeIdx(), recipe.getTitle(), recipe.getContent(), recipe.getRecipeImage(),
ingredientList, sauceList, recipeDescriptionList,
Expand Down Expand Up @@ -165,6 +149,50 @@ public void deleteRecipe(Long recipeIdx) throws BaseException {
}
}

// [작성자] 레시피글 수정 화면 조회
public RecipeEditViewResponse getRecipeEditView(Long recipeIdx) throws BaseException {
try {
Recipe recipe = recipeRepository.findById(recipeIdx).orElseThrow(() -> new BaseException(INVALID_RECIPE_IDX));
if (recipe.getStatus().equals(INACTIVE)) throw new BaseException(ALREADY_DELETED_GIVEAWAY);

User user = userRepository.findByUserIdx(getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
validateWriter(user, recipe);

List<IngredientDto> ingredientList = getIngredientList(recipe);
List<SauceDto> sauceList = getSauceList(recipe);
List<RecipeDescriptionDto> recipeDescriptionList = getRecipeDescriptionList(recipe);

return new RecipeEditViewResponse(recipe.getTitle(), recipe.getContent(), recipe.getRecipeImage(),
ingredientList, sauceList, recipeDescriptionList,
recipe.getUser().getNickname(), recipe.getUser().getContact(), recipe.getUser().getProfileImage());
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

private List<RecipeDescriptionDto> getRecipeDescriptionList(Recipe recipe) {
return recipeDescriptionRepository.findByRecipeAndStatusEqualsOrderByCreatedDateDesc(recipe, ACTIVE).stream()
.map(description -> new RecipeDescriptionDto(
description.getOrderNumber(),
description.getDescription())).toList();
}

private List<SauceDto> getSauceList(Recipe recipe) {
return ingredientRepository.findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(recipe, SAUCE, ACTIVE).stream()
.map(sauce -> new SauceDto(
sauce.getName(),
sauce.getQuantity())).toList();
}

private List<IngredientDto> getIngredientList(Recipe recipe) {
return ingredientRepository.findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(recipe, INGREDIENT, ACTIVE).stream()
.map(ingredient -> new IngredientDto(
ingredient.getName(),
ingredient.getQuantity())).toList();
}

private static void validateWriter(User user, Recipe recipe) throws BaseException {
if (!recipe.getUser().equals(user)) throw new BaseException(NO_RECIPE_WRITER);
if (recipe.getStatus().equals(INACTIVE)) throw new BaseException(ALREADY_DELETED_RECIPE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kkobugi.puremarket.recipe.domain.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

@Schema(description = "레시피글 수정 화면 응답")
public record RecipeEditViewResponse(@Schema(description = "글 제목", example = "레시피글")
String title,
@Schema(description = "글 내용", example = "레시피글 내용")
String content,
@Schema(description = "글 이미지 url", example = "https://dwffwdfdwbwv")
String recipeImage,
@Schema(description = "재료 리스트")
List<IngredientDto> ingredientList,
@Schema(description = "양념 리스트")
List<SauceDto> sauceList,
@Schema(description = "조리 순서 리스트")
List<RecipeDescriptionDto> recipeDescriptionList,
@Schema(description = "닉네임", example = "꼬부기")
String nickname,
@Schema(description = "연락처", example = "010112345678")
String contact,
@Schema(description = "프로필 이미지 url", example = "https://dwffwdfdwbwv")
String profileImage) {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public record RecipePostRequest(
@Schema(description = "글 내용", example = "레시피글 내용")
String content,
@Schema(description = "재료 리스트")
List<RecipeResponse.IngredientDto> ingredientList,
List<IngredientDto> ingredientList,
@Schema(description = "양념 리스트")
List<RecipeResponse.SauceDto> sauceList,
List<SauceDto> sauceList,
@Schema(description = "조리 순서 리스트")
List<RecipeResponse.RecipeDescriptionDto> recipeDescriptionList) {
List<RecipeDescriptionDto> recipeDescriptionList) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,4 @@ public record RecipeResponse(
@Schema(description = "프로필 이미지 url", example = "https://dwffwdfdwbwv")
String profileImage,
@Schema(description = "글 작성자 여부", example = "true")
boolean isWriter) {
public record IngredientDto(
@Schema(description = "재료명", example = "토마토")
String name,
@Schema(description = "재료 양", example = "한 알")
String quantity) {}
public record SauceDto(
@Schema(description = "양념명", example = "소금")
String name,
@Schema(description = "양념 양", example = "한 꼬집")
String quantity) {}
public record RecipeDescriptionDto(
@Schema(description = "조리 순서 번호", example = "1")
Integer orderNumber,
@Schema(description = "상세 설명", example = "재료를 함께 볶아준다.")
String description) {}
}
boolean isWriter) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.kkobugi.puremarket.common.BaseException;
import com.kkobugi.puremarket.common.BaseResponse;
import com.kkobugi.puremarket.recipe.application.RecipeService;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeEditViewResponse;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeListResponse;
import com.kkobugi.puremarket.recipe.domain.dto.RecipePostRequest;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeResponse;
Expand Down Expand Up @@ -91,4 +92,15 @@ public BaseResponse<?> deleteRecipe(@Parameter(description = "레시피글 Idx",
return new BaseResponse<>(e.getStatus());
}
}

// [작성자] 레시피글 수정 화면 조회
@GetMapping("/editView/{recipeIdx}")
@Operation(summary = "레시피글 수정화면 조회", description = "해당 레시피글의 원데이터를 조회한다.")
public BaseResponse<RecipeEditViewResponse> getRecipeEditView(@Parameter(description = "레시피글 Idx", in = ParameterIn.PATH) @PathVariable Long recipeIdx) {
try {
return new BaseResponse<>(recipeService.getRecipeEditView(recipeIdx));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}

0 comments on commit 65b276e

Please sign in to comment.