|
|
|
@ -62,7 +62,7 @@ public class ToBuysService { |
|
|
|
|
* |
|
|
|
|
* @param toBuyDTO 追加する購入アイテムのデータ(DTO) |
|
|
|
|
*/ |
|
|
|
|
public void addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
public ToBuys addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
|
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
String username = authentication.getName(); |
|
|
|
@ -92,8 +92,8 @@ public class ToBuysService { |
|
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
toBuysRepository.save(toBuys); |
|
|
|
|
|
|
|
|
|
return toBuysRepository.save(toBuys); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -101,7 +101,7 @@ public class ToBuysService { |
|
|
|
|
* |
|
|
|
|
* @param toBuyDTO 変更する購入アイテムのデータ(DTO) |
|
|
|
|
*/ |
|
|
|
|
public void updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
public ToBuys updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
|
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
String username = authentication.getName(); |
|
|
|
@ -110,11 +110,20 @@ public class ToBuysService { |
|
|
|
|
|
|
|
|
|
Stuffs stuffs; |
|
|
|
|
if (toBuyDTO.getStuffId() == null) { |
|
|
|
|
|
|
|
|
|
Optional<Stuffs> existingStuffs = stuffsRepository.findByStuffName(toBuyDTO.getStuffName()); |
|
|
|
|
// 新しい材料を作成
|
|
|
|
|
stuffs = new Stuffs(); |
|
|
|
|
stuffs.setStuffName(toBuyDTO.getStuffName()); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
|
if (existingStuffs.isPresent()) { |
|
|
|
|
// 如果存在,更新已有材料的属性
|
|
|
|
|
stuffs = existingStuffs.get(); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); // 可选:更新分类
|
|
|
|
|
} else { |
|
|
|
|
// 否则新建材料
|
|
|
|
|
stuffs = new Stuffs(); |
|
|
|
|
stuffs.setStuffName(toBuyDTO.getStuffName()); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
|
} |
|
|
|
|
stuffsRepository.save(stuffs); |
|
|
|
|
} else { |
|
|
|
|
// 材料情報を取得
|
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); |
|
|
|
@ -124,22 +133,18 @@ public class ToBuysService { |
|
|
|
|
stuffs = optionalStuffs.get(); |
|
|
|
|
|
|
|
|
|
//update
|
|
|
|
|
// stuffs.setStuffName(toBuyDTO.getStuffName());
|
|
|
|
|
// stuffs.setCategory(toBuyDTO.getCategory());
|
|
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
|
toBuys.setTobuyId(toBuyDTO.getTobuyId()); |
|
|
|
|
toBuys.setUser(user); |
|
|
|
|
toBuys.setStuff(stuffs); |
|
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
toBuysRepository.save(toBuys); |
|
|
|
|
|
|
|
|
|
return toBuysRepository.save(toBuys); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|