Skip to content

Commit 7b60189

Browse files
authored
Merge pull request #120 from realBULK/fix/119
[FIX] cascade로 인한 삭제 버그 수정
2 parents 183f9e3 + 34b41d6 commit 7b60189

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

src/main/java/umc7th/bulk/meal/service/command/MealCommandServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import umc7th.bulk.meal.entity.Meal;
1010
import umc7th.bulk.meal.repository.MealRepository;
1111
import umc7th.bulk.mealItem.entity.MealItem;
12+
import umc7th.bulk.mealItem.service.command.MealItemCommandService;
1213
import umc7th.bulk.mealMealItemMapping.entity.MealMealItemMapping;
1314
import umc7th.bulk.mealMealItemMapping.service.command.MealMealItemMappingCommandService;
1415
import umc7th.bulk.mealPlan.dto.MealPlanRequestDTO;
@@ -22,6 +23,7 @@ public class MealCommandServiceImpl implements MealCommandService {
2223
private final MealMealItemMappingCommandService mealMealItemMappingCommandService;
2324
private final MealRepository mealRepository;
2425
private final DailyMealRepository dailyMealRepository;
26+
private final MealItemCommandService mealItemCommandService;
2527

2628
@Override
2729
@Transactional
@@ -34,8 +36,8 @@ public Meal createMeal(DailyMeal dailyMeal, MealPlanRequestDTO.MealDTO dto) {
3436
List<MealMealItemMapping> mappings = mealItems.stream()
3537
.map(mealItemDTO -> {
3638
// mealItem 저장
37-
// MealItem mealItem = mealItemCommandService.createMealItem(mealItemDTO);
38-
MealItem mealItemEntity = mealItemDTO.toMealItemEntity(mealItemDTO);
39+
MealItem mealItemEntity = mealItemCommandService.createMealItem(mealItemDTO);
40+
// MealItem mealItemEntity = mealItemDTO.toMealItemEntity(mealItemDTO);
3941
return mealMealItemMappingCommandService.createMapping(meal, mealItemEntity);
4042
})
4143
.toList();

src/main/java/umc7th/bulk/mealItem/service/command/MealItemCommandServiceImpl.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import umc7th.bulk.mealItem.repository.MealItemRepository;
99
import umc7th.bulk.mealPlan.dto.MealPlanRequestDTO;
1010

11+
import java.util.Optional;
12+
1113

1214
@Service
1315
@RequiredArgsConstructor
@@ -20,26 +22,15 @@ public class MealItemCommandServiceImpl implements MealItemCommandService {
2022
@Transactional
2123
public MealItem createMealItem(MealPlanRequestDTO.MealItemDTO dto) {
2224

23-
log.info("🔍 Checking if meal item exists: {}", dto.getName());
24-
MealItem saveMealItem = mealItemRepository.saveAndFlush(dto.toMealItemEntity(dto));
25-
return saveMealItem;
26-
27-
// System.out.println(mealItem.getName());
28-
29-
/*if (mealItem == null) {
30-
MealItem saveMeal = mealItemRepository.save(dto.toMealItemEntity(dto));
31-
return saveMeal;
32-
}*/
33-
34-
/*if (mealItems.size() >= 2) {
35-
log.warn("⚠️ Found existing meal item: {}", dto.getName());
36-
mealItemRepository.deleteLastByName(dto.getName());
37-
}*/
38-
39-
/*MealItem newMealItem = mealItemRepository.save(dto.toMealItemEntity(dto));
40-
log.info("💾 Saving new meal item: {}", newMealItem);*/
25+
Optional<MealItem> mealItem1 = mealItemRepository.findByName(dto.getName());
4126

42-
// return mealItem;
27+
MealItem mealItem2 = null;
28+
if (mealItem1.isPresent()) {
29+
mealItem2 = mealItem1.get();
30+
} else {
31+
mealItem2 = mealItemRepository.save(dto.toMealItemEntity(dto));
32+
}
4333

34+
return mealItem2;
4435
}
4536
}

src/main/java/umc7th/bulk/mealMealItemMapping/entity/MealMealItemMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MealMealItemMapping {
2121
@JoinColumn(name = "meal_id")
2222
private Meal meal;
2323

24-
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
24+
@ManyToOne(fetch = FetchType.LAZY)
2525
@JoinColumn(name = "meal_item_id")
2626
private MealItem mealItem;
2727

0 commit comments

Comments
 (0)