From 036fbfa09aa6bbc3211d12194e1e2ca2946cecfd Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Thu, 5 Jun 2025 13:43:32 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=82=92=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 10 ++++ .../todoapp/service/ToBuysService.java | 48 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java index bce8a33..7b81ea3 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -59,6 +59,16 @@ public class ToBuysController { return ResponseEntity.ok("Item added to 'To Buys' successfully"); } + @PutMapping("/update") + public ResponseEntity updateToBuys( + @Valid @RequestBody ToBuysDTO dto, + Authentication authentication) { + toBuysService.updateToBuys(dto, authentication); + return ResponseEntity.ok("Item updated to 'To Buys' successfully"); + } + + + /** * 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する * diff --git a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java index 8049ee9..68b6da8 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -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 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に基づいてすべての「買うもの」リストを取得する *