From 765fec0ebc7353ca77396966e7adfbc40ec049de Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 15:43:36 +0900 Subject: [PATCH] =?UTF-8?q?=E8=B3=BC=E5=85=A5=E5=87=A6=E7=90=86=E3=81=AEAP?= =?UTF-8?q?I=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 33 ++++++++-------- .../example/todoapp/dto/BuyRequestDTO.java | 14 +++++++ .../todoapp/repository/ToBuysRepository.java | 16 ++++++-- .../todoapp/service/ToBuysService.java | 38 ++++++++++++------- 4 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 backend/src/main/java/com/example/todoapp/dto/BuyRequestDTO.java 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 7505145..d1ab6f9 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -7,6 +7,7 @@ package com.example.todoapp.controller; +import com.example.todoapp.dto.BuyRequestDTO; import com.example.todoapp.dto.DeleteToBuyRequest; import com.example.todoapp.dto.ToBuyResponse; import com.example.todoapp.dto.ToBuysDTO; @@ -47,9 +48,6 @@ public class ToBuysController { @Autowired private UserRepository userRepository; - @Autowired - private StuffsRepository stuffsRepository; - /** * 新しい購入アイテムを追加する * @@ -140,19 +138,11 @@ public class ToBuysController { public ResponseEntity> deleteToBuy( @RequestBody DeleteToBuyRequest request, Authentication authentication) { - - // 認証されたユーザー名を取得 - String username = authentication.getName(); - // ユーザー情報を取得 - User user = userRepository.findByUsername(username) - .orElseThrow(() -> new UsernameNotFoundException("User not found")); - - int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuyId()); + int deletedCount = toBuysService.deleteToBuyById(request.getTobuyId()); Map response = new HashMap<>(); - if (deletedCount > 0) { response.put("result", true); } else { @@ -162,12 +152,23 @@ public class ToBuysController { return ResponseEntity.ok(response); } - @PostMapping("/{id}") - public ResponseEntity buyToBuys( + /** + * 買うものリストの在庫登録(購入処理) + * 買うものリストから削除し,追加情報を付加して在庫リストに追加 + * + * @param request 「買うもの」の情報を含むリクエストボディ + * @return + */ + @PostMapping("/buy") + public ResponseEntity> buyToBuys( Authentication authentication, - @PathVariable("id") ToBuysDTO toBuysDTO) { + @RequestBody BuyRequestDTO toBuysDTO) { + toBuysService.buyToBuys(authentication.getName(), toBuysDTO); - return ResponseEntity.ok().build(); + + Map response = new HashMap<>(); + response.put("result", true); + return ResponseEntity.ok(response); } } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/BuyRequestDTO.java b/backend/src/main/java/com/example/todoapp/dto/BuyRequestDTO.java new file mode 100644 index 0000000..38438f9 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/BuyRequestDTO.java @@ -0,0 +1,14 @@ +package com.example.todoapp.dto; + +import java.time.LocalDate; + +import lombok.Data; + +@Data +public class BuyRequestDTO { + private Long tobuyId; + private int price; + private LocalDate expDate; + private LocalDate lastUpdate; + private LocalDate buyDate; +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java index ea45de7..f07f742 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -27,6 +27,15 @@ import org.springframework.stereotype.Repository; @Repository public interface ToBuysRepository extends JpaRepository { + /** + * 指定されたIDに基づいて「買うもの」レコードを取得する + * + * @param tobuyId 「買うもの」ID + * @return 「買うもの」レコード + */ + @Query("SELECT t FROM ToBuys t WHERE t.tobuyId = ?1") + ToBuys findById(Long tobuyId); + /** * 指定されたユーザーIDに基づいて「買うもの」リストを取得する * @@ -37,13 +46,12 @@ public interface ToBuysRepository extends JpaRepository { List findByUser(Long userId); /** - * 指定されたユーザーIDに基づいて「買うもの」リストを取得する + * 指定された「買うもの」IDに基づいて「買うもの」リストを削除 * - * @param userId ユーザーID * @param tobuyId 「買うもの」ID * @return */ @Modifying - @Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId") - int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") Long tobuyId); + @Query("DELETE FROM ToBuys t WHERE t.tobuyId = :tobuyId") + int deleteByTobuyId(@Param("tobuyId") Long tobuyId); } \ 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 54ef18e..5cc9cde 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -9,7 +9,7 @@ package com.example.todoapp.service; import com.example.todoapp.util.MessageUtils; - +import com.example.todoapp.dto.BuyRequestDTO; import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.model.Stocks; import com.example.todoapp.model.Stuffs; @@ -148,9 +148,9 @@ public class ToBuysService { } /** - * 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する + * 指定されたユーザーに基づいてすべての「買うもの」リストを取得する * - * @param userId ユーザーID + * @param user ユーザー * @return ユーザーに紐づく「買うもの」リスト */ public List getToBuysByUser(User user) { @@ -158,31 +158,43 @@ public class ToBuysService { } /** - * 指定されたユーザーIDと購入リストIDに基づいて「買うもの」を削除する + * 指定された購入リストIDに基づいて「買うもの」を削除する * - * @param userId ユーザーID * @param tobuyId 購入リストID */ @Transactional - public int deleteToBuyByIds(Long userId, Long tobuyId) { - return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); + public int deleteToBuyById(Long tobuyId) { + return toBuysRepository.deleteByTobuyId(tobuyId); } - public Stocks buyToBuys(String username, ToBuysDTO dto) { + /** + * 指定されたユーザーIDと購入データに基づいて「買うもの」を購入する + * + * @param username ユーザーID + * @param dto 購入データ + */ + @Transactional + public Stocks buyToBuys(String username, BuyRequestDTO dto) { // ユーザー情報を取得 User user = getUserByUsername(username); + + // Tobuy情報の取得 + ToBuys tobuy = toBuysRepository.findById(dto.getTobuyId()); + // 新しい在庫を作成 Stocks stock = new Stocks(); - Stuffs stuff = new Stuffs(); - stuff.setStuffId(dto.getStuffId()); - stock.setStuff(stuff); + stock.setStuff(tobuy.getStuff()); stock.setUser(user); - stock.setAmount(dto.getAmount()); + stock.setAmount(tobuy.getAmount()); stock.setPrice(dto.getPrice()); stock.setLastUpdate(dto.getLastUpdate()); stock.setBuyDate(dto.getBuyDate()); stock.setExpDate(dto.getExpDate()); - deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); + + // 買うものリストから削除 + System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); + deleteToBuyById(tobuy.getTobuyId()); + // データベースに保存 return stocksRepository.save(stock); }