|
|
|
@ -91,14 +91,22 @@ public class ToBuysService { |
|
|
|
|
stuff = optStuff.get(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
|
toBuys.setUser(user); |
|
|
|
|
toBuys.setStuff(stuff); |
|
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
Optional<ToBuys> existingToBuy = toBuysRepository.findByUserAndStuff(user, stuff); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
return toBuysRepository.save(toBuys); |
|
|
|
|
if (existingToBuy.isPresent()) { |
|
|
|
|
// 存在する場合は数量を更新
|
|
|
|
|
ToBuys existing = existingToBuy.get(); |
|
|
|
|
existing.setAmount(existing.getAmount() + toBuyDTO.getAmount()); |
|
|
|
|
return toBuysRepository.save(existing); |
|
|
|
|
} else { |
|
|
|
|
// 新しい材料を作成
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
|
toBuys.setUser(user); |
|
|
|
|
toBuys.setStuff(stuff); |
|
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
return toBuysRepository.save(toBuys); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -212,7 +220,7 @@ public class ToBuysService { |
|
|
|
|
* @param authentication 認証情報 |
|
|
|
|
* @return 追加された「買うもの」のリスト |
|
|
|
|
*/ |
|
|
|
|
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Authentication authentication) { |
|
|
|
|
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings,Authentication authentication) { |
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
String username = authentication.getName(); |
|
|
|
|
User user = userRepository.findByUsername(username) |
|
|
|
@ -225,7 +233,9 @@ public class ToBuysService { |
|
|
|
|
|
|
|
|
|
for (RecipeStuffs rs : recipeStuffsList) { |
|
|
|
|
Stuffs stuff = rs.getStuff(); |
|
|
|
|
int requiredAmount = rs.getAmount(); |
|
|
|
|
|
|
|
|
|
// 材料の数量をサービング数に基づいて計算
|
|
|
|
|
int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1); |
|
|
|
|
|
|
|
|
|
Optional<ToBuys> existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff); |
|
|
|
|
|
|
|
|
|