Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Food와 FavoriteFood 연결 수정 #87

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions src/test/java/com/diareat/diareat/service/FoodServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseFavoriteFoodDto> 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));
}
Expand All @@ -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));
Expand All @@ -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);
Expand Down