From a86180989b819d3eae496b6dad54ec1c89fc2a35 Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Fri, 6 Jun 2025 08:58:38 +0900 Subject: [PATCH] =?UTF-8?q?WIP:=E3=83=90=E3=82=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 12 ++++--- .../todoapp/dto/DeleteToBuyRequest.java | 4 +-- .../example/todoapp/dto/ToBuyResponse.java | 4 +-- .../com/example/todoapp/dto/ToBuysDTO.java | 6 ++-- .../example/todoapp/model/RecipeStuffs.java | 5 +-- .../com/example/todoapp/model/Recipes.java | 7 ++-- .../com/example/todoapp/model/Stocks.java | 7 ++-- .../com/example/todoapp/model/Stuffs.java | 7 ++-- .../com/example/todoapp/model/ToBuys.java | 19 ++++++----- .../todoapp/repository/StocksRepository.java | 6 ++-- .../todoapp/repository/StuffsRepository.java | 2 +- .../todoapp/repository/ToBuysRepository.java | 8 ++--- .../todoapp/service/ToBuysService.java | 34 ++++++++++--------- 13 files changed, 66 insertions(+), 55 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 fd7a0a8..ca72bf5 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -10,6 +10,7 @@ package com.example.todoapp.controller; import com.example.todoapp.dto.DeleteToBuyRequest; import com.example.todoapp.dto.ToBuyResponse; import com.example.todoapp.dto.ToBuysDTO; +import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; import com.example.todoapp.repository.UserRepository; @@ -89,15 +90,16 @@ public class ToBuysController { User user = userRepository.findByUsername(username) .orElseThrow(() -> new UsernameNotFoundException("User not found")); - List toBuysList = toBuysService.getToBuysByUserId(user.getId()); + List toBuysList = toBuysService.getToBuysByUser(user); // DTO形式に変換して返す List responseList = toBuysList.stream() .map(toBuy -> { ToBuyResponse resp = new ToBuyResponse(); - resp.setTobuy_id(toBuy.getTobuy_id()); - resp.setStuff_id(toBuy.getStuff_id()); - resp.setStuff_name(stuffsRepository.findByStuff_id(toBuy.getStuff_id()).getStuff_name()); + Stuffs stuff = toBuy.getStuff(); + resp.setTobuyId(toBuy.getTobuyId()); + resp.setStuffId(stuff.getStuffId()); + resp.setStuff_name(stuff.getStuffName()); resp.setAmount(toBuy.getAmount()); resp.setShop(toBuy.getStore()); return resp; @@ -128,7 +130,7 @@ public class ToBuysController { User user = userRepository.findByUsername(username) .orElseThrow(() -> new UsernameNotFoundException("User not found")); - int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuy_id()); + int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuyId()); Map response = new HashMap<>(); diff --git a/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java b/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java index b19c196..b8f7b9e 100644 --- a/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java +++ b/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java @@ -4,6 +4,6 @@ import lombok.Data; @Data public class DeleteToBuyRequest { - private Long user_id; - private int tobuy_id; + private Long userId; + private int tobuyId; } \ No newline at end of file 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..8d1a992 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -4,8 +4,8 @@ import lombok.Data; @Data public class ToBuyResponse { - private int tobuy_id; - private Long stuff_id; + private int tobuyId; + private Long stuffId; 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..1bdee7f 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -13,9 +13,9 @@ import lombok.Data; @Data public class ToBuysDTO { - private int tobuy_id; - private Long stuff_id; - private Long user_id; + private int tobuyId; + private Long stuffId; + private Long userId; private int price; private int amount; private String shop; diff --git a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java index fc94cd5..0d911ef 100644 --- a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java @@ -37,7 +37,8 @@ public class RecipeStuffs { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int recipe_stuffs_id ; + @Column(name="recipe_stuffs_id") + private int recipeStuffsId ; /** * 料理の一意識別子 FK @@ -62,7 +63,7 @@ public class RecipeStuffs { referencedColumnName = "stuff_id", nullable = false ) - private Stuffs stuffs; + private Stuffs stuff; /** * 材料の数量 diff --git a/backend/src/main/java/com/example/todoapp/model/Recipes.java b/backend/src/main/java/com/example/todoapp/model/Recipes.java index 98d3936..cdaa809 100644 --- a/backend/src/main/java/com/example/todoapp/model/Recipes.java +++ b/backend/src/main/java/com/example/todoapp/model/Recipes.java @@ -35,14 +35,15 @@ public class Recipes { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int recipe_id ; + @Column(name="recipe_id") + private int recipeId ; /** * カテゴリ名 */ @NotNull - @Column(unique = true, length = 255, nullable = false) - private String recipie_name; + @Column(name="recipe_name", unique = true, length = 255, nullable = false) + private String recipieName; /** * カテゴリ 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 fcb13db..398cf2b 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stocks.java +++ b/backend/src/main/java/com/example/todoapp/model/Stocks.java @@ -39,7 +39,8 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long stock_id ; + @Column(name="stock_id") + private Long stockId ; /** @@ -52,7 +53,7 @@ public class Stocks { referencedColumnName = "stuff_id", nullable = false ) - private Long stuff_id; + private Stuffs stuff; /** @@ -65,7 +66,7 @@ public class Stocks { referencedColumnName = "id", nullable = false ) - private User user_id; + private User user; /** * 在庫数量(デフォルト値: 1) 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..2b164ad 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/Stuffs.java @@ -35,14 +35,15 @@ import lombok.NoArgsConstructor; */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long stuff_id ; + @Column(name = "stuff_id") + private Long stuffId; /** * カテゴリ名 */ @NotNull - @Column(unique = true, length = 255, nullable = false) - private String stuff_name; + @Column(name = "stuff_name", unique = true, length = 255, nullable = false) + private String stuffName; /** * カテゴリ 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 2fed2ca..d02faa4 100644 --- a/backend/src/main/java/com/example/todoapp/model/ToBuys.java +++ b/backend/src/main/java/com/example/todoapp/model/ToBuys.java @@ -36,7 +36,8 @@ public class ToBuys { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int tobuy_id ; + @Column(name = "tobuy_id") + private int tobuyId ; /** * 材料の一意識別子 FK @@ -48,18 +49,20 @@ public class ToBuys { referencedColumnName = "stuff_id", nullable = false ) - private Long stuff_id; + private Stuffs stuff; /** * ユーザーテーブル参照用の外部キー */ // @NotNull - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn( - name = "user_id", - referencedColumnName = "id" - ) - private User user_id; + + /** + * タスクの所有者(ユーザー) + * 多対一の関係で、遅延ロードを使用 + */ + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private User user; /** * 購入する数量 diff --git a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java index 55811e2..3fd0855 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -42,9 +42,9 @@ public interface StocksRepository extends JpaRepository { /** * 在庫リストから指定した食材を削除する * - * @param stock_id 削除する在庫 - * @param user_id 削除するユーザー + * @param stockId 削除する在庫 + * @param userId 削除するユーザー * @return 削除した場合true */ - boolean DeleteStockByStock_id(int stock_id, Long user_id); + boolean DeleteStockByStock_id(int stockId, Long userId); } \ No newline at end of file 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 7b37ae1..82d3137 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -24,6 +24,6 @@ import org.springframework.stereotype.Repository; @Repository public interface StuffsRepository extends JpaRepository { // 材料情報を主キーで取得するメソッド(必要に応じて追加) - Stuffs findByStuff_id(Long stuff_id); + Stuffs findByStuffId(Long stuff_id); } \ 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 57cd2cb..89f0d7d 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -33,8 +33,8 @@ public interface ToBuysRepository extends JpaRepository { * @param userId ユーザーID * @return 「買うもの」リスト */ - @Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1") - List findByUserId(Long user_id); + @Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1") + List findByUser(Long user_id); /** * 指定されたユーザーIDに基づいて「買うもの」リストを取得する @@ -44,6 +44,6 @@ public interface ToBuysRepository extends JpaRepository { * @return */ @Modifying - @Query("DELETE FROM ToBuys t WHERE t.user_id.id = :userId AND t.tobuy_id = :tobuyId") - int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId); + @Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId") + int deleteByUser_IdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int 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 f34be5f..0d91e1f 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -70,15 +70,15 @@ public class ToBuysService { .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); Stuffs stuff; - if (toBuyDTO.getStuff_id() == null) { + if (toBuyDTO.getStuffId() == null) { // 新しい材料を作成 stuff = new Stuffs(); - stuff.setStuff_name(toBuyDTO.getStuff_name()); + stuff.setStuffName(toBuyDTO.getStuff_name()); stuff.setCategory(toBuyDTO.getCategory()); stuff = stuffsRepository.save(stuff); } else { // 材料情報を取得 - Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); + Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); if (!optionalStuffs.isPresent()) { throw new RuntimeException("材料がありません"); } @@ -86,8 +86,8 @@ public class ToBuysService { } ToBuys toBuys = new ToBuys(); - toBuys.setUser_id(user); - toBuys.setStuff_id(stuff.getStuff_id()); + toBuys.setUser(user); + toBuys.setStuff(stuff); toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setStore(toBuyDTO.getShop()); @@ -109,15 +109,15 @@ public class ToBuysService { .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); Stuffs stuffs; - if (toBuyDTO.getStuff_id() == null) { + if (toBuyDTO.getStuffId() == null) { // 新しい材料を作成 stuffs = new Stuffs(); - stuffs.setStuff_name(toBuyDTO.getStuff_name()); + stuffs.setStuffName(toBuyDTO.getStuff_name()); stuffs.setCategory(toBuyDTO.getCategory()); stuffs = stuffsRepository.save(stuffs); } else { // 材料情報を取得 - Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); + Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); if (!optionalStuffs.isPresent()) { throw new RuntimeException("材料がありません"); } @@ -132,8 +132,8 @@ public class ToBuysService { ToBuys toBuys = new ToBuys(); - toBuys.setUser_id(user); - toBuys.setStuff_id(stuffs.getStuff_id()); + toBuys.setUser(user); + toBuys.setStuff(stuffs); toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setStore(toBuyDTO.getShop()); @@ -148,8 +148,8 @@ public class ToBuysService { * @param userId ユーザーID * @return ユーザーに紐づく「買うもの」リスト */ - public List getToBuysByUserId(Long userId) { - return toBuysRepository.findByUserId(userId); + public List getToBuysByUser(User user) { + return toBuysRepository.findByUser(user.getId()); } /** @@ -160,7 +160,7 @@ public class ToBuysService { */ @Transactional public int deleteToBuyByIds(Long userId, int tobuyId) { - return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); + return toBuysRepository.deleteByUser_IdAndTobuyId(userId, tobuyId); } public Stocks buyToBuys(String username, ToBuysDTO dto) { @@ -168,14 +168,16 @@ public class ToBuysService { User user = getUserByUsername(username); // 新しい在庫を作成 Stocks stock = new Stocks(); - stock.setStuff_id(dto.getStuff_id()); - stock.setUser_id(user); + Stuffs stuff = new Stuffs(); + stuff.setStuffId(dto.getStuffId()); + stock.setStuff(stuff); + stock.setUser(user); stock.setAmount(dto.getAmount()); stock.setPrice(dto.getPrice()); stock.setLast_update(dto.getLast_update()); stock.setBuy_date(dto.getBuy_date()); stock.setExp_date(dto.getExp_date()); - deleteToBuyByIds(dto.getUser_id(), dto.getTobuy_id()); + deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); // データベースに保存 return stocksRepository.save(stock); }