Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/9-getRecipe] 레시피글 상세 조회 API #53

Merged
merged 2 commits into from
Feb 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ public enum BaseResponseStatus {
INVALID_PASSWORD(false, 2003, "비밀번호가 잘못되었습니다."),
INVALID_LOGIN_ID(false, 2004, "잘못된 아이디입니다."),
INVALID_ACCESS_TOKEN(false, 2005, "잘못된 AccessToken 입니다."),
INVALID_REFRESH_TOKEN(false, 2006, "잘못된 RefreshToken 입니다."),
NULL_ACCESS_TOKEN(false, 2007, "AccessToken을 입력해주세요."),
INVALID_REFRESH_TOKEN(false, 2006, "잘못된 Refresh Token 입니다."),
NULL_ACCESS_TOKEN(false, 2007, "Access Token을 입력해주세요."),

// produce(2100-2199)
INVALID_PRODUCE_IDX(false, 2100, "잘못된 produce idx 입니다."),
INVALID_PRODUCE_IDX(false, 2100, "잘못된 판매글 idx 입니다."),
TITLE_EXCEEDED_MAX_LIMIT(false, 2101, "제목은 32자 이하여야 합니다."),
NO_PRODUCE_WRITER(false, 2102, "해당 판매글의 작성자가 아닙니다."),
ALREADY_DELETED_PRODUCE(false, 2103, "이미 삭제된 판매글입니다."),

// recipe(2200-2299)
INVALID_RECIPE_IDX(false, 2200, "잘못된 레시피글 idx 입니다."),

// ingredient(2300-2399)

// giveaway(2400-2499)
INVALID_GIVEAWAY_IDX(false, 2400, "잘못된 giveaway idx 입니다."),
INVALID_GIVEAWAY_IDX(false, 2400, "잘못된 나눔글 idx 입니다."),
NO_GIVEAWAY_WRITER(false, 2401, "해당 나눔글의 작성자가 아닙니다."),
ALREADY_DELETED_GIVEAWAY(false, 2402, "이미 삭제된 나눔글입니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
@Getter
public enum IngredientType {
INGREDIENT,
SOURCE
SAUCE
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public BaseResponse<?> getGiveawayList() {

// 나눔글 상세 조회
@GetMapping("/{giveawayIdx}")
public BaseResponse<?> getGiveawayList(@PathVariable Long giveawayIdx) {
public BaseResponse<?> getGiveaway(@PathVariable Long giveawayIdx) {
try {
return new BaseResponse<>(giveawayService.getGiveawayPost(giveawayIdx));
} catch (BaseException e) {
Expand Down Expand Up @@ -65,7 +65,7 @@ public BaseResponse<?> changeGiveawayStatus(@PathVariable Long giveawayIdx) {

// [작성자] 나눔글 삭제
@PatchMapping("")
public BaseResponse<?> deleteProduce(@RequestParam Long giveawayIdx) {
public BaseResponse<?> deleteGiveaway(@RequestParam Long giveawayIdx) {
try {
giveawayService.deleteGiveaway(giveawayIdx);
return new BaseResponse<>(SUCCESS);
Expand Down
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
Expand Up @@ -33,7 +33,7 @@ public BaseResponse<?> getProduceList() {

// 판매글 상세 조회
@GetMapping("/{produceIdx}")
public BaseResponse<?> getProduceList(@PathVariable Long produceIdx) {
public BaseResponse<?> getProduce(@PathVariable Long produceIdx) {
try {
return new BaseResponse<>(produceService.getProducePost(produceIdx));
} catch (BaseException e) {
Expand Down
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);
}