変更を実装しました

backend-tobuy-adddish
zhang.pengcheng 5 months ago
parent 88d927c3fe
commit 036fbfa09a
  1. 10
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 48
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -59,6 +59,16 @@ public class ToBuysController {
return ResponseEntity.ok("Item added to 'To Buys' successfully");
}
@PutMapping("/update")
public ResponseEntity<String> updateToBuys(
@Valid @RequestBody ToBuysDTO dto,
Authentication authentication) {
toBuysService.updateToBuys(dto, authentication);
return ResponseEntity.ok("Item updated to 'To Buys' successfully");
}
/**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する
*

@ -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に基づいてすべての買うものリストを取得する
*

Loading…
Cancel
Save