From 99d34a0eedb4af505a41b1d7e46059d89270fd45 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 16:05:46 +0900 Subject: [PATCH] managing --- .../com/example/todoapp/controller/ToBuysController.java | 8 ++++++-- .../main/java/com/example/todoapp/dto/ToBuyResponse.java | 2 +- .../src/main/java/com/example/todoapp/dto/ToBuysDTO.java | 2 +- .../src/main/java/com/example/todoapp/model/Stocks.java | 2 +- .../src/main/java/com/example/todoapp/model/Stuffs.java | 2 +- .../src/main/java/com/example/todoapp/model/ToBuys.java | 2 +- .../com/example/todoapp/repository/StuffsRepository.java | 4 ++++ .../java/com/example/todoapp/service/ToBuysService.java | 3 ++- 8 files changed, 17 insertions(+), 8 deletions(-) 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 96c36e1..fd7a0a8 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -13,6 +13,7 @@ import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; import com.example.todoapp.repository.UserRepository; +import com.example.todoapp.repository.StuffsRepository; import com.example.todoapp.service.ToBuysService; import jakarta.validation.Valid; @@ -44,6 +45,9 @@ public class ToBuysController { @Autowired private UserRepository userRepository; + @Autowired + private StuffsRepository stuffsRepository; + /** * 新しい購入アイテムを追加する * @@ -92,8 +96,8 @@ public class ToBuysController { .map(toBuy -> { ToBuyResponse resp = new ToBuyResponse(); resp.setTobuy_id(toBuy.getTobuy_id()); - resp.setStuff_id(toBuy.getStuffs().getStuff_id()); - resp.setStuff_name(toBuy.getStuffs().getStuff_name()); + resp.setStuff_id(toBuy.getStuff_id()); + resp.setStuff_name(stuffsRepository.findByStuff_id(toBuy.getStuff_id()).getStuff_name()); resp.setAmount(toBuy.getAmount()); resp.setShop(toBuy.getStore()); return resp; diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java index bf5730b..3968169 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -5,7 +5,7 @@ import lombok.Data; @Data public class ToBuyResponse { private int tobuy_id; - private Long stuff_id; + private int stuff_id; private String stuff_name; private int amount; private String shop; diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java index fe6db25..649b912 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -14,7 +14,7 @@ import lombok.Data; @Data public class ToBuysDTO { private int tobuy_id; - private Long stuff_id; + private Integer stuff_id; private Long user_id; private int price; private int amount; diff --git a/backend/src/main/java/com/example/todoapp/model/Stocks.java b/backend/src/main/java/com/example/todoapp/model/Stocks.java index fb1b84b..4a13d3f 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stocks.java +++ b/backend/src/main/java/com/example/todoapp/model/Stocks.java @@ -52,7 +52,7 @@ public class Stocks { referencedColumnName = "stuff_id", nullable = false ) - private Long stuff_id; + private int stuff_id; /** diff --git a/backend/src/main/java/com/example/todoapp/model/Stuffs.java b/backend/src/main/java/com/example/todoapp/model/Stuffs.java index 55fe1f9..fc69e4e 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/Stuffs.java @@ -35,7 +35,7 @@ import lombok.NoArgsConstructor; */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long stuff_id ; + private int stuff_id ; /** * カテゴリ名 diff --git a/backend/src/main/java/com/example/todoapp/model/ToBuys.java b/backend/src/main/java/com/example/todoapp/model/ToBuys.java index 0670392..7c46c1a 100644 --- a/backend/src/main/java/com/example/todoapp/model/ToBuys.java +++ b/backend/src/main/java/com/example/todoapp/model/ToBuys.java @@ -48,7 +48,7 @@ public class ToBuys { referencedColumnName = "stuff_id", nullable = false ) - private Stuffs stuffs; + private int stuff_id; /** * ユーザーテーブル参照用の外部キー 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 1e2fa69..fa6e952 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -9,6 +9,8 @@ package com.example.todoapp.repository; import com.example.todoapp.model.Stuffs; + + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -22,4 +24,6 @@ import org.springframework.stereotype.Repository; @Repository public interface StuffsRepository extends JpaRepository { // 材料情報を主キーで取得するメソッド(必要に応じて追加) + Stuffs findByStuff_id(int stuff_id); + } \ 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 a59c814..376c1c0 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -78,7 +78,8 @@ public class ToBuysService { stuffs = stuffsRepository.save(stuffs); } else { // 材料情報を取得 - Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); + int stuff_id = toBuyDTO.getStuff_id(); + Optional optionalStuffs = stuffsRepository.findById(stuff_id); if (!optionalStuffs.isPresent()) { throw new RuntimeException("材料がありません"); }