|
|
|
@ -85,6 +85,54 @@ public class ToBuysService { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 購入リストに新しいアイテムを追加する |
|
|
|
|
* |
|
|
|
|
* @param toBuyDTO 追加する購入アイテムのデータ(DTO) |
|
|
|
|
*/ |
|
|
|
|
public void updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
|
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
String username = authentication.getName(); |
|
|
|
|
User user = userRepository.findByUsername(username) |
|
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); |
|
|
|
|
|
|
|
|
|
Stuffs stuffs; |
|
|
|
|
if (toBuyDTO.getStuff_id() == null) { |
|
|
|
|
// 新しい材料を作成
|
|
|
|
|
stuffs = new Stuffs(); |
|
|
|
|
stuffs.setStuff_name(toBuyDTO.getStuff_name()); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
|
} else { |
|
|
|
|
// 材料情報を取得
|
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); |
|
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
|
} |
|
|
|
|
stuffs = optionalStuffs.get(); |
|
|
|
|
|
|
|
|
|
//update
|
|
|
|
|
// stuffs.setStuff_name(toBuyDTO.getStuff_name());
|
|
|
|
|
// stuffs.setCategory(toBuyDTO.getCategory());
|
|
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
|
toBuys.setUser_id(user); |
|
|
|
|
toBuys.setStuffs(stuffs); |
|
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
toBuysRepository.save(toBuys); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する |
|
|
|
|
* |
|
|
|
|