diff --git a/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java b/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java index 14138ce..ea5053a 100644 --- a/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java +++ b/src/main/java/com/diareat/diareat/food/domain/FavoriteFood.java @@ -37,9 +37,10 @@ public void addCount(){ } // 생성 메서드 - public static FavoriteFood createFavoriteFood(String name, User user, BaseNutrition baseNutrition) { + public static FavoriteFood createFavoriteFood(String name, User user, Food food, BaseNutrition baseNutrition) { FavoriteFood favoriteFood = new FavoriteFood(); favoriteFood.name = name; + favoriteFood.food = food; favoriteFood.user = user; favoriteFood.baseNutrition = baseNutrition; return favoriteFood; diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index f9e6baa..e6c25cd 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -80,10 +80,14 @@ public void deleteFood(Long foodId, Long userId, LocalDate date) { @CacheEvict(value = "ResponseFavoriteFoodDto", key = "#createFavoriteFoodDto.getUserId()", cacheManager = "diareatCacheManager") @Transactional public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { + validateFood(createFavoriteFoodDto.getFoodId(), createFavoriteFoodDto.getUserId()); User user = getUserById(createFavoriteFoodDto.getUserId()); + Food food = getFoodById(createFavoriteFoodDto.getFoodId()); + if (favoriteFoodRepository.existsByFoodId(createFavoriteFoodDto.getFoodId())) throw new FavoriteException(ResponseCode.FAVORITE_ALREADY_EXIST); - FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood(createFavoriteFoodDto.getName(), user, createFavoriteFoodDto.getBaseNutrition()); + + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood(createFavoriteFoodDto.getName(), user, food, createFavoriteFoodDto.getBaseNutrition()); return favoriteFoodRepository.save(favoriteFood).getId(); } diff --git a/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java b/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java index d33e366..270d35c 100644 --- a/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java +++ b/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java @@ -51,7 +51,7 @@ class FoodControllerTest { private final User testUser = User.createUser("test", "test", "test", 180, 70, 0, 20, BaseNutrition.createNutrition(2000, 300, 80, 80)); private final Food testFood = Food.createFood("testFood", testUser, BaseNutrition.createNutrition(500, 50, 30, 10), 2021, 10, 10); - private final FavoriteFood testFavoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", testUser, BaseNutrition.createNutrition(500, 50, 30, 10)); + private final FavoriteFood testFavoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", testUser, testFood, BaseNutrition.createNutrition(500, 50, 30, 10)); private final BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(500, 50, 30, 10); @BeforeEach diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index febdcec..df16cdf 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -122,23 +122,28 @@ void testDeleteFood() { void testSaveAndGetFavoriteFood() { BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); User user = User.createUser("testUser", "testImage","testPassword", 1,180, 80,18, testBaseNutrition); - FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user, testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition, 2010,1,1); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user,food, testBaseNutrition); user.setId(1L); + food.setId(2L); favoriteFood.setId(3L); - CreateFavoriteFoodDto createFavoriteFoodDto = CreateFavoriteFoodDto.of(favoriteFood.getId(), user.getId(),"testFood", testBaseNutrition); + CreateFavoriteFoodDto createFavoriteFoodDto = CreateFavoriteFoodDto.of(food.getId(), user.getId(),"testFood", testBaseNutrition); given(userRepository.findById(user.getId())).willReturn(Optional.of(user)); given(userRepository.existsById(user.getId())).willReturn(true); + given(foodRepository.existsById(food.getId())).willReturn(true); + given(foodRepository.existsByIdAndUserId(food.getId(), user.getId())).willReturn(true); + given(foodRepository.findById(food.getId())).willReturn(Optional.of(food)); given(favoriteFoodRepository.save(any(FavoriteFood.class))).willReturn(favoriteFood); given(favoriteFoodRepository.findAllByUserId(any(Long.class))).willReturn(List.of(favoriteFood)); //when - Long foodId = foodService.saveFavoriteFood(createFavoriteFoodDto); + Long favoriteFoodId = foodService.saveFavoriteFood(createFavoriteFoodDto); List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(user.getId()); - assertEquals(3L, foodId); + assertEquals(3L, favoriteFoodId); assertEquals("testFavoriteFood",responseFavoriteFoodDtoList.get(0).getName()); verify(favoriteFoodRepository, times(1)).save(any(FavoriteFood.class)); } @@ -147,7 +152,8 @@ void testUpdateFavoriteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); - FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user, testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition, 2010,1,1); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFavoriteFood", user, food,testBaseNutrition); favoriteFood.setId(1L); given(favoriteFoodRepository.findById(favoriteFood.getId())).willReturn(Optional.of(favoriteFood)); @@ -169,7 +175,8 @@ void testDeleteFavoriteFood() { //given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition); - FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFood", user, testBaseNutrition); + Food food = Food.createFood("testFood", user, testBaseNutrition, 2010,1,1); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFood", user, food, testBaseNutrition); favoriteFood.setId(1L); given(favoriteFoodRepository.existsById(favoriteFood.getId())).willReturn(true);