Skip to content

Commit

Permalink
Merge pull request #119 from CAUSOLDOUTMEN/feature/114-fix-favorite
Browse files Browse the repository at this point in the history
Fix: 즐찾 해제 시 자식 음식객체 연쇄삭제 명시적으로 추가 (#114)
  • Loading branch information
synoti21 authored Nov 22, 2023
2 parents 92dd0a8 + 005c18e commit 258b335
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public void updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto) {
@Transactional
public void deleteFavoriteFood(Long favoriteFoodId, Long userId) {
validateFavoriteFood(favoriteFoodId, userId);
FavoriteFood favoriteFood = getFavoriteFoodById(favoriteFoodId);
favoriteFood.getFoods().forEach(food -> food.setFavoriteFood(null)); // 즐겨찾기 음식으로부터 태어난 음식들의 즐겨찾기 정보를 null로 초기화
favoriteFoodRepository.deleteById(favoriteFoodId);
log.info("즐겨찾기 음식 해제 완료: " + favoriteFoodId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ void testDeleteFavoriteFood() {
User user = User.createUser("testUser", "testImage","tessPassword", 1, 180, 80, 18, testBaseNutrition);
Food food = Food.createFood("testFood", user, testBaseNutrition, 2010,1,1);
FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("testFood", user, food, testBaseNutrition);
food.setFavoriteFood(favoriteFood);
favoriteFood.setId(1L);

given(favoriteFoodRepository.existsById(favoriteFood.getId())).willReturn(true);
given(favoriteFoodRepository.existsByIdAndUserId(favoriteFood.getId(), 1L)).willReturn(true);
given(favoriteFoodRepository.findById(favoriteFood.getId())).willReturn(Optional.of(favoriteFood));

//when
foodService.deleteFavoriteFood(favoriteFood.getId(), 1L);
Expand Down

0 comments on commit 258b335

Please sign in to comment.