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 196f8a8..03c7e30 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -57,19 +57,36 @@ public class ToBuysController { */ @PostMapping("/add") - public ResponseEntity addToBuys( + public ResponseEntity> addToBuys( @Valid @RequestBody ToBuysDTO dto, Authentication authentication) { - toBuysService.addToBuys(dto, authentication); - return ResponseEntity.ok("Item added to 'To Buys' successfully"); + + ToBuys createdToBuy = toBuysService.addToBuys(dto, authentication); + + // 构建响应体 + Map response = new HashMap<>(); + response.put("result", true); + response.put("tobuyId", createdToBuy.getTobuyId()); + response.put("stuffId", createdToBuy.getStuff().getStuffId()); + response.put("message", "追加に成功しました"); + + return ResponseEntity.ok(response); } @PutMapping("/update") - public ResponseEntity updateToBuys( + public ResponseEntity> updateToBuys( @Valid @RequestBody ToBuysDTO dto, Authentication authentication) { toBuysService.updateToBuys(dto, authentication); - return ResponseEntity.ok("Item updated to 'To Buys' successfully"); + + ToBuys updatedToBuy = toBuysService.updateToBuys(dto, authentication); + + + Map response = new HashMap<>(); + response.put("tobuyId", updatedToBuy.getTobuyId()); + response.put("stuffId", updatedToBuy.getStuff().getStuffId()); + + return ResponseEntity.ok(response); } diff --git a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java index 6edbc88..1bd452f 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -10,6 +10,7 @@ package com.example.todoapp.repository; import com.example.todoapp.model.Stuffs; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -26,4 +27,6 @@ public interface StuffsRepository extends JpaRepository { // 材料情報を主キーで取得するメソッド(必要に応じて追加) Stuffs findByStuffId(Long stuffId); + Optional findByStuffName(String stuffName); + } \ No newline at end of file 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 0908e01..54ef18e 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -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 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 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); } /**