diff --git a/src/main/java/com/diareat/diareat/food/controller/FoodController.java b/src/main/java/com/diareat/diareat/food/controller/FoodController.java index 50863a2..bc13c50 100644 --- a/src/main/java/com/diareat/diareat/food/controller/FoodController.java +++ b/src/main/java/com/diareat/diareat/food/controller/FoodController.java @@ -140,4 +140,10 @@ public ApiResponse getAnalysisOfUser(@PathVariable Long use public ApiResponse> getUserRankByWeek(@PathVariable Long userId){ return ApiResponse.success(foodService.getUserRankByWeek(userId),ResponseCode.FOOD_RANK_READ_SUCCESS.getMessage()); } + + @Operation(summary = "[음식] 즐겨찾기 음식으로 음식 생성",description = "즐겨찾기 음식으로 음식을 생성합니다.") + @PostMapping("/favorite/createfrom") + public ApiResponse createFoodFromFavoriteFood(@RequestBody @Valid CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto){ + return ApiResponse.success(foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto),ResponseCode.FOOD_CREATE_SUCCESS.getMessage()); + } } diff --git a/src/main/java/com/diareat/diareat/food/dto/CreateFoodFromFavoriteFoodDto.java b/src/main/java/com/diareat/diareat/food/dto/CreateFoodFromFavoriteFoodDto.java new file mode 100644 index 0000000..28011b9 --- /dev/null +++ b/src/main/java/com/diareat/diareat/food/dto/CreateFoodFromFavoriteFoodDto.java @@ -0,0 +1,23 @@ +package com.diareat.diareat.food.dto; + +import com.diareat.diareat.util.MessageUtil; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotNull; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class CreateFoodFromFavoriteFoodDto { + @NotNull(message = MessageUtil.NOT_NULL) + private Long userId; + + @NotNull(message = MessageUtil.NOT_NULL) + private Long favoriteFoodId; + + public static CreateFoodFromFavoriteFoodDto of(Long userId, Long favoriteFoodId) { + return new CreateFoodFromFavoriteFoodDto(userId, favoriteFoodId); + } +} 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 9addcce..8987fee 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -319,9 +319,11 @@ public List getUserRankByWeek(Long userId) { } @Transactional() - public Long createFoodFromFavoriteFood(Long favoriteFoodId) { - FavoriteFood favoriteFood = getFavoriteFoodById(favoriteFoodId); + public Long createFoodFromFavoriteFood(CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto) { + validateFavoriteFood(createFoodFromFavoriteFoodDto.getFavoriteFoodId(), createFoodFromFavoriteFoodDto.getUserId()); + FavoriteFood favoriteFood = getFavoriteFoodById(createFoodFromFavoriteFoodDto.getFavoriteFoodId()); Food food = FavoriteFood.createFoodFromFavoriteFood(favoriteFood); + food.setFavoriteFood(favoriteFood); return foodRepository.save(food).getId(); } diff --git a/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java b/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java index 270d35c..1110177 100644 --- a/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java +++ b/src/test/java/com/diareat/diareat/controller/FoodControllerTest.java @@ -466,4 +466,29 @@ void testGetUserRankByWeek() throws Exception{ .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].proteinScore").value(expectedResponse.getData().get(0).getProteinScore())) .andExpect(MockMvcResultMatchers.jsonPath("$.data[0].fatScore").value(expectedResponse.getData().get(0).getFatScore())); } + + @DisplayName("즐겨찾기 음식으로 음식 생성") + @Test + @WithMockUser("test") + void testCreateFoodFromFavoriteFood() throws Exception{ + //Given + Long testNewFoodId = 1L; + CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto = CreateFoodFromFavoriteFoodDto.of(testUserId, testFavoriteFoodId); + when(foodService.createFoodFromFavoriteFood(any(CreateFoodFromFavoriteFoodDto.class))).thenReturn(testNewFoodId); + ApiResponse expectedResponse = ApiResponse.success(foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto), ResponseCode.FOOD_CREATE_SUCCESS.getMessage()); + String json = mapper.writeValueAsString(createFoodFromFavoriteFoodDto); + + + //When + mockMvc.perform(MockMvcRequestBuilders + .post("/api/food/favorite/createfrom") + .contentType(MediaType.APPLICATION_JSON) + .content(json) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode())) + .andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage())) + .andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedResponse.getData())) + .andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg())); + } } diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index df16cdf..28741e3 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -493,4 +493,31 @@ void testGetAnalysisOfUser(){ verify(foodRepository, times(1)).findAllByUserIdAndDateBetween(user.getId(), LocalDate.now().with(DayOfWeek.MONDAY), LocalDate.now(),sort); } + + @Test + void testCreateFoodFromFavoriteFood() { + // given + User user = User.createUser("testUser", "testImage","testPassword", 1, 180, 80, 18, BaseNutrition.createNutrition(2000,400,100,50)); + Food food = Food.createFood( "Food", user, BaseNutrition.createNutrition(100, 100 ,10, 1), 2010,1,1); + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("FavoriteFood", user, food, BaseNutrition.createNutrition(100, 100 ,10, 1)); + Food newFood = Food.createFood("FoodFromFavorite", user, BaseNutrition.createNutrition(100, 100 ,10, 1), 2010,1,1); + user.setId(1L); + food.setId(2L); + favoriteFood.setId(3L); + newFood.setId(4L); + CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto = CreateFoodFromFavoriteFoodDto.of(user.getId(), favoriteFood.getId()); + + + given(favoriteFoodRepository.existsById(favoriteFood.getId())).willReturn(true); + given(favoriteFoodRepository.existsByIdAndUserId(favoriteFood.getId(),user.getId())).willReturn(true); + given(favoriteFoodRepository.findById(favoriteFood.getId())).willReturn(Optional.of(favoriteFood)); + given(foodRepository.save(any(Food.class))).willReturn(newFood); + + // when + Long foodId = foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto); + + // then + assertEquals(4L, foodId); + verify(foodRepository, times(1)).save(any(Food.class)); + } }