diff --git a/src/main/java/zipdabang/server/converter/RecipeConverter.java b/src/main/java/zipdabang/server/converter/RecipeConverter.java index 46f6e12..648c9d4 100644 --- a/src/main/java/zipdabang/server/converter/RecipeConverter.java +++ b/src/main/java/zipdabang/server/converter/RecipeConverter.java @@ -9,10 +9,7 @@ import zipdabang.server.aws.s3.AmazonS3Manager; import zipdabang.server.domain.etc.Uuid; import zipdabang.server.domain.member.Member; -import zipdabang.server.domain.recipe.Ingredient; -import zipdabang.server.domain.recipe.Recipe; -import zipdabang.server.domain.recipe.RecipeCategoryMapping; -import zipdabang.server.domain.recipe.Step; +import zipdabang.server.domain.recipe.*; import zipdabang.server.repository.CategoryRepository; import zipdabang.server.repository.recipeRepositories.LikesRepository; import zipdabang.server.repository.recipeRepositories.RecipeCategoryMappingRepository; @@ -260,4 +257,18 @@ private static Ingredient toIngredientDto(RecipeRequestDto.NewIngredientDto ingr .recipe(recipe) .build(); } + + public static Likes toLikes(Recipe recipe, Member member) { + return Likes.builder() + .recipe(recipe) + .member(member) + .build(); + } + + public static Scrap toScrap(Recipe recipe, Member member) { + return Scrap.builder() + .recipe(recipe) + .member(member) + .build(); + } } diff --git a/src/main/java/zipdabang/server/domain/recipe/Likes.java b/src/main/java/zipdabang/server/domain/recipe/Likes.java index 562eae2..cc5ca0d 100644 --- a/src/main/java/zipdabang/server/domain/recipe/Likes.java +++ b/src/main/java/zipdabang/server/domain/recipe/Likes.java @@ -28,4 +28,23 @@ public class Likes extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "recipe_id", nullable = false) private Recipe recipe; + + public Likes deleteLikes(Recipe recipe){ + if(this.recipe != null) + recipe.getLikesList().remove(this); + recipe.updateToTalLike(-1); + return this; + } + + public Likes setRecipe(Recipe recipe){ + + recipe.updateToTalLike(1); + + if(this.recipe != null) + recipe.getLikesList().remove(this); + this.recipe = recipe; + recipe.getLikesList().add(this); + + return this; + } } diff --git a/src/main/java/zipdabang/server/domain/recipe/Recipe.java b/src/main/java/zipdabang/server/domain/recipe/Recipe.java index 87f6c2f..a37be97 100644 --- a/src/main/java/zipdabang/server/domain/recipe/Recipe.java +++ b/src/main/java/zipdabang/server/domain/recipe/Recipe.java @@ -90,4 +90,14 @@ public Recipe setThumbnail(String imageUrl) { this.thumbnailUrl = imageUrl; return this; } + + public Recipe updateToTalLike(Integer i){ + this.totalLike += i; + return this; + } + + public Recipe updateToTalScrap(Integer i){ + this.totalScrap += i; + return this; + } } diff --git a/src/main/java/zipdabang/server/domain/recipe/Scrap.java b/src/main/java/zipdabang/server/domain/recipe/Scrap.java index 498d6f1..79e979e 100644 --- a/src/main/java/zipdabang/server/domain/recipe/Scrap.java +++ b/src/main/java/zipdabang/server/domain/recipe/Scrap.java @@ -28,4 +28,23 @@ public class Scrap extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "recipe_id") private Recipe recipe; + + public Scrap deleteScrap(Recipe recipe){ + if(this.recipe != null) + recipe.getScrapList().remove(this); + recipe.updateToTalScrap(-1); + return this; + } + + public Scrap setRecipe(Recipe recipe){ + + recipe.updateToTalScrap(1); + + if(this.recipe != null) + recipe.getScrapList().remove(this); + this.recipe = recipe; + recipe.getScrapList().add(this); + + return this; + } } diff --git a/src/main/java/zipdabang/server/service/RecipeService.java b/src/main/java/zipdabang/server/service/RecipeService.java index 8d3bf93..a85b091 100644 --- a/src/main/java/zipdabang/server/service/RecipeService.java +++ b/src/main/java/zipdabang/server/service/RecipeService.java @@ -24,4 +24,8 @@ public interface RecipeService { Page searchRecipe(String keyword, Integer pageIndex, Member member); List getWrittenByRecipePreview(String writtenby, Member member); + + Recipe updateLikeOnRecipe(Long recipeId, Member member); + + Recipe updateScrapOnRecipe(Long recipeId, Member member); } diff --git a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java index 2e9dd56..036c015 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java @@ -16,7 +16,9 @@ import zipdabang.server.converter.RecipeConverter; import zipdabang.server.domain.member.BlockedMember; import zipdabang.server.domain.member.Member; +import zipdabang.server.domain.recipe.Likes; import zipdabang.server.domain.recipe.Recipe; +import zipdabang.server.domain.recipe.Scrap; import zipdabang.server.repository.memberRepositories.BlockedMemberRepository; import zipdabang.server.repository.recipeRepositories.*; import zipdabang.server.service.RecipeService; @@ -168,4 +170,42 @@ public List getWrittenByRecipePreview(String writtenby, Member member) { return recipeList; } + + @Override + @Transactional(readOnly = false) + public Recipe updateLikeOnRecipe(Long recipeId, Member member) { + Recipe recipe = recipeRepository.findById(recipeId).orElseThrow(() -> new RecipeException(Code.NO_RECIPE_EXIST)); + + Optional likesExist = likesRepository.findByRecipeAndMember(recipe,member); + + if(likesExist.isEmpty()) { + Likes savedLikes = likesRepository.save(RecipeConverter.toLikes(recipe, member)); + savedLikes.setRecipe(recipe); + } + else{ + likesExist.get().deleteLikes(recipe); + likesRepository.deleteById(likesExist.get().getId()); + } + + return recipe; + } + + @Override + @Transactional(readOnly = false) + public Recipe updateScrapOnRecipe(Long recipeId, Member member) { + Recipe recipe = recipeRepository.findById(recipeId).orElseThrow(() -> new RecipeException(Code.NO_RECIPE_EXIST)); + + Optional scrapExist = scrapRepository.findByRecipeAndMember(recipe,member); + + if(scrapExist.isEmpty()) { + Scrap savedScrap = scrapRepository.save(RecipeConverter.toScrap(recipe, member)); + savedScrap.setRecipe(recipe); + } + else { + scrapExist.get().deleteScrap(recipe); + scrapRepository.deleteById(scrapExist.get().getId()); + } + + return recipe; + } } diff --git a/src/main/java/zipdabang/server/web/controller/RecipeController.java b/src/main/java/zipdabang/server/web/controller/RecipeController.java index d9a1fb1..c84f1e3 100644 --- a/src/main/java/zipdabang/server/web/controller/RecipeController.java +++ b/src/main/java/zipdabang/server/web/controller/RecipeController.java @@ -238,7 +238,7 @@ public ResponseDto recipeWeekBest(@AuthMember M return null; } - @Operation(summary = "레시피 스크랩/취소 API 🔑", description = "레시피 스크랩/취소 API입니다.") + @Operation(summary = "레시피 스크랩/취소 API 🔑 ✔", description = "레시피 스크랩/취소 API입니다.") @ApiResponses({ @ApiResponse(responseCode = "2000"), @ApiResponse(responseCode = "4003",description = "UNAUTHORIZED, 토큰 모양이 이상함, 토큰 제대로 주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @@ -253,10 +253,12 @@ public ResponseDto recipeWeekBest(@AuthMember M }) @PostMapping(value = "/members/recipes/{recipeId}/scrap") public ResponseDto recipeScrapOrCancel(@PathVariable Long recipeId, @AuthMember Member member){ - return null; + Recipe recipe = recipeService.updateScrapOnRecipe(recipeId, member); + + return ResponseDto.of(RecipeConverter.toRecipeStatusDto(recipe)); } - @Operation(summary = "레시피 좋아요/취소 API 🔑", description = "레시피 좋아요/취소 API입니다.") + @Operation(summary = "레시피 좋아요/취소 API 🔑 ✔", description = "레시피 좋아요/취소 API입니다.") @ApiResponses({ @ApiResponse(responseCode = "2000"), @ApiResponse(responseCode = "4003",description = "UNAUTHORIZED, 토큰 모양이 이상함, 토큰 제대로 주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @@ -271,7 +273,10 @@ public ResponseDto recipeScrapOrCancel(@PathV }) @PostMapping(value = "/members/recipes/{recipeId}/likes") public ResponseDto recipeLikeOrCancel(@PathVariable Long recipeId, @AuthMember Member member){ - return null; + + Recipe recipe = recipeService.updateLikeOnRecipe(recipeId, member); + + return ResponseDto.of(RecipeConverter.toRecipeStatusDto(recipe)); } @Operation(summary = "레시피 배너 이미지 API 🔑", description = "레시피 화면의 배너 이미지를 가져옵니다. order는 배너 순서를 의미합니다.")