Skip to content

Commit

Permalink
feat : 추천 레시피 카테고리를 변경하는 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Due-IT committed Oct 2, 2024
1 parent a189c4f commit b1b4e06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.sundaegukbap.banchango.recipe.domain.Recipe;
import com.sundaegukbap.banchango.recipe.domain.RecipeCategory;
import com.sundaegukbap.banchango.recipe.domain.UserRecommendedRecipe;
import com.sundaegukbap.banchango.recipe.dto.event.RecommendedRecipeCategoryChangedEvent;
import com.sundaegukbap.banchango.recipe.repository.RecipeRepository;
import com.sundaegukbap.banchango.recipe.repository.RecommendedRecipeRepository;
import com.sundaegukbap.banchango.user.domain.User;
Expand All @@ -36,16 +35,15 @@ public class RecipeService {

@EventListener
public void refreshRecommendedRecipes(IngredientChangedEvent event) {
refreshRecommendedRecipes(event.userId());
refreshRecommendedRecipes(event.userId(), RecipeCategory.전체);
}

@EventListener
public void refreshRecommendedRecipes(RecommendedRecipeCategoryChangedEvent event) {
refreshRecommendedRecipes(event.userId());
public void changeRecipeCategory(Long userId, RecipeCategory recipeCategory) {
refreshRecommendedRecipes(userId, recipeCategory);
}

@Transactional
public void refreshRecommendedRecipes(Long userId) {
public void refreshRecommendedRecipes(Long userId, RecipeCategory recipeCategory) {
List<Container> containers = containerRepository.findAllByUserId(userId);
List<ContainerIngredient> containerIngredients = containerIngredientRepository.findByContainerIn(containers);
List<Ingredient> ingredients = containerIngredients.stream()
Expand All @@ -56,7 +54,7 @@ public void refreshRecommendedRecipes(Long userId) {

User user = userRepository.findById(userId)
.orElseThrow(() -> new NoSuchElementException("no user"));
List<Long> recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(RecipeCategory.전체, ingredients);
List<Long> recommendedRecipeIds = aiRecipeRecommendClient.getRecommendedRecipesFromAI(recipeCategory, ingredients);
List<Recipe> recipes = recipeRepository.findAllById(recommendedRecipeIds);
List<UserRecommendedRecipe> recommendedRecipes = recipes.stream()
.map(recipe -> UserRecommendedRecipe.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.sundaegukbap.banchango.recipe.dto.event;

import com.sundaegukbap.banchango.recipe.domain.RecipeCategory;

public record RecommendedRecipeCategoryChangedEvent(
Long userId
Long userId,
RecipeCategory recipeCategory
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sundaegukbap.banchango.recipe.presentation;

import com.sundaegukbap.banchango.recipe.application.RecipeQueryService;
import com.sundaegukbap.banchango.recipe.application.RecipeService;
import com.sundaegukbap.banchango.recipe.domain.RecipeCategory;
import com.sundaegukbap.banchango.recipe.dto.response.RecommendedRecipeResponse;
import com.sundaegukbap.banchango.recipe.dto.response.RecommendedRecipeResponses;
Expand All @@ -11,6 +12,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -21,25 +23,29 @@
@Tag(name = "레시피 조회 관련 컨트롤러")
public class RecipeController {
private final RecipeQueryService recipeQueryService;
private final RecipeService recipeService;

@GetMapping("/recommend/{userId}")
@Operation(summary = "추천 레시피 목록 조회",
description = "추천 레시피 목록을 조회합니다.\n" +
"카테고리 종류(전체)\n" +
"* 현재 카테고리는 개발중에 있습니다.")
@Operation(summary = "추천 레시피 목록 조회", description = "추천 레시피 목록을 조회합니다.")
public ResponseEntity<RecommendedRecipeResponses> getRecommendedRecipes(@PathVariable("userId") Long userId,
@RequestParam(defaultValue = "0") int pageIndex,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam(defaultValue = "전체") RecipeCategory category
) {
@RequestParam(defaultValue = "10") int pageSize) {
RecommendedRecipeResponses response = recipeQueryService.getRecommendedRecipes(pageIndex, pageSize, userId);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("/{userId}/{recipeId}")
@Operation(summary = "특정 레시피 상세 조회", description = "레시피를 조회한다.")
public ResponseEntity<RecommendedRecipeResponse> getRecipeDetail(@PathVariable("userId") Long userId, @PathVariable("recipeId") Long recipeId) {
RecommendedRecipeResponse response = recipeQueryService.getRecipeDetail(userId,recipeId);
RecommendedRecipeResponse response = recipeQueryService.getRecipeDetail(userId, recipeId);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PostMapping("/{userId}")
@Operation(summary = "추천 레시피 카테고리 변경", description = "추천 레시피 카테고리를 변경하고 새로운 추천 레시피를 받아옵니다.")
public ResponseEntity<String> changeRecipeCategory(@PathVariable("userId") Long userId,
@RequestParam(defaultValue = "전체") RecipeCategory recipeCategory) {
recipeService.changeRecipeCategory(userId, recipeCategory);
return new ResponseEntity<>("카테고리에 맞게 추천 레시피목록이 변경되었습니다.", HttpStatus.OK);
}
}

0 comments on commit b1b4e06

Please sign in to comment.