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 5, 2024
1 parent 8ad1f54 commit 2efad3a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kkobugi.puremarket.ingredient.repository;

import com.kkobugi.puremarket.common.enums.IngredientType;
import com.kkobugi.puremarket.ingredient.domain.entity.Ingredient;
import com.kkobugi.puremarket.recipe.domain.entity.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface IngredientRepository extends JpaRepository<Ingredient, Long> {
List<Ingredient> findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(Recipe recipe, IngredientType ingredientType, String status);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.kkobugi.puremarket.recipe.application;

import com.kkobugi.puremarket.common.BaseException;
import com.kkobugi.puremarket.ingredient.repository.IngredientRepository;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeListResponse;
import com.kkobugi.puremarket.recipe.domain.dto.RecipeResponse;
import com.kkobugi.puremarket.recipe.domain.entity.Recipe;
import com.kkobugi.puremarket.recipe.repository.RecipeDescriptionRepository;
import com.kkobugi.puremarket.recipe.repository.RecipeRepository;
import com.kkobugi.puremarket.user.application.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.kkobugi.puremarket.common.constants.Constant.ACTIVE;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.DATABASE_ERROR;
import static com.kkobugi.puremarket.common.enums.BaseResponseStatus.*;
import static com.kkobugi.puremarket.common.enums.IngredientType.INGREDIENT;
import static com.kkobugi.puremarket.common.enums.IngredientType.SAUCE;

@Service
@RequiredArgsConstructor
public class RecipeService {
private final RecipeRepository recipeRepository;
private final AuthService authService;
private final IngredientRepository ingredientRepository;
private final RecipeDescriptionRepository recipeDescriptionRepository;

// 레시피글 목록 조회
public RecipeListResponse getRecipeList() throws BaseException {
Expand All @@ -34,4 +44,43 @@ public RecipeListResponse getRecipeList() throws BaseException {
throw new BaseException(DATABASE_ERROR);
}
}

// 레시피글 상세 조회
public RecipeResponse getRecipe(Long recipeIdx) throws BaseException {
try {
Long userIdx = authService.getUserIdxFromToken();
Recipe recipe = recipeRepository.findById(recipeIdx).orElseThrow(() -> new BaseException(INVALID_RECIPE_IDX));

boolean isWriter = false;
if (userIdx != null && recipe.getUser() != null) {
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();

return new RecipeResponse(recipe.getRecipeIdx(), recipe.getTitle(), recipe.getContent(), recipe.getRecipeImage(),
ingredientList, sauceList, recipeDescriptionList,
recipe.getUser().getNickname(), recipe.getUser().getContact(), recipe.getUser().getProfileImage(), isWriter);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kkobugi.puremarket.recipe.domain.dto;

import java.util.List;

public record RecipeResponse(Long recipeIdx,
String title,
String content,
String recipeImage,
List<IngredientDto> ingredientList,
List<SauceDto> sauceList,
List<RecipeDescriptionDto> recipeDescriptionList,
String nickname,
String contact,
String profileImage,
boolean isWriter) {
public record IngredientDto(String name, String quantity) {}
public record SauceDto(String name, String quantity) {}
public record RecipeDescriptionDto(Integer orderNumber, String description) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DynamicInsert
public class RecipeDescription extends BaseEntity { // 레시피 상세
public class RecipeDescription extends BaseEntity { // 레시피 상세(조리 순서)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long recipeDescriptionIdx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -30,4 +31,14 @@ public BaseResponse<?> getRecipeList() {
return new BaseResponse<>(e.getStatus());
}
}

// 레시피글 상세 조회
@GetMapping("/{recipeIdx}")
public BaseResponse<?> getRecipe(@PathVariable Long recipeIdx) {
try {
return new BaseResponse<>(recipeService.getRecipe(recipeIdx));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kkobugi.puremarket.recipe.repository;

import com.kkobugi.puremarket.recipe.domain.entity.Recipe;
import com.kkobugi.puremarket.recipe.domain.entity.RecipeDescription;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface RecipeDescriptionRepository extends JpaRepository<RecipeDescription, Long> {
List<RecipeDescription> findByRecipeAndStatusEqualsOrderByCreatedDateDesc(Recipe recipe, String status);
}

0 comments on commit 2efad3a

Please sign in to comment.