Skip to content

Commit

Permalink
Fix: 레시피 상세 북마크 반환 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongho427 committed Jun 11, 2024
1 parent 10abe09 commit d1f1926
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package capstone.recipable.domain.bookmark.repository;

import capstone.recipable.domain.bookmark.entity.Bookmark;
import capstone.recipable.domain.bookmark.repository.custom.BookmarkRepositoryCustom;
import capstone.recipable.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
public interface BookmarkRepository extends JpaRepository<Bookmark, Long>, BookmarkRepositoryCustom {
List<Bookmark> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package capstone.recipable.domain.bookmark.repository.custom;

import capstone.recipable.domain.recipe.entity.Recipe;
import capstone.recipable.domain.user.entity.User;

public interface BookmarkRepositoryCustom {
boolean isBookmarked(User user, Recipe recipe);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package capstone.recipable.domain.bookmark.repository.custom;

import capstone.recipable.domain.recipe.entity.Recipe;
import capstone.recipable.domain.user.entity.User;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import static capstone.recipable.domain.bookmark.entity.QBookmark.bookmark;

@RequiredArgsConstructor
public class BookmarkRepositoryImpl implements BookmarkRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public boolean isBookmarked(User user, Recipe recipe) {
return jpaQueryFactory
.selectFrom(bookmark)
.where(bookmark.user.eq(user).and(bookmark.recipe.eq(recipe)))
.fetchOne() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import capstone.recipable.domain.ingredient.service.NaverSearchImageService;
import capstone.recipable.domain.recipe.dto.request.CreateRecipeRequest;
import capstone.recipable.domain.recipe.dto.response.CreateRecipeResponse;
import capstone.recipable.domain.recipe.dto.response.ImageResponse;
import capstone.recipable.domain.recipe.dto.response.RecipeDetailsResponse;
import capstone.recipable.domain.recipe.dto.response.RecipeVideoResponse;
Expand Down Expand Up @@ -49,8 +50,8 @@ public ResponseEntity<SuccessResponse<ImageResponse>> getImage(@RequestParam Str
저장한 레시피와 레시피 관련 영상을 반환해줍니다.
""")
@PostMapping
public ResponseEntity<SuccessResponse<RecipeDetailsResponse>> createRecipe(@RequestBody CreateRecipeRequest request) throws IOException {
RecipeDetailsResponse response = recipeService.createRecipe(request);
public ResponseEntity<SuccessResponse<CreateRecipeResponse>> createRecipe(@RequestBody CreateRecipeRequest request) throws IOException {
CreateRecipeResponse response = recipeService.createRecipe(request);
return SuccessResponse.of(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package capstone.recipable.domain.recipe.dto.response;

import capstone.recipable.domain.bookmark.repository.BookmarkRepository;
import capstone.recipable.domain.recipe.entity.Recipe;
import lombok.*;

import java.util.List;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@Builder
public class CreateRecipeResponse {
private Long recipeId;

private String recipeName;

private String introduce;

private String recipeImg;

private String ingredients;

private String recipeDetails;

private List<RecipeVideoResponse> recipeVideoResponses;

private boolean bookmark = false;

public static CreateRecipeResponse of(Recipe recipe) {
List<RecipeVideoResponse> responses = recipe.getRecipeVideos().stream()
.map(recipeVideos -> RecipeVideoResponse.of(recipeVideos.getVideoUrl(),
recipeVideos.getTitle(),
recipeVideos.getThumbnail()))
.toList();

return CreateRecipeResponse.builder()
.recipeId(recipe.getId())
.recipeName(recipe.getRecipeName())
.introduce(recipe.getIntroduce())
.recipeImg(recipe.getRecipeImg())
.ingredients(recipe.getIngredients())
.recipeDetails(recipe.getRecipeDetails())
.recipeVideoResponses(responses)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package capstone.recipable.domain.recipe.dto.response;

import capstone.recipable.domain.bookmark.repository.BookmarkRepository;
import capstone.recipable.domain.recipe.entity.Recipe;
import lombok.*;

Expand All @@ -25,7 +26,9 @@ public class RecipeDetailsResponse {

private List<RecipeVideoResponse> recipeVideoResponses;

public static RecipeDetailsResponse of(Recipe recipe) {
private boolean bookmark;

public static RecipeDetailsResponse of(Recipe recipe, boolean isMarked) {
List<RecipeVideoResponse> responses = recipe.getRecipeVideos().stream()
.map(recipeVideos -> RecipeVideoResponse.of(recipeVideos.getVideoUrl(),
recipeVideos.getTitle(),
Expand All @@ -40,6 +43,7 @@ public static RecipeDetailsResponse of(Recipe recipe) {
.ingredients(recipe.getIngredients())
.recipeDetails(recipe.getRecipeDetails())
.recipeVideoResponses(responses)
.bookmark(isMarked)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package capstone.recipable.domain.recipe.service;

import capstone.recipable.domain.auth.jwt.SecurityContextProvider;
import capstone.recipable.domain.bookmark.repository.BookmarkRepository;
import capstone.recipable.domain.ingredient.service.NaverSearchImageService;
import capstone.recipable.domain.recipe.dto.request.CreateRecipeRequest;
import capstone.recipable.domain.recipe.dto.response.CreateRecipeResponse;
import capstone.recipable.domain.recipe.dto.response.RecipeDetailsResponse;
import capstone.recipable.domain.recipe.dto.response.RecipeVideoResponse;
import capstone.recipable.domain.recipe.entity.Recipe;
Expand Down Expand Up @@ -30,18 +32,25 @@ public class RecipeService {
private final RecipeVideosRepository recipeVideosRepository;
private final YoutubeService youtubeService;
private final UserRepository userRepository;
private final BookmarkRepository bookmarkRepository;

//레시피 상세 조회
public RecipeDetailsResponse getRecipeDetails(Long recipeId) {
Long userId = SecurityContextProvider.getAuthenticatedUserId();
User user = userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(ErrorCode.USER_NOT_FOUND));

Recipe recipe = recipeRepository.findById(recipeId)
.orElseThrow(() -> new ApplicationException(ErrorCode.RECIPE_NOT_FOUND));

return RecipeDetailsResponse.of(recipe);
boolean isMarked = bookmarkRepository.isBookmarked(user, recipe);

return RecipeDetailsResponse.of(recipe, isMarked);
}

//레시피 생성
@Transactional
public RecipeDetailsResponse createRecipe(CreateRecipeRequest request) throws IOException {
public CreateRecipeResponse createRecipe(CreateRecipeRequest request) throws IOException {
Long userId = SecurityContextProvider.getAuthenticatedUserId();
User user = userRepository.findById(userId)
.orElseThrow(() -> new ApplicationException(ErrorCode.USER_NOT_FOUND));
Expand All @@ -60,6 +69,6 @@ public RecipeDetailsResponse createRecipe(CreateRecipeRequest request) throws IO

recipe.updateVideo(recipeVideos);

return RecipeDetailsResponse.of(recipe);
return CreateRecipeResponse.of(recipe);
}
}

0 comments on commit d1f1926

Please sign in to comment.