|
|
|
@ -61,6 +61,8 @@ public class ToBuysService { |
|
|
|
|
* 購入リストに新しいアイテムを追加する |
|
|
|
|
* |
|
|
|
|
* @param toBuyDTO 追加する購入アイテムのデータ(DTO) |
|
|
|
|
* @param authentication 認証情報 |
|
|
|
|
* @return 追加された購入アイテム |
|
|
|
|
*/ |
|
|
|
|
public ToBuys addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { |
|
|
|
|
|
|
|
|
@ -78,7 +80,7 @@ public class ToBuysService { |
|
|
|
|
stuff = stuffsRepository.save(stuff); |
|
|
|
|
} else { |
|
|
|
|
// 材料情報を取得
|
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); |
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findByStuffId(toBuyDTO.getStuffId()); |
|
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
|
} |
|
|
|
@ -114,11 +116,11 @@ public class ToBuysService { |
|
|
|
|
Optional<Stuffs> existingStuffs = stuffsRepository.findByStuffName(toBuyDTO.getStuffName()); |
|
|
|
|
// 新しい材料を作成
|
|
|
|
|
if (existingStuffs.isPresent()) { |
|
|
|
|
// 如果存在,更新已有材料的属性
|
|
|
|
|
// 材料が存在する場合、更新
|
|
|
|
|
stuffs = existingStuffs.get(); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); // 可选:更新分类
|
|
|
|
|
} else { |
|
|
|
|
// 否则新建材料
|
|
|
|
|
// 材料が存在しない場合、新規作成
|
|
|
|
|
stuffs = new Stuffs(); |
|
|
|
|
stuffs.setStuffName(toBuyDTO.getStuffName()); |
|
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
@ -154,7 +156,7 @@ public class ToBuysService { |
|
|
|
|
* @return ユーザーに紐づく「買うもの」リスト |
|
|
|
|
*/ |
|
|
|
|
public List<ToBuys> getToBuysByUser(User user) { |
|
|
|
|
return toBuysRepository.findByUser(user.getId()); |
|
|
|
|
return toBuysRepository.findByUserIdOrderByTobuyIdAsc(user.getId()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -163,7 +165,7 @@ public class ToBuysService { |
|
|
|
|
* @param tobuyId 購入リストID |
|
|
|
|
*/ |
|
|
|
|
@Transactional |
|
|
|
|
public int deleteToBuyById(Long tobuyId) { |
|
|
|
|
public int deleteToBuysByTobuyId(Long tobuyId) { |
|
|
|
|
return toBuysRepository.deleteByTobuyId(tobuyId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -179,7 +181,7 @@ public class ToBuysService { |
|
|
|
|
User user = getUserByUsername(username); |
|
|
|
|
|
|
|
|
|
// Tobuy情報の取得
|
|
|
|
|
ToBuys tobuy = toBuysRepository.findById(dto.getTobuyId()); |
|
|
|
|
ToBuys tobuy = toBuysRepository.findByTobuyId(dto.getTobuyId()); |
|
|
|
|
|
|
|
|
|
// 新しい在庫を作成
|
|
|
|
|
Stocks stock = new Stocks(); |
|
|
|
@ -193,7 +195,7 @@ public class ToBuysService { |
|
|
|
|
|
|
|
|
|
// 買うものリストから削除
|
|
|
|
|
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); |
|
|
|
|
deleteToBuyById(tobuy.getTobuyId()); |
|
|
|
|
deleteToBuysByTobuyId(tobuy.getTobuyId()); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
return stocksRepository.save(stock); |
|
|
|
|