From 6cf66af7cae66222be2396cd6fc1ca1d2adcdcb7 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 13:06:39 +0900 Subject: [PATCH 01/32] created stock backend(controller, dto, repository, service) --- .../todoapp/controller/StocksController.java | 106 +++++++++++++++++ .../com/example/todoapp/dto/StockDTO.java | 47 ++++++++ .../todoapp/repository/StocksRepository.java | 50 ++++++++ .../todoapp/service/StocksService.java | 112 ++++++++++++++++++ 4 files changed, 315 insertions(+) create mode 100644 backend/src/main/java/com/example/todoapp/controller/StocksController.java create mode 100644 backend/src/main/java/com/example/todoapp/dto/StockDTO.java create mode 100644 backend/src/main/java/com/example/todoapp/repository/StocksRepository.java create mode 100644 backend/src/main/java/com/example/todoapp/service/StocksService.java diff --git a/backend/src/main/java/com/example/todoapp/controller/StocksController.java b/backend/src/main/java/com/example/todoapp/controller/StocksController.java new file mode 100644 index 0000000..8bdf482 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/StocksController.java @@ -0,0 +1,106 @@ +package com.example.todoapp.controller; + +import com.example.todoapp.dto.StockDTO; +import com.example.todoapp.model.Stocks; +import com.example.todoapp.service.StocksService; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 在庫管理のコントローラー + *

+ * このコントローラーは在庫の取得、作成、更新、削除などの + * エンドポイントを提供します。すべてのエンドポイントは認証が必要です。 + *

+ */ +@RestController +@RequestMapping("/stocks") +public class StocksController { + + @Autowired + private StocksService stockService; + + /** + * ログインユーザーのすべての在庫を取得する + * + * @param authentication 認証情報 + * @return ユーザーの在庫リスト + */ + @GetMapping + public ResponseEntity> getAllStocks(Authentication authentication) { + List stocks = stockService.getALLStocksByUser(authentication.getName()); + // エンティティからDTOへの変換 + List stockDTOs = stocks.stream() + .map(StockDTO::fromEntity) + .collect(Collectors.toList()); + return ResponseEntity.ok(stockDTOs); + } + + /** + * 指定されたIDの在庫を取得する + * + * @param authentication 認証情報 + * @param stockId 在庫ID + * @return 在庫情報 + */ + @GetMapping("/{id}") + public ResponseEntity getStockById( + Authentication authentication, + @PathVariable("id") Long stockId) { + Stocks stock = stockService.getStockById(authentication.getName(), stockId); + return ResponseEntity.ok(StockDTO.fromEntity(stock)); + } + + /** + * 新しい在庫を作成する + * + * @param authentication 認証情報 + * @param stock 作成する在庫の情報 + * @return 作成された在庫 + */ + @PostMapping + public ResponseEntity createStock( + Authentication authentication, + @Valid @RequestBody Stocks stock) { + Stocks createdStock = stockService.createStock(authentication.getName(), stock); + return ResponseEntity.ok(StockDTO.fromEntity(createdStock)); + } + + /** + * 指定されたIDの在庫を更新する + * + * @param authentication 認証情報 + * @param stockId 更新する在庫のID + * @param stockDetails 更新内容 + * @return 更新された在庫 + */ + @PutMapping("/{id}") + public ResponseEntity updateStock( + Authentication authentication, + @PathVariable("id") Long stockId, + @Valid @RequestBody Stocks stockDetails) { + Stocks updatedStock = stockService.updateStocks(authentication.getName(), stockId, stockDetails); + return ResponseEntity.ok(StockDTO.fromEntity(updatedStock)); + } + + /** + * 指定されたIDの在庫を削除する + * + * @param authentication 認証情報 + * @param taskId 削除する在庫のID + * @return 空のレスポンス + */ + @DeleteMapping("/{id}") + public ResponseEntity deleteStock( + Authentication authentication, + @PathVariable("id") Long stockId) { + stockService.deleteStock(authentication.getName(), stockId); + return ResponseEntity.ok().build(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java new file mode 100644 index 0000000..8363c3e --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java @@ -0,0 +1,47 @@ +package com.example.todoapp.dto; + +import com.example.todoapp.model.Stocks; +import com.example.todoapp.model.Stuffs; + +import lombok.Data; + +import java.time.LocalDate; + +/** + * 在庫のデータ転送オブジェクト(DTO) + *

+ * このクラスはクライアントとサーバー間で在庫情報をやり取りするために使用されます。 + * エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 + *

+ */ + +@Data +public class StockDTO { + private int stock_id; + private Stuffs stuffs; + private Long user_id; + private int amount; + private int price; + private LocalDate buy_date; + private LocalDate last_update; + private LocalDate exp_date; + + /** + * 在庫エンティティからDTOを作成する + * + * @param stock 変換元の在庫エンティティ + * @return 変換されたStockDTOオブジェクト + */ + public static StockDTO fromEntity(Stocks stock) { + StockDTO dto = new StockDTO(); + dto.setStock_id(stock.getStock_id()); + dto.setStuffs(stock.getStuffs()); + dto.setUser_id(stock.getUser_id().getId()); + dto.setAmount(stock.getAmount()); + dto.setPrice(stock.getPrice()); + dto.setBuy_date(stock.getBuy_date()); + dto.setLast_update(stock.getLast_update()); + dto.setExp_date(stock.getExp_date()); + return dto; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java new file mode 100644 index 0000000..10334d2 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -0,0 +1,50 @@ +//-------------------------------- +// ToBuysRepository.java +// +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.repository; + +import com.example.todoapp.model.Stocks; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + * 在庫エンティティのリポジトリインターフェース + *

+ * このインターフェースは在庫データへのアクセスと操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ + +@Repository +public interface StocksRepository extends JpaRepository { + /** + * user_idから在庫一覧をstock_id順で取得する + * + * @param user_id 検索するユーザーID + * @return 在庫リスト + */ + List findStocksByUser_id(Long user_id); + + /** + * 在庫情報を更新する + * + * @param stock 編集する新たな情報が入ったstockオブジェクト + * @return 編集に成功したらtrue + */ + boolean UpdateStockByStock_id(Stocks stock); + + /** + * 在庫リストから指定した食材を削除する + * + * @param stock_id 削除する在庫 + * @param user_id 削除するユーザー + * @return 削除した場合true + */ + boolean DeleteStockByStock_id(int stock_id, Long user_id); +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/StocksService.java b/backend/src/main/java/com/example/todoapp/service/StocksService.java new file mode 100644 index 0000000..85580d2 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/StocksService.java @@ -0,0 +1,112 @@ +package com.example.todoapp.service; + +import com.example.todoapp.model.Stocks; +import com.example.todoapp.util.MessageUtils; +import com.example.todoapp.model.User; +import com.example.todoapp.repository.StocksRepository; +import com.example.todoapp.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Stocksテーブルのサービスクラス + *

+ * このクラスは在庫の追加、取得、更新、削除などのビジネスロジックを提供します。 + *

+ */ +@Service +public class StocksService { + @Autowired + private StocksRepository stocksRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private MessageUtils messageUtils; + + /** + * 新たな在庫を追加する + * + * @param username ユーザー名 + * @param stock 追加する在庫情報 + * @return 保存された在庫エンティティ + */ + public Stocks createStock(String username, Stocks stock) { + User user = getUserByUsername(username); + stock.setUser_id(user); + return stocksRepository.save(stock); + + } + + /** + * 指定されたユーザのすべての在庫を取得する + * @param username ユーザー名 + * @return ユーザーの在庫リスト(stock_id昇順) + */ + public List getALLStocksByUser(String username) { + User user = getUserByUsername(username); + return stocksRepository.findStocksByUser_id(user.getId()); + } + + /** + * 指定されたユーザーの特定の在庫を取得する + * + * @param username ユーザー名 + * @param stockId 在庫ID + * @return 在庫エンティティ + * @throws RuntimeException 在庫が見つからない場合、または他のユーザーの在庫にアクセスしようとした場合 + */ + public Stocks getStockById(String username, Long stockId) { + User user = getUserByUsername(username); + return stocksRepository.findById(stockId) + .filter(stock -> stock.getUser_id().getId().equals(user.getId())) // ユーザーの在庫かどうかを確認 + .orElseThrow(() -> new RuntimeException(messageUtils.getMessage("error.stock.not.found"))); + } + + /** + * 指定された在庫情報を編集する + * + * @param username ユーザー名 + * @param stockId 変数する在庫ID + * @param stockDetails 編集内容(新しい情報) + * @return 編集された在庫エンティティ + */ + public Stocks updateStocks(String username, Long stockId, Stocks stockDetails) { + Stocks stock = getStockById(username, stockId); + stock.setAmount(stockDetails.getAmount()); + stock.setPrice(stockDetails.getPrice()); + stock.setLast_update(stockDetails.getLast_update()); + stock.setBuy_date(stockDetails.getBuy_date()); + stock.setExp_date(stockDetails.getExp_date()); + return stocksRepository.save(stock); + } + + /** + * 指定された在庫を削除する + * + * @param username ユーザー名 + * @param taskId 削除する在庫のID + */ + public void deleteStock(String username, Long stockId) { + Stocks stock = getStockById(username, stockId); + stocksRepository.delete(stock); + } + + + /** + * ユーザー名からユーザーエンティティを取得する + * + * @param username ユーザー名 + * @return ユーザーエンティティ + * @throws UsernameNotFoundException ユーザーが見つからない場合 + */ + private User getUserByUsername(String username) { + return userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException(messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[]{username}))); + } + +} From d3c0e5eaf312115ca32f735ba6a72c98848db6cb Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:27:11 +0900 Subject: [PATCH 02/32] create but function to tobuys --- .../todoapp/controller/ToBuysController.java | 10 +++++ .../com/example/todoapp/dto/ToBuysBuyDTO.java | 18 ++++++++ .../todoapp/service/ToBuysService.java | 42 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.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 7b81ea3..54f643c 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.dto.ToBuysBuyDTO; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; import com.example.todoapp.repository.UserRepository; @@ -137,4 +138,13 @@ public class ToBuysController { return ResponseEntity.ok(response); } + + @PostMapping("/{id}") + public ResponseEntity buyToBuys( + Authentication authentication, + @PathVariable("id") ToBuysBuyDTO toBuysbuyDTO) { + toBuysService.buyToBuys(authentication.getName(), toBuysbuyDTO); + return ResponseEntity.ok().build(); + } + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java new file mode 100644 index 0000000..fef1aac --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java @@ -0,0 +1,18 @@ +package com.example.todoapp.dto; + +import lombok.Data; +/** + * カテゴリDTOクラス + * このクラスはタスクのカテゴリ情報を表します。 + * カテゴリは名前、色、所有ユーザーなどの情報を持ちます。 + * + */ + + @Data + public class ToBuysBuyDTO { + private Long stuff_id; + private int amount; + private String shop; + private String stuff_name; + private String category; + } \ 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 3fcaee2..fd4d50e 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -8,10 +8,15 @@ package com.example.todoapp.service; +import com.example.todoapp.util.MessageUtils; + import com.example.todoapp.dto.ToBuysDTO; +import com.example.todoapp.dto.ToBuysBuyDTO; +import com.example.todoapp.model.Stocks; import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; +import com.example.todoapp.repository.StocksRepository; import com.example.todoapp.repository.StuffsRepository; import com.example.todoapp.repository.ToBuysRepository; import com.example.todoapp.repository.UserRepository; @@ -19,6 +24,7 @@ import com.example.todoapp.repository.UserRepository; import jakarta.transaction.Transactional; import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -44,6 +50,12 @@ public class ToBuysService { @Autowired private StuffsRepository stuffsRepository; + + @Autowired + private StuffsRepository stocksRepository; + + @Autowired + private MessageUtils messageUtils; /** @@ -151,4 +163,34 @@ public class ToBuysService { public int deleteToBuyByIds(Long userId, int tobuyId) { return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); } + + public Stocks buyToBuys(String username, ToBuysBuyDTO dto) { + // ユーザー情報を取得 + User user = getUserByUsername(username); + // 新しい在庫を作成 + Stocks stock = new Stocks(); + stock.setStuffs(dto.getStuff_id()); + stock.setUser_id(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()); + deleteToBuys(toBuyDTO); + // データベースに保存 + return stockRepository.save(stock); + } + + /** + * ユーザー名からユーザーエンティティを取得する + * + * @param username ユーザー名 + * @return ユーザーエンティティ + * @throws UsernameNotFoundException ユーザーが見つからない場合 + */ + private User getUserByUsername(String username) { + return userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException(messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[]{username}))); + } + } \ No newline at end of file From 0f522ad6283304aa20498e3799728ab9ec2e9de5 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:27:51 +0900 Subject: [PATCH 03/32] create dto for buy tobuy --- backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java index fef1aac..664a01c 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java @@ -10,7 +10,7 @@ import lombok.Data; @Data public class ToBuysBuyDTO { - private Long stuff_id; + private Long stuff_id; private int amount; private String shop; private String stuff_name; From db72254b5f47b419a21514d198583959b207ac73 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:31:50 +0900 Subject: [PATCH 04/32] fixed stock entity key --- backend/src/main/java/com/example/todoapp/dto/StockDTO.java | 4 ++-- backend/src/main/java/com/example/todoapp/model/Stocks.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java index 8363c3e..3f56af9 100644 --- a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java @@ -18,7 +18,7 @@ import java.time.LocalDate; @Data public class StockDTO { private int stock_id; - private Stuffs stuffs; + private int stuff_id; private Long user_id; private int amount; private int price; @@ -35,7 +35,7 @@ public class StockDTO { public static StockDTO fromEntity(Stocks stock) { StockDTO dto = new StockDTO(); dto.setStock_id(stock.getStock_id()); - dto.setStuffs(stock.getStuffs()); + dto.setStuff_id(stock.getStuff_id()); dto.setUser_id(stock.getUser_id().getId()); dto.setAmount(stock.getAmount()); dto.setPrice(stock.getPrice()); 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 d9f3f42..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 Stuffs stuffs; + private int stuff_id; /** From cc1efbf179672d166d8a28f27dd1125562a615c8 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:48:13 +0900 Subject: [PATCH 05/32] prepare to merge between dev-backend-stocks --- .../com/example/todoapp/dto/ToBuysBuyDTO.java | 12 ++++---- .../com/example/todoapp/dto/ToBuysDTO.java | 28 +++++++++++-------- .../com/example/todoapp/model/Stocks.java | 2 +- .../todoapp/service/ToBuysService.java | 6 ++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java index 664a01c..89a0fc1 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java @@ -1,5 +1,7 @@ package com.example.todoapp.dto; +import java.time.LocalDate; + import lombok.Data; /** * カテゴリDTOクラス @@ -10,9 +12,9 @@ import lombok.Data; @Data public class ToBuysBuyDTO { - private Long stuff_id; - private int amount; - private String shop; - private String stuff_name; - private String category; + private int stuff_id; + private int price; + private LocalDate exp_date; + private LocalDate last_update; + private LocalDate buy_date; } \ No newline at end of file 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 90830fd..fe6db25 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -1,6 +1,9 @@ package com.example.todoapp.dto; +import java.time.LocalDate; + import lombok.Data; + /** * カテゴリDTOクラス * このクラスはタスクのカテゴリ情報を表します。 @@ -8,14 +11,17 @@ import lombok.Data; * */ - @Data - public class ToBuysDTO { - private Long stuff_id; - private Long user_id; - - private int amount; - private String shop; - - private String stuff_name; - private String category; - } \ No newline at end of file +@Data +public class ToBuysDTO { + private int tobuy_id; + private Long stuff_id; + private Long user_id; + private int price; + private int amount; + private String shop; + private String stuff_name; + private String category; + private LocalDate exp_date; + private LocalDate last_update; + private LocalDate buy_date; +} \ No newline at end of file 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 d9f3f42..fb1b84b 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 Stuffs stuffs; + private Long stuff_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 fd4d50e..2484ecf 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -164,19 +164,19 @@ public class ToBuysService { return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); } - public Stocks buyToBuys(String username, ToBuysBuyDTO dto) { + public Stocks buyToBuys(String username, ToBuysDTO dto) { // ユーザー情報を取得 User user = getUserByUsername(username); // 新しい在庫を作成 Stocks stock = new Stocks(); - stock.setStuffs(dto.getStuff_id()); + stock.setStuff_id(dto.getStuff_id()); stock.setUser_id(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()); - deleteToBuys(toBuyDTO); + deleteToBuyByIds(dto.getUser_id(), dto.getTobuy_id()); // データベースに保存 return stockRepository.save(stock); } From 7f0c86bc3f395ce0bd1653f107e891f819cee619 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:49:57 +0900 Subject: [PATCH 06/32] prepare to merge between dev-backend-tobuys --- backend/src/main/java/com/example/todoapp/dto/StockDTO.java | 4 ++-- backend/src/main/java/com/example/todoapp/model/Stocks.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java index 3f56af9..5e96238 100644 --- a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java @@ -17,8 +17,8 @@ import java.time.LocalDate; @Data public class StockDTO { - private int stock_id; - private int stuff_id; + private Long stock_id; + private Long stuff_id; private Long user_id; private int amount; private int price; 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 4a13d3f..fcb13db 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,7 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int stock_id ; + private Long stock_id ; /** @@ -52,7 +52,7 @@ public class Stocks { referencedColumnName = "stuff_id", nullable = false ) - private int stuff_id; + private Long stuff_id; /** From f1126fb4d94543805845068a831901ecbeb41a0d Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:52:56 +0900 Subject: [PATCH 07/32] remove unused import --- backend/src/main/java/com/example/todoapp/dto/StockDTO.java | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java index 5e96238..7d54c84 100644 --- a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java @@ -1,7 +1,6 @@ package com.example.todoapp.dto; import com.example.todoapp.model.Stocks; -import com.example.todoapp.model.Stuffs; import lombok.Data; From e01724ace4ac9e37dfda157dbbafcfbbba7af284 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 14:54:22 +0900 Subject: [PATCH 08/32] remove tobuysbuydto --- .../todoapp/controller/ToBuysController.java | 5 ++--- .../com/example/todoapp/dto/ToBuysBuyDTO.java | 20 ------------------- 2 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.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 54f643c..96c36e1 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -10,7 +10,6 @@ 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.dto.ToBuysBuyDTO; import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.User; import com.example.todoapp.repository.UserRepository; @@ -142,8 +141,8 @@ public class ToBuysController { @PostMapping("/{id}") public ResponseEntity buyToBuys( Authentication authentication, - @PathVariable("id") ToBuysBuyDTO toBuysbuyDTO) { - toBuysService.buyToBuys(authentication.getName(), toBuysbuyDTO); + @PathVariable("id") ToBuysDTO toBuysDTO) { + toBuysService.buyToBuys(authentication.getName(), toBuysDTO); return ResponseEntity.ok().build(); } diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java b/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java deleted file mode 100644 index 89a0fc1..0000000 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysBuyDTO.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.example.todoapp.dto; - -import java.time.LocalDate; - -import lombok.Data; -/** - * カテゴリDTOクラス - * このクラスはタスクのカテゴリ情報を表します。 - * カテゴリは名前、色、所有ユーザーなどの情報を持ちます。 - * - */ - - @Data - public class ToBuysBuyDTO { - private int stuff_id; - private int price; - private LocalDate exp_date; - private LocalDate last_update; - private LocalDate buy_date; - } \ No newline at end of file From 664a071ef96480bb1da37fdd5fe5e9f7813bb19a Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 15:11:39 +0900 Subject: [PATCH 09/32] add stocksrepository.java by copy --- .../todoapp/repository/StocksRepository.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 backend/src/main/java/com/example/todoapp/repository/StocksRepository.java diff --git a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java new file mode 100644 index 0000000..10334d2 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -0,0 +1,50 @@ +//-------------------------------- +// ToBuysRepository.java +// +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.repository; + +import com.example.todoapp.model.Stocks; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + * 在庫エンティティのリポジトリインターフェース + *

+ * このインターフェースは在庫データへのアクセスと操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ + +@Repository +public interface StocksRepository extends JpaRepository { + /** + * user_idから在庫一覧をstock_id順で取得する + * + * @param user_id 検索するユーザーID + * @return 在庫リスト + */ + List findStocksByUser_id(Long user_id); + + /** + * 在庫情報を更新する + * + * @param stock 編集する新たな情報が入ったstockオブジェクト + * @return 編集に成功したらtrue + */ + boolean UpdateStockByStock_id(Stocks stock); + + /** + * 在庫リストから指定した食材を削除する + * + * @param stock_id 削除する在庫 + * @param user_id 削除するユーザー + * @return 削除した場合true + */ + boolean DeleteStockByStock_id(int stock_id, Long user_id); +} \ No newline at end of file From 5564f6e5073b8929b5943de21b61647f807c36d3 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 15:23:44 +0900 Subject: [PATCH 10/32] typo fix --- .../com/example/todoapp/repository/StocksRepository.java | 2 +- .../main/java/com/example/todoapp/service/ToBuysService.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) 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 10334d2..55811e2 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -1,5 +1,5 @@ //-------------------------------- -// ToBuysRepository.java +// StocksRepository.java // // // 更新履歴:2025/06/05 新規作成 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 2484ecf..a59c814 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -11,7 +11,6 @@ package com.example.todoapp.service; import com.example.todoapp.util.MessageUtils; import com.example.todoapp.dto.ToBuysDTO; -import com.example.todoapp.dto.ToBuysBuyDTO; import com.example.todoapp.model.Stocks; import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.ToBuys; @@ -52,7 +51,7 @@ public class ToBuysService { private StuffsRepository stuffsRepository; @Autowired - private StuffsRepository stocksRepository; + private StocksRepository stocksRepository; @Autowired private MessageUtils messageUtils; @@ -178,7 +177,7 @@ public class ToBuysService { stock.setExp_date(dto.getExp_date()); deleteToBuyByIds(dto.getUser_id(), dto.getTobuy_id()); // データベースに保存 - return stockRepository.save(stock); + return stocksRepository.save(stock); } /** From 99d34a0eedb4af505a41b1d7e46059d89270fd45 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 16:05:46 +0900 Subject: [PATCH 11/32] 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("材料がありません"); } From 184c3846cf701309d701cc59a46abba06d4731be Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Thu, 5 Jun 2025 16:14:33 +0900 Subject: [PATCH 12/32] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E5=87=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/todoapp/dto/ToBuysDTO.java | 2 +- .../com/example/todoapp/model/Stocks.java | 2 +- .../todoapp/service/ToBuysService.java | 19 +++++++++---------- 3 files changed, 11 insertions(+), 12 deletions(-) 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 649b912..fe6db25 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 Integer stuff_id; + private Long 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 4a13d3f..fb1b84b 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 int stuff_id; + private Long stuff_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 376c1c0..f34be5f 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -69,26 +69,25 @@ public class ToBuysService { User user = userRepository.findByUsername(username) .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); - Stuffs stuffs; + Stuffs stuff; if (toBuyDTO.getStuff_id() == null) { // 新しい材料を作成 - stuffs = new Stuffs(); - stuffs.setStuff_name(toBuyDTO.getStuff_name()); - stuffs.setCategory(toBuyDTO.getCategory()); - stuffs = stuffsRepository.save(stuffs); + stuff = new Stuffs(); + stuff.setStuff_name(toBuyDTO.getStuff_name()); + stuff.setCategory(toBuyDTO.getCategory()); + stuff = stuffsRepository.save(stuff); } else { // 材料情報を取得 - int stuff_id = toBuyDTO.getStuff_id(); - Optional optionalStuffs = stuffsRepository.findById(stuff_id); + Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); if (!optionalStuffs.isPresent()) { throw new RuntimeException("材料がありません"); } - stuffs = optionalStuffs.get(); + stuff = optionalStuffs.get(); } ToBuys toBuys = new ToBuys(); toBuys.setUser_id(user); - toBuys.setStuffs(stuffs); + toBuys.setStuff_id(stuff.getStuff_id()); toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setStore(toBuyDTO.getShop()); @@ -134,7 +133,7 @@ public class ToBuysService { ToBuys toBuys = new ToBuys(); toBuys.setUser_id(user); - toBuys.setStuffs(stuffs); + toBuys.setStuff_id(stuffs.getStuff_id()); toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setStore(toBuyDTO.getShop()); From 067c5783e7be76d2160832f4ab1c66e57752b8e3 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Thu, 5 Jun 2025 17:01:10 +0900 Subject: [PATCH 13/32] stuff int -> long --- .../src/main/java/com/example/todoapp/dto/ToBuyResponse.java | 2 +- backend/src/main/java/com/example/todoapp/model/Stocks.java | 2 +- backend/src/main/java/com/example/todoapp/model/Stuffs.java | 2 +- backend/src/main/java/com/example/todoapp/model/ToBuys.java | 2 +- .../java/com/example/todoapp/repository/StuffsRepository.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) 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 3968169..bf5730b 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 int stuff_id; + private Long stuff_id; private String stuff_name; private int amount; private String shop; 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..fcb13db 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,7 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int stock_id ; + private Long stock_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 fc69e4e..55fe1f9 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 int stuff_id ; + private Long 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 7c46c1a..2fed2ca 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 int stuff_id; + private Long 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 fa6e952..7b37ae1 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(int stuff_id); + Stuffs findByStuff_id(Long stuff_id); } \ No newline at end of file From a86180989b819d3eae496b6dad54ec1c89fc2a35 Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Fri, 6 Jun 2025 08:58:38 +0900 Subject: [PATCH 14/32] =?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); } From c0c025dc40bacf2cd8ae1008dd1365bc27de7a15 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Fri, 6 Jun 2025 11:01:23 +0900 Subject: [PATCH 15/32] build-passed --- .../todoapp/controller/ToBuysController.java | 4 ++-- .../example/todoapp/dto/ToBuyResponse.java | 2 +- .../com/example/todoapp/dto/ToBuysDTO.java | 8 +++---- .../example/todoapp/model/RecipeStuffs.java | 8 +++---- .../com/example/todoapp/model/Recipes.java | 4 ++-- .../com/example/todoapp/model/Stocks.java | 14 +++++------ .../com/example/todoapp/model/Stuffs.java | 4 ++-- .../com/example/todoapp/model/ToBuys.java | 10 ++++---- .../todoapp/repository/StocksRepository.java | 24 ++++++++++++++----- .../todoapp/repository/StuffsRepository.java | 2 +- .../todoapp/repository/ToBuysRepository.java | 8 +++---- .../todoapp/service/ToBuysService.java | 16 ++++++------- 12 files changed, 58 insertions(+), 46 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 ca72bf5..196f8a8 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -32,7 +32,7 @@ import org.springframework.web.bind.annotation.*; /** * 購入リストに関するRESTコントローラー *

- * このコントローラーは、購入リスト (to_buys) へのアイテム追加機能を提供します。 + * このコントローラーは、購入リスト (toBuys) へのアイテム追加機能を提供します。 * リクエストボディには ToBuyDTO 形式のデータが期待されます。 *

*/ @@ -99,7 +99,7 @@ public class ToBuysController { Stuffs stuff = toBuy.getStuff(); resp.setTobuyId(toBuy.getTobuyId()); resp.setStuffId(stuff.getStuffId()); - resp.setStuff_name(stuff.getStuffName()); + resp.setStuffName(stuff.getStuffName()); 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 8d1a992..e4a677a 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -6,7 +6,7 @@ import lombok.Data; public class ToBuyResponse { private int tobuyId; private Long stuffId; - private String stuff_name; + private String stuffName; private int amount; private String shop; } \ No newline at end of file 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 1bdee7f..111ccad 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -19,9 +19,9 @@ public class ToBuysDTO { private int price; private int amount; private String shop; - private String stuff_name; + private String stuffName; private String category; - private LocalDate exp_date; - private LocalDate last_update; - private LocalDate buy_date; + 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/model/RecipeStuffs.java b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java index 0d911ef..efd9017 100644 --- a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java @@ -46,8 +46,8 @@ public class RecipeStuffs { @NotBlank @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( - name = "recipe_id", - referencedColumnName = "recipe_id", + name = "recipeId", + referencedColumnName = "recipeId", nullable = false ) private Recipes recipes; @@ -59,8 +59,8 @@ public class RecipeStuffs { @NotBlank @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( - name = "stuff_id", - referencedColumnName = "stuff_id", + name = "stuffId", + referencedColumnName = "stuffId", nullable = false ) 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 cdaa809..25eaaad 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,14 @@ public class Recipes { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="recipe_id") + @Column(name="recipeId") private int recipeId ; /** * カテゴリ名 */ @NotNull - @Column(name="recipe_name", unique = true, length = 255, nullable = false) + @Column(name="recipeName", 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 398cf2b..8c7eca6 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,7 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="stock_id") + @Column(name="stockId") private Long stockId ; @@ -49,8 +49,8 @@ public class Stocks { @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( - name = "stuff_id", - referencedColumnName = "stuff_id", + name = "stuffId", + referencedColumnName = "stuffId", nullable = false ) private Stuffs stuff; @@ -62,7 +62,7 @@ public class Stocks { @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( - name = "user_id", + name = "userId", referencedColumnName = "id", nullable = false ) @@ -84,18 +84,18 @@ public class Stocks { * 購入日 */ @Column(nullable = false) - private LocalDate buy_date; + private LocalDate buyDate; /** * 最後の操作時間 */ @Column(nullable = false) - private LocalDate last_update; + private LocalDate lastUpdate; /** * 賞味期限 */ @Column(nullable = false) - private LocalDate exp_date; + private LocalDate expDate; } \ No newline at end of file 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 2b164ad..14fb111 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,14 @@ import lombok.NoArgsConstructor; */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "stuff_id") + @Column(name = "stuffId") private Long stuffId; /** * カテゴリ名 */ @NotNull - @Column(name = "stuff_name", unique = true, length = 255, nullable = false) + @Column(name = "stuffName", 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 d02faa4..e000516 100644 --- a/backend/src/main/java/com/example/todoapp/model/ToBuys.java +++ b/backend/src/main/java/com/example/todoapp/model/ToBuys.java @@ -28,7 +28,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @Entity -@Table(name = "to_buys") +@Table(name = "toBuys") public class ToBuys { /** @@ -36,7 +36,7 @@ public class ToBuys { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "tobuy_id") + @Column(name = "tobuyId") private int tobuyId ; /** @@ -45,8 +45,8 @@ public class ToBuys { @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( - name = "stuff_id", - referencedColumnName = "stuff_id", + name = "stuffId", + referencedColumnName = "stuffId", nullable = false ) private Stuffs stuff; @@ -61,7 +61,7 @@ public class ToBuys { * 多対一の関係で、遅延ロードを使用 */ @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) + @JoinColumn(name = "userId", 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 3fd0855..35b2dd4 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -8,7 +8,13 @@ package com.example.todoapp.repository; import com.example.todoapp.model.Stocks; + +import jakarta.transaction.Transactional; + import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -24,12 +30,12 @@ import java.util.List; @Repository public interface StocksRepository extends JpaRepository { /** - * user_idから在庫一覧をstock_id順で取得する + * userIdから在庫一覧をstockId順で取得する * - * @param user_id 検索するユーザーID + * @param userId 検索するユーザーID * @return 在庫リスト */ - List findStocksByUser_id(Long user_id); + List findStocksByUserId(Long userId); /** * 在庫情報を更新する @@ -37,14 +43,20 @@ public interface StocksRepository extends JpaRepository { * @param stock 編集する新たな情報が入ったstockオブジェクト * @return 編集に成功したらtrue */ - boolean UpdateStockByStock_id(Stocks stock); + // boolean updateStocksByStockId(Stocks stock); + + //updateをクエリ作成にて実装 + + @Modifying + @Transactional + @Query("UPDATE Stocks s SET s.amount = :#{#stock.amount}, s.price = :#{#stock.price}, s.buyDate = :#{#stock.buyDate}, s.lastUpdate = :#{#stock.lastUpdate}, s.expDate = :#{#stock.expDate} WHERE s.stockId = :#{#stock.stockId}") + int updateStocksById(@Param("stock") Stocks stock); /** * 在庫リストから指定した食材を削除する * * @param stockId 削除する在庫 * @param userId 削除するユーザー - * @return 削除した場合true */ - boolean DeleteStockByStock_id(int stockId, Long userId); + void deleteStocksByStockIdAndUserId(Long 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 82d3137..6edbc88 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 findByStuffId(Long stuff_id); + Stuffs findByStuffId(Long stuffId); } \ 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 89f0d7d..c208db9 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -18,9 +18,9 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; /** - * to_buys テーブルへのアクセスを行うリポジトリ + * toBuys テーブルへのアクセスを行うリポジトリ *

- * このクラスは to_buys テーブルに対する基本的なCRUD操作を提供します。 + * このクラスは toBuys テーブルに対する基本的なCRUD操作を提供します。 * Spring Data JPAによって自動的に実装されます。 *

*/ @@ -34,7 +34,7 @@ public interface ToBuysRepository extends JpaRepository { * @return 「買うもの」リスト */ @Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1") - List findByUser(Long user_id); + List findByUser(Long userId); /** * 指定されたユーザーIDに基づいて「買うもの」リストを取得する @@ -45,5 +45,5 @@ public interface ToBuysRepository extends JpaRepository { */ @Modifying @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); + int deleteByUserIdAndTobuyId(@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 0d91e1f..41847d9 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -33,7 +33,7 @@ import java.util.Optional; /** * 購入リストのサービスクラス *

- * このクラスは、購入リスト (to_buys) の登録処理を提供します。 + * このクラスは、購入リスト (toBuys) の登録処理を提供します。 * 材料 (stuffs) の存在確認と新規作成、ユーザー情報の取得などを行います。 *

*/ @@ -73,7 +73,7 @@ public class ToBuysService { if (toBuyDTO.getStuffId() == null) { // 新しい材料を作成 stuff = new Stuffs(); - stuff.setStuffName(toBuyDTO.getStuff_name()); + stuff.setStuffName(toBuyDTO.getStuffName()); stuff.setCategory(toBuyDTO.getCategory()); stuff = stuffsRepository.save(stuff); } else { @@ -112,7 +112,7 @@ public class ToBuysService { if (toBuyDTO.getStuffId() == null) { // 新しい材料を作成 stuffs = new Stuffs(); - stuffs.setStuffName(toBuyDTO.getStuff_name()); + stuffs.setStuffName(toBuyDTO.getStuffName()); stuffs.setCategory(toBuyDTO.getCategory()); stuffs = stuffsRepository.save(stuffs); } else { @@ -124,7 +124,7 @@ public class ToBuysService { stuffs = optionalStuffs.get(); //update - // stuffs.setStuff_name(toBuyDTO.getStuff_name()); + // stuffs.setStuffName(toBuyDTO.getStuffName()); // stuffs.setCategory(toBuyDTO.getCategory()); stuffs = stuffsRepository.save(stuffs); } @@ -160,7 +160,7 @@ public class ToBuysService { */ @Transactional public int deleteToBuyByIds(Long userId, int tobuyId) { - return toBuysRepository.deleteByUser_IdAndTobuyId(userId, tobuyId); + return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); } public Stocks buyToBuys(String username, ToBuysDTO dto) { @@ -174,9 +174,9 @@ public class ToBuysService { 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()); + stock.setLastUpdate(dto.getLastUpdate()); + stock.setBuyDate(dto.getBuyDate()); + stock.setExpDate(dto.getExpDate()); deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); // データベースに保存 return stocksRepository.save(stock); From 1df241c8656b4c5d535723e0833202fd3b17f3fd Mon Sep 17 00:00:00 2001 From: Amagasu Date: Fri, 6 Jun 2025 13:15:50 +0900 Subject: [PATCH 16/32] varname _ remove, id type int -> long --- .../todoapp/dto/DeleteToBuyRequest.java | 2 +- .../com/example/todoapp/dto/StockDTO.java | 24 +++++++-------- .../example/todoapp/dto/ToBuyResponse.java | 2 +- .../com/example/todoapp/dto/ToBuysDTO.java | 2 +- .../example/todoapp/model/RecipeStuffs.java | 2 +- .../com/example/todoapp/model/Recipes.java | 2 +- .../com/example/todoapp/model/Stocks.java | 29 ++++++++++++------- .../com/example/todoapp/model/ToBuys.java | 2 +- .../todoapp/repository/ToBuysRepository.java | 2 +- .../todoapp/service/StocksService.java | 12 ++++---- .../todoapp/service/ToBuysService.java | 2 +- 11 files changed, 45 insertions(+), 36 deletions(-) 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 b8f7b9e..07b2a37 100644 --- a/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java +++ b/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java @@ -5,5 +5,5 @@ import lombok.Data; @Data public class DeleteToBuyRequest { private Long userId; - private int tobuyId; + private Long tobuyId; } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java index 7d54c84..284b134 100644 --- a/backend/src/main/java/com/example/todoapp/dto/StockDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/StockDTO.java @@ -16,14 +16,14 @@ import java.time.LocalDate; @Data public class StockDTO { - private Long stock_id; - private Long stuff_id; - private Long user_id; + private Long stockId; + private Long stuffId; + private Long userId; private int amount; private int price; - private LocalDate buy_date; - private LocalDate last_update; - private LocalDate exp_date; + private LocalDate buyDate; + private LocalDate lastUpdate; + private LocalDate expDate; /** * 在庫エンティティからDTOを作成する @@ -33,14 +33,14 @@ public class StockDTO { */ public static StockDTO fromEntity(Stocks stock) { StockDTO dto = new StockDTO(); - dto.setStock_id(stock.getStock_id()); - dto.setStuff_id(stock.getStuff_id()); - dto.setUser_id(stock.getUser_id().getId()); + dto.setStockId(stock.getStockId()); + dto.setStuffId(stock.getStuff().getStuffId()); + dto.setUserId(stock.getUser().getId()); dto.setAmount(stock.getAmount()); dto.setPrice(stock.getPrice()); - dto.setBuy_date(stock.getBuy_date()); - dto.setLast_update(stock.getLast_update()); - dto.setExp_date(stock.getExp_date()); + dto.setBuyDate(stock.getBuyDate()); + dto.setLastUpdate(stock.getLastUpdate()); + dto.setExpDate(stock.getExpDate()); return dto; } } \ 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 e4a677a..407ae91 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -4,7 +4,7 @@ import lombok.Data; @Data public class ToBuyResponse { - private int tobuyId; + private Long tobuyId; private Long stuffId; private String stuffName; private int amount; 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 111ccad..fd4a6ff 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java @@ -13,7 +13,7 @@ import lombok.Data; @Data public class ToBuysDTO { - private int tobuyId; + private Long tobuyId; private Long stuffId; private Long userId; private int price; 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 efd9017..0855330 100644 --- a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java @@ -38,7 +38,7 @@ public class RecipeStuffs { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="recipe_stuffs_id") - private int recipeStuffsId ; + private Long recipeStuffsId ; /** * 料理の一意識別子 FK 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 25eaaad..c4a986d 100644 --- a/backend/src/main/java/com/example/todoapp/model/Recipes.java +++ b/backend/src/main/java/com/example/todoapp/model/Recipes.java @@ -36,7 +36,7 @@ public class Recipes { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="recipeId") - private int recipeId ; + private Long recipeId ; /** * カテゴリ名 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 1b5b067..1b8ede7 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,7 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long stockI ; + private Long stockId; /** @@ -55,18 +55,27 @@ public class Stocks { private Stuffs stuff; - /** - * ユーザーテーブル参照用の外部キー + // /** + // * ユーザーテーブル参照用の外部キー + // */ + // @NotNull + // @ManyToOne(fetch = FetchType.LAZY) + // @JoinColumn( + // name = "userId", + // referencedColumnName = "id", + // nullable = false + // ) + // private User user; + + /** + * タスクの所有者(ユーザー) + * 多対一の関係で、遅延ロードを使用 */ - @NotNull - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn( - name = "userId", - referencedColumnName = "id", - nullable = false - ) + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "userId", nullable = false) private User user; + /** * 在庫数量(デフォルト値: 1) */ 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 e000516..6ab9052 100644 --- a/backend/src/main/java/com/example/todoapp/model/ToBuys.java +++ b/backend/src/main/java/com/example/todoapp/model/ToBuys.java @@ -37,7 +37,7 @@ public class ToBuys { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "tobuyId") - private int tobuyId ; + private Long tobuyId ; /** * 材料の一意識別子 FK 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 c208db9..ea45de7 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -45,5 +45,5 @@ public interface ToBuysRepository extends JpaRepository { */ @Modifying @Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId") - int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId); + int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") Long tobuyId); } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/StocksService.java b/backend/src/main/java/com/example/todoapp/service/StocksService.java index 85580d2..07dad3c 100644 --- a/backend/src/main/java/com/example/todoapp/service/StocksService.java +++ b/backend/src/main/java/com/example/todoapp/service/StocksService.java @@ -37,7 +37,7 @@ public class StocksService { */ public Stocks createStock(String username, Stocks stock) { User user = getUserByUsername(username); - stock.setUser_id(user); + stock.setUser(user); return stocksRepository.save(stock); } @@ -49,7 +49,7 @@ public class StocksService { */ public List getALLStocksByUser(String username) { User user = getUserByUsername(username); - return stocksRepository.findStocksByUser_id(user.getId()); + return stocksRepository.findStocksByUserId(user.getId()); } /** @@ -63,7 +63,7 @@ public class StocksService { public Stocks getStockById(String username, Long stockId) { User user = getUserByUsername(username); return stocksRepository.findById(stockId) - .filter(stock -> stock.getUser_id().getId().equals(user.getId())) // ユーザーの在庫かどうかを確認 + .filter(stock -> stock.getUser().equals(user)) // ユーザーの在庫かどうかを確認 .orElseThrow(() -> new RuntimeException(messageUtils.getMessage("error.stock.not.found"))); } @@ -79,9 +79,9 @@ public class StocksService { Stocks stock = getStockById(username, stockId); stock.setAmount(stockDetails.getAmount()); stock.setPrice(stockDetails.getPrice()); - stock.setLast_update(stockDetails.getLast_update()); - stock.setBuy_date(stockDetails.getBuy_date()); - stock.setExp_date(stockDetails.getExp_date()); + stock.setLastUpdate(stockDetails.getLastUpdate()); + stock.setBuyDate(stockDetails.getBuyDate()); + stock.setExpDate(stockDetails.getExpDate()); return stocksRepository.save(stock); } 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 41847d9..0908e01 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -159,7 +159,7 @@ public class ToBuysService { * @param tobuyId 購入リストID */ @Transactional - public int deleteToBuyByIds(Long userId, int tobuyId) { + public int deleteToBuyByIds(Long userId, Long tobuyId) { return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); } From 33428779d5b38853d223b7bb9772806082c1b84d Mon Sep 17 00:00:00 2001 From: Amagasu Date: Mon, 9 Jun 2025 11:21:34 +0900 Subject: [PATCH 17/32] recipestuff default change --- .../src/main/java/com/example/todoapp/model/RecipeStuffs.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 0855330..5c3a933 100644 --- a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java @@ -66,10 +66,10 @@ public class RecipeStuffs { private Stuffs stuff; /** - * 材料の数量 + * 材料の数量(デフォルト1) */ @NotBlank @Column(nullable = false) - private int amount; + private int amount = 1; } From 527194d5b7bde9973edba94bee4d610bbb7514ee Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 13:03:35 +0900 Subject: [PATCH 18/32] add vscode backend debugger --- .vscode/launch.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..73a1f7d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch Spring Boot App (local profile)", + "request": "launch", + "cwd": "${workspaceFolder}/backend", + "mainClass": "com.example.todoapp.TodoApplication", + "projectName": "todo-app", + "args": "", + "envFile": "${workspaceFolder}/backend/.env", + "vmArgs": "-Dspring.profiles.active=local" + } + ] +} \ No newline at end of file From b048eb01d5ad9db160dd13b139a1b8704413e17f Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 13:03:59 +0900 Subject: [PATCH 19/32] change to camelcase in frontend --- frontend/src/pages/StockPage.tsx | 60 ++++++++-------- frontend/src/pages/TaskListPage.tsx | 32 ++++----- frontend/src/services/api.ts | 104 ++++++++++++++-------------- frontend/src/types/types.ts | 22 +++--- 4 files changed, 109 insertions(+), 109 deletions(-) diff --git a/frontend/src/pages/StockPage.tsx b/frontend/src/pages/StockPage.tsx index ecf3059..67103b5 100644 --- a/frontend/src/pages/StockPage.tsx +++ b/frontend/src/pages/StockPage.tsx @@ -139,14 +139,14 @@ const StockPage: React.FC = () => { .filter(stock => stock.category == "乳製品") // 乳製品だけ抽出 .map(stock => ( handleRowClick(stock.stock_id)} - style={{ backgroundColor: selectedRow === stock.stock_id ? "yellow" : "white", cursor: "pointer" }}> - {stock.stuff_name} + key={stock.stockId} + onClick={() => handleRowClick(stock.stockId)} + style={{ backgroundColor: selectedRow === stock.stockId ? "yellow" : "white", cursor: "pointer" }}> + {stock.stuffName} {stock.amount} {stock.price} - {formatDate(stock.exp_date)} - {formatDate(stock.buy_date)} + {formatDate(stock.expDate)} + {formatDate(stock.buyDate)} ))} @@ -178,14 +178,14 @@ const StockPage: React.FC = () => { .filter(stock => stock.category == "肉" || stock.category == "魚") // 肉と魚だけ抽出 .map(stock => ( handleRowClick(stock.stock_id)} - style={{ backgroundColor: selectedRow === stock.stock_id ? "yellow" : "white", cursor: "pointer" }}> - {stock.stuff_name} + key={stock.stockId} + onClick={() => handleRowClick(stock.stockId)} + style={{ backgroundColor: selectedRow === stock.stockId ? "yellow" : "white", cursor: "pointer" }}> + {stock.stuffName} {stock.amount} {stock.price} - {formatDate(stock.exp_date)} - {formatDate(stock.buy_date)} + {formatDate(stock.expDate)} + {formatDate(stock.buyDate)} ))} @@ -217,14 +217,14 @@ const StockPage: React.FC = () => { .filter(stock => stock.category == "野菜") // 野菜だけ抽出 .map(stock => ( handleRowClick(stock.stock_id)} - style={{ backgroundColor: selectedRow === stock.stock_id ? "yellow" : "white", cursor: "pointer" }}> - {stock.stuff_name} + key={stock.stockId} + onClick={() => handleRowClick(stock.stockId)} + style={{ backgroundColor: selectedRow === stock.stockId ? "yellow" : "white", cursor: "pointer" }}> + {stock.stuffName} {stock.amount} {stock.price} - {formatDate(stock.exp_date)} - {formatDate(stock.buy_date)} + {formatDate(stock.expDate)} + {formatDate(stock.buyDate)} ))} @@ -255,14 +255,14 @@ const StockPage: React.FC = () => { .filter(stock => stock.category == "調味料") // 調味料だけ抽出 .map(stock => ( handleRowClick(stock.stock_id)} - style={{ backgroundColor: selectedRow === stock.stock_id ? "yellow" : "white", cursor: "pointer" }}> - {stock.stuff_name} + key={stock.stockId} + onClick={() => handleRowClick(stock.stockId)} + style={{ backgroundColor: selectedRow === stock.stockId ? "yellow" : "white", cursor: "pointer" }}> + {stock.stuffName} {stock.amount} {stock.price} - {formatDate(stock.exp_date)} - {formatDate(stock.buy_date)} + {formatDate(stock.expDate)} + {formatDate(stock.buyDate)} ))} @@ -293,14 +293,14 @@ const StockPage: React.FC = () => { .filter(stock => stock.category == "その他") // その他だけ抽出 .map(stock => ( handleRowClick(stock.stock_id)} - style={{ backgroundColor: selectedRow === stock.stock_id ? "yellow" : "white", cursor: "pointer" }}> - {stock.stuff_name} + key={stock.stockId} + onClick={() => handleRowClick(stock.stockId)} + style={{ backgroundColor: selectedRow === stock.stockId ? "yellow" : "white", cursor: "pointer" }}> + {stock.stuffName} {stock.amount} {stock.price} - {formatDate(stock.exp_date)} - {formatDate(stock.buy_date)} + {formatDate(stock.expDate)} + {formatDate(stock.buyDate)} ))} diff --git a/frontend/src/pages/TaskListPage.tsx b/frontend/src/pages/TaskListPage.tsx index cc97ec7..340381d 100644 --- a/frontend/src/pages/TaskListPage.tsx +++ b/frontend/src/pages/TaskListPage.tsx @@ -40,9 +40,9 @@ import { TASK_ERRORS } from '../constants/errorMessages'; // 新規タスクの初期状態 -const EMPTY_TASK: Omit & { stuff_id: number | null, category: string } & { newAddition: boolean } = { - stuff_id: null, - stuff_name: '', +const EMPTY_TASK: Omit & { stuffId: number | null, category: string } & { newAddition: boolean } = { + stuffId: null, + stuffName: '', amount: 0, shop: '', category: '', @@ -58,7 +58,7 @@ const TaskListPage: React.FC = () => { //在庫登録ダイアログの表示状態 const [openInfoDialog, setOpenInfoDialog] = useState(false); - const [selectedTask, setSelectedTask] = useState(0); + const [selectedTask, setSelectedTask] = useState(0); const [newToBuy, setNewToBuy] = useState(EMPTY_TASK); @@ -126,7 +126,7 @@ const TaskListPage: React.FC = () => { const handleCreateTask = async () => { try { if (newToBuy.newAddition) { - newToBuy.stuff_id = null; + newToBuy.stuffId = null; } console.log(newToBuy) await toBuyApi.addToBuy(newToBuy); @@ -149,7 +149,7 @@ const TaskListPage: React.FC = () => { {/* タスク一覧をマップして各タスクをリストアイテムとして表示 */} {tobuys && tobuys.map((tobuy) => ( { */} {/* タスクのタイトルと説明 - 完了状態に応じて取り消し線を表示 */} { aria-label="食材情報追加" onClick={() => { setOpenInfoDialog(true) - setSelectedTask(tobuy.tobuy_id) - // handleDeleteTask(tobuy.tobuy_id) + setSelectedTask(tobuy.tobuyId) + // handleDeleteTask(tobuy.tobuyId) }} > @@ -205,7 +205,7 @@ const TaskListPage: React.FC = () => { handleDeleteTask(tobuy.tobuy_id)} + onClick={() => handleDeleteTask(tobuy.tobuyId)} > @@ -273,12 +273,12 @@ const TaskListPage: React.FC = () => { 材料名(選択) @@ -290,8 +290,8 @@ const TaskListPage: React.FC = () => { margin="dense" label="材料名" fullWidth - value={newToBuy.stuff_name} - onChange={(e) => setNewToBuy({ ...newToBuy, stuff_name: e.target.value })} + value={newToBuy.stuffName} + onChange={(e) => setNewToBuy({ ...newToBuy, stuffName: e.target.value })} sx={{ marginBottom: 2 }} />} {/* 数量入力フィールド */} diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index d7423fc..d360657 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -107,16 +107,16 @@ export const toBuyApi = { // return { // "tobuy_array": [ // { - // "tobuy_id": 1, - // "stuff_id": 2, - // "stuff_name": "じゃがいも", + // "tobuyId": 1, + // "stuffId": 2, + // "stuffName": "じゃがいも", // "amount": 3, // "shop": "shopXXX" // }, // { - // "tobuy_id": 2, - // "stuff_id": 5, - // "stuff_name": "にんじん", + // "tobuyId": 2, + // "stuffId": 5, + // "stuffName": "にんじん", // "amount": 1 // } // ] @@ -129,7 +129,7 @@ export const toBuyApi = { * @param tobuy 作成する材料情報 * @returns 作成された材料情報 */ - addToBuy: async (tobuy: Omit & { stuff_id: number | null, category: string }): Promise => { + addToBuy: async (tobuy: Omit & { stuffId: number | null, category: string }): Promise => { const response = await fetch(`${API_BASE_URL}/api/tobuy/add`, { method: 'POST', headers: getHeaders(), @@ -145,8 +145,8 @@ export const toBuyApi = { // return { // "result": true, - // "tobuy_id": 1, - // "stuff_id": 6, + // "tobuyId": 1, + // "stuffId": 6, // "message": "追加に成功しました", // } @@ -156,11 +156,11 @@ export const toBuyApi = { * 買うものリストを削除 * @param id 削除対象の買うものリストID */ - deleteToBuy: async (tobuy_id: number): Promise<{ result: boolean }> => { + deleteToBuy: async (tobuyId: number): Promise<{ result: boolean }> => { const response = await fetch(`${API_BASE_URL}/api/tobuy/delete`, { method: 'DELETE', headers: getHeaders(), - body: JSON.stringify({tobuy_id}), + body: JSON.stringify({tobuyId}), }); if (!response.ok) { @@ -178,35 +178,35 @@ export const toBuyApi = { export const stuffApi = { getStuffs: async (category: string): Promise<{ stuff_array: Stuff[] }> => { const data = [ - { "stuff_id": 1, "stuff_name": "牛乳", "category": "乳製品" }, - { "stuff_id": 2, "stuff_name": "ヨーグルト", "category": "乳製品" }, - { "stuff_id": 3, "stuff_name": "チーズ", "category": "乳製品" }, - { "stuff_id": 4, "stuff_name": "バター", "category": "乳製品" }, - { "stuff_id": 5, "stuff_name": "生クリーム", "category": "乳製品" }, - - { "stuff_id": 6, "stuff_name": "鮭", "category": "魚・肉" }, - { "stuff_id": 7, "stuff_name": "鶏むね肉", "category": "魚・肉" }, - { "stuff_id": 8, "stuff_name": "豚バラ肉", "category": "魚・肉" }, - { "stuff_id": 9, "stuff_name": "牛ひき肉", "category": "魚・肉" }, - { "stuff_id": 10, "stuff_name": "まぐろ", "category": "魚・肉" }, - - { "stuff_id": 11, "stuff_name": "にんじん", "category": "野菜" }, - { "stuff_id": 12, "stuff_name": "キャベツ", "category": "野菜" }, - { "stuff_id": 13, "stuff_name": "ほうれん草", "category": "野菜" }, - { "stuff_id": 14, "stuff_name": "玉ねぎ", "category": "野菜" }, - { "stuff_id": 15, "stuff_name": "ピーマン", "category": "野菜" }, - - { "stuff_id": 16, "stuff_name": "醤油", "category": "調味料" }, - { "stuff_id": 17, "stuff_name": "味噌", "category": "調味料" }, - { "stuff_id": 18, "stuff_name": "塩", "category": "調味料" }, - { "stuff_id": 19, "stuff_name": "砂糖", "category": "調味料" }, - { "stuff_id": 20, "stuff_name": "酢", "category": "調味料" }, - - { "stuff_id": 21, "stuff_name": "米", "category": "その他" }, - { "stuff_id": 22, "stuff_name": "パスタ", "category": "その他" }, - { "stuff_id": 23, "stuff_name": "小麦粉", "category": "その他" }, - { "stuff_id": 24, "stuff_name": "卵", "category": "その他" }, - { "stuff_id": 25, "stuff_name": "豆腐", "category": "その他" } + { stuffId: 1, stuffName: "牛乳", category: "乳製品" }, + { stuffId: 2, stuffName: "ヨーグルト", category: "乳製品" }, + { stuffId: 3, stuffName: "チーズ", category: "乳製品" }, + { stuffId: 4, stuffName: "バター", category: "乳製品" }, + { stuffId: 5, stuffName: "生クリーム", category: "乳製品" }, + + { stuffId: 6, stuffName: "鮭", category: "魚・肉" }, + { stuffId: 7, stuffName: "鶏むね肉", category: "魚・肉" }, + { stuffId: 8, stuffName: "豚バラ肉", category: "魚・肉" }, + { stuffId: 9, stuffName: "牛ひき肉", category: "魚・肉" }, + { stuffId: 10, stuffName: "まぐろ", category: "魚・肉" }, + + { stuffId: 11, stuffName: "にんじん", category: "野菜" }, + { stuffId: 12, stuffName: "キャベツ", category: "野菜" }, + { stuffId: 13, stuffName: "ほうれん草", category: "野菜" }, + { stuffId: 14, stuffName: "玉ねぎ", category: "野菜" }, + { stuffId: 15, stuffName: "ピーマン", category: "野菜" }, + + { stuffId: 16, stuffName: "醤油", category: "調味料" }, + { stuffId: 17, stuffName: "味噌", category: "調味料" }, + { stuffId: 18, stuffName: "塩", category: "調味料" }, + { stuffId: 19, stuffName: "砂糖", category: "調味料" }, + { stuffId: 20, stuffName: "酢", category: "調味料" }, + + { stuffId: 21, stuffName: "米", category: "その他" }, + { stuffId: 22, stuffName: "パスタ", category: "その他" }, + { stuffId: 23, stuffName: "小麦粉", category: "その他" }, + { stuffId: 24, stuffName: "卵", category: "その他" }, + { stuffId: 25, stuffName: "豆腐", category: "その他" } ] const filtered = data.filter(stuff => stuff.category == category) @@ -237,25 +237,25 @@ export const stockApi = { return { "stock_array": [ { - "stock_id": 1, - "stuff_id": 10, - "stuff_name": "豚肉", + "stockId": 1, + "stuffId": 10, + "stuffName": "豚肉", "amount": 100, "price": 200, - "buy_date": "2025-05-18T09:00:00.000Z", - "last_update": "2025-05-18T09:00:00.000Z", - "exp_date": "2025-05-19T10:15:00.000Z", + "buyDate": "2025-05-18T09:00:00.000Z", + "lastUpdate": "2025-05-18T09:00:00.000Z", + "expDate": "2025-05-19T10:15:00.000Z", "category": "肉" }, { - "stock_id": 2, - "stuff_id": 1, - "stuff_name": "トマト", + "stockId": 2, + "stuffId": 1, + "stuffName": "トマト", "amount": 10, "price": 200, - "buy_date": "2025-05-18T09:00:00.000Z", - "last_update": "2025-05-18T09:00:00.000Z", - "exp_date": "2025-05-19T10:15:00.000Z", + "buyDate": "2025-05-18T09:00:00.000Z", + "lastUpdate": "2025-05-18T09:00:00.000Z", + "expDate": "2025-05-19T10:15:00.000Z", "category": "野菜" } ] diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index 76c645d..1a93f80 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -22,16 +22,16 @@ export interface Task { * タスクの基本情報と状態を管理 */ export interface ToBuy { - tobuy_id: number, - stuff_id: number, - stuff_name: string, + tobuyId: number, + stuffId: number, + stuffName: string, amount: number, shop?: string, } export interface Stuff { - stuff_id: number, - stuff_name: string, + stuffId: number, + stuffName: string, category: string, } @@ -40,14 +40,14 @@ export interface Stuff { * タスクの基本情報と状態を管理 */ export interface Stock { - stock_id: number, - stuff_id: number, - stuff_name: string, + stockId: number, + stuffId: number, + stuffName: string, amount: number, price: number, - buy_date: string, - last_update: string, - exp_date: string, + buyDate: string, + lastUpdate: string, + expDate: string, category: string, } From 46963ed46c216aac4a53aa3e5a7516acf4f1c5f8 Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 13:25:42 +0900 Subject: [PATCH 20/32] =?UTF-8?q?=E9=85=8D=E5=88=97=E3=82=92=E7=94=9F?= =?UTF-8?q?=E3=81=A7=E8=BF=94=E3=81=99=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 4 +- frontend/src/pages/StockPage.tsx | 2 +- frontend/src/pages/TaskListPage.tsx | 4 +- frontend/src/services/api.ts | 82 +++++++------------ 4 files changed, 34 insertions(+), 58 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 196f8a8..c644e14 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -106,8 +106,8 @@ public class ToBuysController { }) .collect(Collectors.toList()); - Map responseBody = new HashMap<>(); - responseBody.put("tobuy_array", responseList); + // Map responseBody = new HashMap<>(); + // responseBody.put("tobuy_array", responseList); return ResponseEntity.ok(responseList); } diff --git a/frontend/src/pages/StockPage.tsx b/frontend/src/pages/StockPage.tsx index 67103b5..3904178 100644 --- a/frontend/src/pages/StockPage.tsx +++ b/frontend/src/pages/StockPage.tsx @@ -47,7 +47,7 @@ const StockPage: React.FC = () => { const fetchStocks = async () => { try { const stocks = await stockApi.getStocks(); - setStocks(stocks.stock_array); + setStocks(stocks); } catch (error) { console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error); } diff --git a/frontend/src/pages/TaskListPage.tsx b/frontend/src/pages/TaskListPage.tsx index 340381d..6686e47 100644 --- a/frontend/src/pages/TaskListPage.tsx +++ b/frontend/src/pages/TaskListPage.tsx @@ -77,7 +77,7 @@ const TaskListPage: React.FC = () => { const fetchTasks = async () => { try { const tobuys = await toBuyApi.getToBuys(); - setToBuys(tobuys.tobuy_array); + setToBuys(tobuys); } catch (error) { console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error); } @@ -86,7 +86,7 @@ const TaskListPage: React.FC = () => { const onChangeCategory = async (category: string) => { setNewToBuy({ ...newToBuy, category }) const result = await stuffApi.getStuffs(category) - setStuffs(result.stuff_array) + setStuffs(result) } // /** diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index d360657..221b16f 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -92,7 +92,7 @@ export const toBuyApi = { * 全買うものリストを取得 * @returns 買うものリスト一覧 */ - getToBuys: async (): Promise<{ "tobuy_array": ToBuy[] }> => { + getToBuys: async (): Promise => { const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { headers: getHeaders(), // 認証トークンを含むヘッダー }); @@ -101,27 +101,7 @@ export const toBuyApi = { throw new Error(TOBUY_ERRORS.FETCH_FAILED); } - const tobuy_array = await response.json(); - return {tobuy_array}; - - // return { - // "tobuy_array": [ - // { - // "tobuyId": 1, - // "stuffId": 2, - // "stuffName": "じゃがいも", - // "amount": 3, - // "shop": "shopXXX" - // }, - // { - // "tobuyId": 2, - // "stuffId": 5, - // "stuffName": "にんじん", - // "amount": 1 - // } - // ] - // } - + return await response.json(); }, /** @@ -176,7 +156,7 @@ export const toBuyApi = { } export const stuffApi = { - getStuffs: async (category: string): Promise<{ stuff_array: Stuff[] }> => { + getStuffs: async (category: string): Promise => { const data = [ { stuffId: 1, stuffName: "牛乳", category: "乳製品" }, { stuffId: 2, stuffName: "ヨーグルト", category: "乳製品" }, @@ -211,9 +191,7 @@ export const stuffApi = { const filtered = data.filter(stuff => stuff.category == category) - return { - "stuff_array": filtered - } + return filtered } } @@ -223,7 +201,7 @@ export const stockApi = { * 全在庫リストを取得 * @returns 買在庫リスト一覧 */ - getStocks: async (): Promise<{ "stock_array": Stock[] }> => { + getStocks: async (): Promise => { // const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { // headers: getHeaders(), // 認証トークンを含むヘッダー // }); @@ -234,32 +212,30 @@ export const stockApi = { // return response.json(); - return { - "stock_array": [ - { - "stockId": 1, - "stuffId": 10, - "stuffName": "豚肉", - "amount": 100, - "price": 200, - "buyDate": "2025-05-18T09:00:00.000Z", - "lastUpdate": "2025-05-18T09:00:00.000Z", - "expDate": "2025-05-19T10:15:00.000Z", - "category": "肉" - }, - { - "stockId": 2, - "stuffId": 1, - "stuffName": "トマト", - "amount": 10, - "price": 200, - "buyDate": "2025-05-18T09:00:00.000Z", - "lastUpdate": "2025-05-18T09:00:00.000Z", - "expDate": "2025-05-19T10:15:00.000Z", - "category": "野菜" - } - ] - } + return [ + { + "stockId": 1, + "stuffId": 10, + "stuffName": "豚肉", + "amount": 100, + "price": 200, + "buyDate": "2025-05-18T09:00:00.000Z", + "lastUpdate": "2025-05-18T09:00:00.000Z", + "expDate": "2025-05-19T10:15:00.000Z", + "category": "肉" + }, + { + "stockId": 2, + "stuffId": 1, + "stuffName": "トマト", + "amount": 10, + "price": 200, + "buyDate": "2025-05-18T09:00:00.000Z", + "lastUpdate": "2025-05-18T09:00:00.000Z", + "expDate": "2025-05-19T10:15:00.000Z", + "category": "野菜" + } + ] }, From 292bb6e43b626617d0ea7204d20d37888791dfde Mon Sep 17 00:00:00 2001 From: Amagasu Date: Mon, 9 Jun 2025 13:31:03 +0900 Subject: [PATCH 21/32] remove _id --- .../example/todoapp/model/RecipeStuffs.java | 4 +- .../java/com/example/todoapp/model/Task.java | 2 +- .../todoapp/service/StocksService.java | 2 +- frontend/src/pages/AddDishes1.tsx | 2 +- frontend/src/services/api.ts | 70 +++++++++---------- frontend/src/types/types.ts | 2 +- 6 files changed, 41 insertions(+), 41 deletions(-) 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 5c3a933..74d5738 100644 --- a/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java @@ -28,7 +28,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @Entity -@Table(name = "recipe_stuffs") +@Table(name = "recipeStuffs") public class RecipeStuffs { @@ -37,7 +37,7 @@ public class RecipeStuffs { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="recipe_stuffs_id") + @Column(name="recipeStuffsId") private Long recipeStuffsId ; /** diff --git a/backend/src/main/java/com/example/todoapp/model/Task.java b/backend/src/main/java/com/example/todoapp/model/Task.java index 9e3ad8a..c2ba9bd 100644 --- a/backend/src/main/java/com/example/todoapp/model/Task.java +++ b/backend/src/main/java/com/example/todoapp/model/Task.java @@ -51,7 +51,7 @@ public class Task { * 多対一の関係で、遅延ロードを使用 */ @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", nullable = false) + @JoinColumn(name = "userId", nullable = false) private User user; /** diff --git a/backend/src/main/java/com/example/todoapp/service/StocksService.java b/backend/src/main/java/com/example/todoapp/service/StocksService.java index 07dad3c..e793f1f 100644 --- a/backend/src/main/java/com/example/todoapp/service/StocksService.java +++ b/backend/src/main/java/com/example/todoapp/service/StocksService.java @@ -45,7 +45,7 @@ public class StocksService { /** * 指定されたユーザのすべての在庫を取得する * @param username ユーザー名 - * @return ユーザーの在庫リスト(stock_id昇順) + * @return ユーザーの在庫リスト(stockId昇順) */ public List getALLStocksByUser(String username) { User user = getUserByUsername(username); diff --git a/frontend/src/pages/AddDishes1.tsx b/frontend/src/pages/AddDishes1.tsx index d003ebd..5c28373 100644 --- a/frontend/src/pages/AddDishes1.tsx +++ b/frontend/src/pages/AddDishes1.tsx @@ -60,7 +60,7 @@ const AddDishes1: React.FC = () => { InputLabelProps={{ style: { fontSize: "40px" }}} style={{width: "80%" }} InputProps={{ style: { fontSize: "40px"} }} - name="dish_name" + name="dishName" // autoComplete="username" autoFocus value={dish} diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index d360657..682b954 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -140,8 +140,8 @@ export const toBuyApi = { throw new Error(TOBUY_ERRORS.CREATE_FAILED); } - // return response.json(); - return {result: true} + return response.json(); + // return {result: true} // return { // "result": true, @@ -224,42 +224,42 @@ export const stockApi = { * @returns 買在庫リスト一覧 */ getStocks: async (): Promise<{ "stock_array": Stock[] }> => { - // const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { - // headers: getHeaders(), // 認証トークンを含むヘッダー - // }); + const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { + headers: getHeaders(), // 認証トークンを含むヘッダー + }); - // if (!response.ok) { - // throw new Error(TASK_ERRORS.FETCH_FAILED); - // } + if (!response.ok) { + throw new Error(TASK_ERRORS.FETCH_FAILED); + } - // return response.json(); + return response.json(); - return { - "stock_array": [ - { - "stockId": 1, - "stuffId": 10, - "stuffName": "豚肉", - "amount": 100, - "price": 200, - "buyDate": "2025-05-18T09:00:00.000Z", - "lastUpdate": "2025-05-18T09:00:00.000Z", - "expDate": "2025-05-19T10:15:00.000Z", - "category": "肉" - }, - { - "stockId": 2, - "stuffId": 1, - "stuffName": "トマト", - "amount": 10, - "price": 200, - "buyDate": "2025-05-18T09:00:00.000Z", - "lastUpdate": "2025-05-18T09:00:00.000Z", - "expDate": "2025-05-19T10:15:00.000Z", - "category": "野菜" - } - ] - } + // return { + // "stock_array": [ + // { + // "stockId": 1, + // "stuffId": 10, + // "stuffName": "豚肉", + // "amount": 100, + // "price": 200, + // "buyDate": "2025-05-18T09:00:00.000Z", + // "lastUpdate": "2025-05-18T09:00:00.000Z", + // "expDate": "2025-05-19T10:15:00.000Z", + // "category": "肉" + // }, + // { + // "stockId": 2, + // "stuffId": 1, + // "stuffName": "トマト", + // "amount": 10, + // "price": 200, + // "buyDate": "2025-05-18T09:00:00.000Z", + // "lastUpdate": "2025-05-18T09:00:00.000Z", + // "expDate": "2025-05-19T10:15:00.000Z", + // "category": "野菜" + // } + // ] + // } }, diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index 1a93f80..0f2e897 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -4,7 +4,7 @@ */ export interface Task { id: number; // タスクの一意識別子 - stuff_name: string; // タスクのタイトル + stuffName: string; // タスクのタイトル amount: number; //材料の数量 price: number; //材料の値段 buyDate:Date; //購入日時 From b08c92f6c012a9707ee23eeb4ef16738de6a1457 Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 13:39:25 +0900 Subject: [PATCH 22/32] Fix response of tobuy API --- .../com/example/todoapp/controller/ToBuysController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 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 c644e14..00a9766 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.Collections; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -57,19 +58,19 @@ 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"); + return ResponseEntity.ok(Collections.singletonMap("result", true)); } @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"); + return ResponseEntity.ok(Collections.singletonMap("result", true)); } From d7c2eadb09c5085cc5d56a366c2f93aaebbda4ce Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Mon, 9 Jun 2025 13:43:26 +0900 Subject: [PATCH 23/32] =?UTF-8?q?tobuy=E3=81=AE=E8=BF=94=E3=81=99=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=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 | 27 ++++++++++++--- .../todoapp/repository/StuffsRepository.java | 3 ++ .../todoapp/service/ToBuysService.java | 33 +++++++++++-------- 3 files changed, 44 insertions(+), 19 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 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); } /** From e4414713dc39ec7a2784208c5bf96e31362885b7 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Mon, 9 Jun 2025 15:11:35 +0900 Subject: [PATCH 24/32] create API stuffs/get (get all stuffs which has input category) --- .../todoapp/controller/StuffsController.java | 64 +++++++++++++++++++ .../com/example/todoapp/dto/StuffsDTO.java | 42 ++++++++++++ .../com/example/todoapp/model/Stuffs.java | 3 +- .../todoapp/repository/StuffsRepository.java | 11 +++- .../todoapp/service/StuffsService.java | 39 +++++++++++ 5 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/example/todoapp/controller/StuffsController.java create mode 100644 backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java create mode 100644 backend/src/main/java/com/example/todoapp/service/StuffsService.java diff --git a/backend/src/main/java/com/example/todoapp/controller/StuffsController.java b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java new file mode 100644 index 0000000..68722f6 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/StuffsController.java @@ -0,0 +1,64 @@ +//-------------------------------- +// StuffsController.java +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.controller; + +import com.example.todoapp.dto.StuffsDTO; +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.service.StuffsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.Collections; + + +/** + * 材料リストに関するRESTコントローラー + * + * このコントローラーは、材料 (stuffs) の管理に関するエンドポイントを提供します。 + * + */ +@RestController +@RequestMapping("/stuff") +public class StuffsController { + + @Autowired + private StuffsService stuffsService; + + + /** + * カテゴリ指定で材料を取得するメソッド + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + @GetMapping("/get") + public ResponseEntity getAllStuffsByCategory(String category) { + List stuffsList = stuffsService.getAllStuffsByCategory(category); + //DTOに変換 + List responceList = stuffsList.stream() + .map(stuff -> { + StuffsDTO resp = new StuffsDTO(); + resp.setStuffId(stuff.getStuffId()); + resp.setStuffName(stuff.getStuffName()); + resp.setSummary(stuff.getSummary()); + resp.setCategory(stuff.getCategory()); + return resp; + }) + .collect(Collectors.toList()); + // 空のリストの場合は空のリストを返す + if (responceList.isEmpty()) { + return ResponseEntity.ok(Collections.emptyList()); + } + return ResponseEntity.ok(responceList); + } + +} diff --git a/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java b/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java new file mode 100644 index 0000000..5dd6706 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/StuffsDTO.java @@ -0,0 +1,42 @@ +//-------------------------------- +// StuffsDTO.java +// +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.dto; + +import com.example.todoapp.model.Stuffs; +import lombok.Data; + + +/** + * 材料のデータ転送オブジェクト(DTO)クラス + * + * このクラスは、材料情報をクライアントとサーバー間でやり取りするために使用されます。 + * エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 + */ +@Data +public class StuffsDTO { + private Long stuffId; // 材料ID + private String stuffName; // 材料名 + private String summary; // 概要 + private String category; // カテゴリ + + /** + * StuffsエンティティからDTOを作成する + * + * @param stuff 変換元のStuffsエンティティ + * @return 変換されたStuffsDTOオブジェクト + */ + public static StuffsDTO fromEntity(Stuffs stuff) { + StuffsDTO dto = new StuffsDTO(); + dto.setStuffId(stuff.getStuffId()); + dto.setStuffName(stuff.getStuffName()); + dto.setSummary(stuff.getSummary()); + dto.setCategory(stuff.getCategory()); + return dto; + } + +} 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 14fb111..8807fcf 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/Stuffs.java @@ -1,9 +1,8 @@ //-------------------------------- // Stuffs.java // -// 分類:社員管理システムV2・ビジネスロジック層 // -// 更新履歴:2025/06/02 新規作成 +// 更新履歴:2025/06/09 新規作成 // Copyright(c) 2025 IVIS All rights reserved. //-------------------------------------------- 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..e1b02c6 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -2,7 +2,7 @@ // StuffsRepository.java // // -// 更新履歴:2025/06/05 新規作成 +// 更新履歴:2025/06/09 新規作成 // Copyright(c) 2025 IVIS All rights reserved. //-------------------------------------------- @@ -10,6 +10,7 @@ package com.example.todoapp.repository; import com.example.todoapp.model.Stuffs; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -26,4 +27,12 @@ public interface StuffsRepository extends JpaRepository { // 材料情報を主キーで取得するメソッド(必要に応じて追加) Stuffs findByStuffId(Long stuffId); + /** + * 指定されたカテゴリに属する材料のリストを取得するメソッド. + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + List findByCategory(String category); + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/StuffsService.java b/backend/src/main/java/com/example/todoapp/service/StuffsService.java new file mode 100644 index 0000000..18b3924 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/StuffsService.java @@ -0,0 +1,39 @@ +//-------------------------------- +// StuffsService.java +// +// +// 更新履歴:2025/06/09 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.service; + +import com.example.todoapp.model.Stuffs; +import com.example.todoapp.repository.StuffsRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 材料 (stuffs) のサービスクラス + * + * このクラスは、材料 (stuffs) に関するビジネスロジックを提供します。 + */ + +@Service +public class StuffsService { + + @Autowired + private StuffsRepository stuffsRepository; + + /** + * カテゴリ指定で材料を取得するメソッド + * + * @param category カテゴリ名 + * @return 指定されたカテゴリに属する材料のリスト + */ + public List getAllStuffsByCategory(String category) { + return stuffsRepository.findByCategory(category); + } +} From 22ac67172ed8acf50d6cadb6131c8a0f28d7ff8e Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 15:43:00 +0900 Subject: [PATCH 25/32] =?UTF-8?q?=E5=8F=A4=E3=81=84taskApi=E3=81=AE?= =?UTF-8?q?=E5=89=8A=E9=99=A4=EF=BC=8C=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=A1?= =?UTF-8?q?=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8=E5=A4=89=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/constants/errorMessages.ts | 35 +++- frontend/src/pages/AddDishes2.tsx | 49 +++-- frontend/src/services/api.ts | 250 ++++++++++++------------ frontend/src/types/types.ts | 36 ++-- 4 files changed, 197 insertions(+), 173 deletions(-) diff --git a/frontend/src/constants/errorMessages.ts b/frontend/src/constants/errorMessages.ts index 909bbfa..0aa0f98 100644 --- a/frontend/src/constants/errorMessages.ts +++ b/frontend/src/constants/errorMessages.ts @@ -14,18 +14,37 @@ export const AUTH_ERRORS = { REGISTER_FAILED: 'ユーザー登録に失敗しました', }; -// タスク関連のエラーメッセージ -export const TASK_ERRORS = { - FETCH_FAILED: 'タスクの取得に失敗しました', - CREATE_FAILED: 'タスクの作成に失敗しました', - UPDATE_FAILED: 'タスクの更新に失敗しました', - DELETE_FAILED: 'タスクの削除に失敗しました', -}; - // 買うものリスト関連のエラーメッセージ export const TOBUY_ERRORS = { FETCH_FAILED: '買うものリストの取得に失敗しました', CREATE_FAILED: '買うものリストの作成に失敗しました', UPDATE_FAILED: '買うものリストの更新に失敗しました', DELETE_FAILED: '買うものリストの削除に失敗しました', + BUY_FAILED: '買うものリストの購入処理に失敗しました', +}; + +// 材料リスト関連のエラーメッセージ(料理の追加編集で用いる) +export const STUFF_ERRORS = { + FETCH_FAILED: '材料リストの取得に失敗しました', + CREATE_FAILED: '材料リストの作成に失敗しました', + UPDATE_FAILED: '材料リストの更新に失敗しました', + DELETE_FAILED: '材料リストの削除に失敗しました', + BUY_FAILED: '材料リストの購入処理に失敗しました', }; + +// 在庫リスト関連のエラーメッセージ +export const STOCK_ERRORS = { + FETCH_FAILED: '在庫リストの取得に失敗しました', + CREATE_FAILED: '在庫リストの作成に失敗しました', + UPDATE_FAILED: '在庫リストの更新に失敗しました', + DELETE_FAILED: '在庫リストの削除に失敗しました', + BUY_FAILED: '在庫リストの購入処理に失敗しました', +}; + +// // タスク関連のエラーメッセージ +// export const TASK_ERRORS = { +// FETCH_FAILED: 'タスクの取得に失敗しました', +// CREATE_FAILED: 'タスクの作成に失敗しました', +// UPDATE_FAILED: 'タスクの更新に失敗しました', +// DELETE_FAILED: 'タスクの削除に失敗しました', +// }; \ No newline at end of file diff --git a/frontend/src/pages/AddDishes2.tsx b/frontend/src/pages/AddDishes2.tsx index d8aa62c..123f8b7 100644 --- a/frontend/src/pages/AddDishes2.tsx +++ b/frontend/src/pages/AddDishes2.tsx @@ -3,7 +3,7 @@ * 白紙の状態で表示されるテスト用のページ */ import React, { useState, useEffect } from 'react'; -import { taskApi, toBuyApi } from '../services/api'; +import { toBuyApi } from '../services/api'; import { Container, Typography, @@ -31,8 +31,7 @@ import { Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon, SoupKitchen as SoupKitchenIcon } from '@mui/icons-material'; -import { Task } from '../types/types'; -import { TASK_ERRORS } from '../constants/errorMessages'; +import { STUFF_ERRORS } from '../constants/errorMessages'; import { GENERAL_ERRORS } from '../constants/errorMessages'; import CategoryDropDown from "../components/CategoryDropDown"; @@ -42,7 +41,7 @@ const EMPTY_TASK = { id: 0, title: '', amount: 0, completed: false }; // 新規タスクの初期状態(データベース登録用) const EMPTY_TASK_DATA_BASE = { id: 0, title: '', amount: 0, completed: false }; -interface Empty_Task { +interface Stuff { title: string; // 食材名 amount: number; // 食材の個数 completed: boolean; // @@ -51,9 +50,9 @@ interface Empty_Task { const AddDishes2: React.FC = () => { const receivedData = localStorage.getItem("dishName"); // タスク一覧の状態管理 - const [tasks, setTasks] = useState([]); + const [tasks, setTasks] = useState([]); - const [addtasks, setAddTasks] = useState([]); + const [addtasks, setAddTasks] = useState([]); // エラーメッセージの状態管理 const [error, setError] = useState(false); // 新規タスク作成ダイアログの表示状態 @@ -61,23 +60,23 @@ const AddDishes2: React.FC = () => { // 新規タスクの入力内容 const [newTask, setNewTask] = useState(EMPTY_TASK); - // コンポーネントマウント時にタスク一覧を取得 - useEffect(() => { - fetchTasks(); - }, []); + // // コンポーネントマウント時にタスク一覧を取得 + // useEffect(() => { + // fetchTasks(); + // }, []); - /** - * APIからタスク一覧を取得する関数 - * 取得したタスクをstate(tasks)に設定 - */ - const fetchTasks = async () => { - try { - const tasks = await taskApi.getTasks(); - setTasks(tasks); - } catch (error) { - console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error); - } - }; + // /** + // * APIからタスク一覧を取得する関数 + // * 取得したタスクをstate(tasks)に設定 + // */ + // const fetchTasks = async () => { + // try { + // const tasks = await taskApi.getTasks(); + // setTasks(tasks); + // } catch (error) { + // console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error); + // } + // }; /** * タスクを削除するハンドラー * 指定されたIDのタスクをAPIを通じて削除 @@ -89,7 +88,7 @@ const AddDishes2: React.FC = () => { setAddTasks(newAddTasks); // fetchTasks(); // 削除後のタスク一覧を再取得 } catch (error) { - console.error(`${TASK_ERRORS.DELETE_FAILED}:`, error); + console.error(`${STUFF_ERRORS.DELETE_FAILED}:`, error); } }; @@ -120,7 +119,7 @@ const AddDishes2: React.FC = () => { setNewTask(EMPTY_TASK); // 入力内容をリセット // fetchTasks(); // 作成後のタスク一覧を再取得 } catch (error) { - console.error(`${TASK_ERRORS.CREATE_FAILED}:`, error); + console.error(`${STUFF_ERRORS.CREATE_FAILED}:`, error); } }; @@ -134,7 +133,7 @@ const AddDishes2: React.FC = () => { setNewTask(EMPTY_TASK); // 入力内容をリセット // fetchTasks(); // 作成後のタスク一覧を再取得 } catch (error) { - console.error(`${TASK_ERRORS.CREATE_FAILED}:`, error); + console.error(`${STUFF_ERRORS.CREATE_FAILED}:`, error); } }; diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index f214404..916729d 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -3,8 +3,8 @@ * バックエンドAPIとの通信を担当するモジュール * 認証、タスク管理などの機能を提供 */ -import { LoginCredentials, RegisterCredentials, AuthResponse, Task, ToBuy, Stuff, Stock } from '../types/types'; -import { AUTH_ERRORS, TASK_ERRORS, TOBUY_ERRORS } from '../constants/errorMessages'; +import { LoginCredentials, RegisterCredentials, AuthResponse, /* Task, */ ToBuy, Stuff, Stock } from '../types/types'; +import { AUTH_ERRORS, TOBUY_ERRORS, STOCK_ERRORS } from '../constants/errorMessages'; // APIのベースURL - 環境変数から取得するか、デフォルト値を使用 const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080'; @@ -153,6 +153,30 @@ export const toBuyApi = { // "result": true // } }, + + /** + * 買うものリストの在庫登録(購入処理) + */ + buy: async (req: {tobuyId: number, price: number, expDate: string, buyDate: string, lastUpdate: string}): Promise<{ result: boolean }> => { + + console.log('req: ', req) + + req.buyDate = makeDateObject(req.buyDate)?.toISOString()?.substring(0, 10) || '' + req.expDate = makeDateObject(req.expDate)?.toISOString()?.substring(0, 10) || '' + + const response = await fetch(`${API_BASE_URL}/api/tobuy/buy`, { + method: 'POST', + headers: getHeaders(), + body: JSON.stringify(req), + }); + + if (!response.ok) { + throw new Error(TOBUY_ERRORS.BUY_FAILED); + } + + return response.json() + }, + } export const stuffApi = { @@ -202,138 +226,120 @@ export const stockApi = { * @returns 買在庫リスト一覧 */ getStocks: async (): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { + const response = await fetch(`${API_BASE_URL}/api/stocks/get`, { headers: getHeaders(), // 認証トークンを含むヘッダー }); if (!response.ok) { - throw new Error(TASK_ERRORS.FETCH_FAILED); + throw new Error(STOCK_ERRORS.FETCH_FAILED); } return response.json(); - // return { - // "stock_array": [ - // { - // "stockId": 1, - // "stuffId": 10, - // "stuffName": "豚肉", - // "amount": 100, - // "price": 200, - // "buyDate": "2025-05-18T09:00:00.000Z", - // "lastUpdate": "2025-05-18T09:00:00.000Z", - // "expDate": "2025-05-19T10:15:00.000Z", - // "category": "肉" - // }, - // { - // "stockId": 2, - // "stuffId": 1, - // "stuffName": "トマト", - // "amount": 10, - // "price": 200, - // "buyDate": "2025-05-18T09:00:00.000Z", - // "lastUpdate": "2025-05-18T09:00:00.000Z", - // "expDate": "2025-05-19T10:15:00.000Z", - // "category": "野菜" - // } - // ] - // } - - }, } -/** - * (サンプル,実際には不要) - * タスク管理関連のAPI機能を提供するオブジェクト - * タスクの取得、作成、更新、削除などの機能を含む - */ -export const taskApi = { - /** - * 全タスクを取得 - * @returns タスク一覧 - */ - getTasks: async (): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tasks`, { - headers: getHeaders(), // 認証トークンを含むヘッダー - }); - - if (!response.ok) { - throw new Error(TASK_ERRORS.FETCH_FAILED); - } - - return response.json(); - }, - - /** - * 指定IDのタスクを取得 - * @param id タスクID - * @returns 単一のタスク情報 - */ - getTask: async (id: number): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { - headers: getHeaders(), - }); - - if (!response.ok) { - throw new Error(TASK_ERRORS.FETCH_FAILED); +function makeDateObject(dateStr: String) { + // 例: '2025/06/15' または '2025-06-15' を '2025-06-15' に変換 + const parts = dateStr.split(/[-\/]/); // ハイフンかスラッシュで分割 + if (parts.length === 3) { + return new Date(parts[0] + '-' + parts[1] + '-' + parts[2]); } + return null; // 無効な日付の場合 +} - return response.json(); - }, - - /** - * 新規材料を作成 - * @param task 作成するタスク情報(価格,作成日時、更新日時は除外) - * @returns 作成されたタスク情報 - */ - addStuff: async (task: Omit): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tubuy/add`, { - method: 'POST', - headers: getHeaders(), - body: JSON.stringify(task), - }); - - if (!response.ok) { - throw new Error(TASK_ERRORS.CREATE_FAILED); - } - - return response.json(); - }, - - /** - * タスクを更新 - * @param id 更新対象のタスクID - * @param task 更新するタスク情報(部分的な更新も可能) - * @returns 更新後のタスク情報 - */ - updateTask: async (id: number, task: Partial): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { - method: 'PUT', - headers: getHeaders(), - body: JSON.stringify(task), - }); - - if (!response.ok) { - throw new Error(TASK_ERRORS.UPDATE_FAILED); - } - - return response.json(); - }, - - /** - * タスクを削除 - * @param id 削除対象のタスクID - */ - deleteTask: async (id: number): Promise => { - const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { - method: 'DELETE', - headers: getHeaders(), - }); - if (!response.ok) { - throw new Error(TASK_ERRORS.DELETE_FAILED); - } - }, -}; +// /** +// * (サンプル,実際には不要) +// * タスク管理関連のAPI機能を提供するオブジェクト +// * タスクの取得、作成、更新、削除などの機能を含む +// */ +// export const taskApi = { +// /** +// * 全タスクを取得 +// * @returns タスク一覧 +// */ +// getTasks: async (): Promise => { +// const response = await fetch(`${API_BASE_URL}/api/tasks`, { +// headers: getHeaders(), // 認証トークンを含むヘッダー +// }); + +// if (!response.ok) { +// throw new Error(TASK_ERRORS.FETCH_FAILED); +// } + +// return response.json(); +// }, + +// /** +// * 指定IDのタスクを取得 +// * @param id タスクID +// * @returns 単一のタスク情報 +// */ +// getTask: async (id: number): Promise => { +// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { +// headers: getHeaders(), +// }); + +// if (!response.ok) { +// throw new Error(TASK_ERRORS.FETCH_FAILED); +// } + +// return response.json(); +// }, + +// /** +// * 新規材料を作成 +// * @param task 作成するタスク情報(価格,作成日時、更新日時は除外) +// * @returns 作成されたタスク情報 +// */ +// addStuff: async (task: Omit): Promise => { +// const response = await fetch(`${API_BASE_URL}/api/tubuy/add`, { +// method: 'POST', +// headers: getHeaders(), +// body: JSON.stringify(task), +// }); + +// if (!response.ok) { +// throw new Error(TASK_ERRORS.CREATE_FAILED); +// } + +// return response.json(); +// }, + +// /** +// * タスクを更新 +// * @param id 更新対象のタスクID +// * @param task 更新するタスク情報(部分的な更新も可能) +// * @returns 更新後のタスク情報 +// */ +// updateTask: async (id: number, task: Partial): Promise => { +// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { +// method: 'PUT', +// headers: getHeaders(), +// body: JSON.stringify(task), +// }); + +// if (!response.ok) { +// throw new Error(TASK_ERRORS.UPDATE_FAILED); +// } + +// return response.json(); +// }, + +// /** +// * タスクを削除 +// * @param id 削除対象のタスクID +// */ +// deleteTask: async (id: number): Promise => { +// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { +// method: 'DELETE', +// headers: getHeaders(), +// }); + +// if (!response.ok) { +// throw new Error(TASK_ERRORS.DELETE_FAILED); +// } +// }, +// }; diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index 0f2e897..3e7f654 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -1,21 +1,21 @@ -/** - * タスク情報を表す型定義 - * タスクの基本情報と状態を管理 - */ -export interface Task { - id: number; // タスクの一意識別子 - stuffName: string; // タスクのタイトル - amount: number; //材料の数量 - price: number; //材料の値段 - buyDate:Date; //購入日時 - expirationDate: Date; //賞味・消費期限 - //description?: string; // タスクの詳細説明(任意) - completed: boolean; // タスクの完了状態 - userId: number; // タスクの所有者ID - createdAt: string; // タスク作成日時 - newAddition: boolean //材料を新規追加するかどうか - //updatedAt: string; // タスク更新日時 -} +// /** +// * タスク情報を表す型定義 +// * タスクの基本情報と状態を管理 +// */ +// export interface Task { +// id: number; // タスクの一意識別子 +// stuffName: string; // タスクのタイトル +// amount: number; //材料の数量 +// price: number; //材料の値段 +// buyDate:Date; //購入日時 +// expirationDate: Date; //賞味・消費期限 +// //description?: string; // タスクの詳細説明(任意) +// completed: boolean; // タスクの完了状態 +// userId: number; // タスクの所有者ID +// createdAt: string; // タスク作成日時 +// newAddition: boolean //材料を新規追加するかどうか +// //updatedAt: string; // タスク更新日時 +// } /** * タスク情報を表す型定義 From 765fec0ebc7353ca77396966e7adfbc40ec049de Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 15:43:36 +0900 Subject: [PATCH 26/32] =?UTF-8?q?=E8=B3=BC=E5=85=A5=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AEAPI=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); } From 285ef1e81e700abbcd422c577071179c246892db Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 15:43:44 +0900 Subject: [PATCH 27/32] =?UTF-8?q?=E5=9C=A8=E5=BA=AB=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E3=81=AE=E5=AE=8C=E5=85=A8=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/StocksController.java | 11 ++-- .../example/todoapp/dto/ResponseStockDTO.java | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 backend/src/main/java/com/example/todoapp/dto/ResponseStockDTO.java diff --git a/backend/src/main/java/com/example/todoapp/controller/StocksController.java b/backend/src/main/java/com/example/todoapp/controller/StocksController.java index 8bdf482..2631bc1 100644 --- a/backend/src/main/java/com/example/todoapp/controller/StocksController.java +++ b/backend/src/main/java/com/example/todoapp/controller/StocksController.java @@ -1,5 +1,6 @@ package com.example.todoapp.controller; +import com.example.todoapp.dto.ResponseStockDTO; import com.example.todoapp.dto.StockDTO; import com.example.todoapp.model.Stocks; import com.example.todoapp.service.StocksService; @@ -32,12 +33,12 @@ public class StocksController { * @param authentication 認証情報 * @return ユーザーの在庫リスト */ - @GetMapping - public ResponseEntity> getAllStocks(Authentication authentication) { + @GetMapping("/get") + public ResponseEntity> getAllStocks(Authentication authentication) { List stocks = stockService.getALLStocksByUser(authentication.getName()); // エンティティからDTOへの変換 - List stockDTOs = stocks.stream() - .map(StockDTO::fromEntity) + List stockDTOs = stocks.stream() + .map(ResponseStockDTO::fromEntity) .collect(Collectors.toList()); return ResponseEntity.ok(stockDTOs); } @@ -64,7 +65,7 @@ public class StocksController { * @param stock 作成する在庫の情報 * @return 作成された在庫 */ - @PostMapping + @PostMapping("/add") public ResponseEntity createStock( Authentication authentication, @Valid @RequestBody Stocks stock) { diff --git a/backend/src/main/java/com/example/todoapp/dto/ResponseStockDTO.java b/backend/src/main/java/com/example/todoapp/dto/ResponseStockDTO.java new file mode 100644 index 0000000..1de5012 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/ResponseStockDTO.java @@ -0,0 +1,54 @@ +package com.example.todoapp.dto; + +import com.example.todoapp.model.Stocks; +import com.example.todoapp.model.Stuffs; + +import lombok.Data; + +import java.time.LocalDate; + +/** + * 在庫のデータ転送オブジェクト(DTO) + *

+ * このクラスはクライアントとサーバー間で在庫情報をやり取りするために使用されます。 + * エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 + *

+ */ + +@Data +public class ResponseStockDTO { + private Long stockId; + private Long stuffId; + private Long userId; + private int amount; + private int price; + private LocalDate buyDate; + private LocalDate lastUpdate; + private LocalDate expDate; + private String stuffName; + private String category; + + /** + * 在庫エンティティからDTOを作成する + * + * @param stock 変換元の在庫エンティティ + * @return 変換されたStockDTOオブジェクト + */ + public static ResponseStockDTO fromEntity(Stocks stock) { + ResponseStockDTO dto = new ResponseStockDTO(); + Stuffs stuff = stock.getStuff(); + + dto.setStockId(stock.getStockId()); + dto.setStuffId(stock.getStuff().getStuffId()); + dto.setUserId(stock.getUser().getId()); + dto.setAmount(stock.getAmount()); + dto.setPrice(stock.getPrice()); + dto.setBuyDate(stock.getBuyDate()); + dto.setLastUpdate(stock.getLastUpdate()); + dto.setExpDate(stock.getExpDate()); + dto.setStuffName(stuff.getStuffName()); + dto.setCategory(stuff.getCategory()); + + return dto; + } +} \ No newline at end of file From 9e1142ae0930ca36e4e9acae674e1270722958d7 Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Mon, 9 Jun 2025 15:44:00 +0900 Subject: [PATCH 28/32] =?UTF-8?q?=E8=B3=BC=E5=85=A5=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AEUI=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/pages/TaskListPage.tsx | 49 ++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/frontend/src/pages/TaskListPage.tsx b/frontend/src/pages/TaskListPage.tsx index 6686e47..ae7eeef 100644 --- a/frontend/src/pages/TaskListPage.tsx +++ b/frontend/src/pages/TaskListPage.tsx @@ -33,14 +33,14 @@ import { Add as AddIcon, Delete as DeleteIcon, ShoppingBasket as ShoppingBasketIcon, SoupKitchen as SoupKitchenIcon } from '@mui/icons-material'; -import { Task, ToBuy, Stuff } from '../types/types'; -import { TASK_ERRORS } from '../constants/errorMessages'; +import { ToBuy, Stuff, Stock } from '../types/types'; +import { TOBUY_ERRORS } from '../constants/errorMessages'; //import { FaCarrot } from "react-icons/fa6"; //エラー起きる いったん保留 // 新規タスクの初期状態 -const EMPTY_TASK: Omit & { stuffId: number | null, category: string } & { newAddition: boolean } = { +const EMPTY_TOBUY: Omit & { stuffId: number | null, category: string } & { newAddition: boolean } = { stuffId: null, stuffName: '', amount: 0, @@ -49,6 +49,12 @@ const EMPTY_TASK: Omit & { stuffId: number | null, newAddition: false, } +const EMPTY_STOCK = { + price: 0, + buyDate: '', + expDate: '', +} + const TaskListPage: React.FC = () => { // タスク一覧の状態管理 const [tobuys, setToBuys] = useState([]); @@ -60,10 +66,12 @@ const TaskListPage: React.FC = () => { const [selectedTask, setSelectedTask] = useState(0); - const [newToBuy, setNewToBuy] = useState(EMPTY_TASK); + const [newToBuy, setNewToBuy] = useState(EMPTY_TOBUY); const [stuffs, setStuffs] = useState([]); + const [newStock, setNewStock] = useState(EMPTY_STOCK); + // コンポーネントマウント時にタスク一覧を取得 useEffect(() => { @@ -79,7 +87,7 @@ const TaskListPage: React.FC = () => { const tobuys = await toBuyApi.getToBuys(); setToBuys(tobuys); } catch (error) { - console.error(`${TASK_ERRORS.FETCH_FAILED}:`, error); + console.error(`${TOBUY_ERRORS.FETCH_FAILED}:`, error); } }; @@ -112,9 +120,22 @@ const TaskListPage: React.FC = () => { const handleDeleteTask = async (toBuyId: number) => { try { await toBuyApi.deleteToBuy(toBuyId); - fetchTasks(); // 削除後のタスク一覧を再取得 + fetchTasks(); // 削除後の買うもの一覧を再取得 + } catch (error) { + console.error(`${TOBUY_ERRORS.DELETE_FAILED}:`, error); + } + }; + + /** + * 買うものリストの在庫登録(購入処理)を行うハンドラー + */ + const handleBuy = async (tobuyId: number) => { + try { + const today = new Date().toISOString().substring(0, 10); + await toBuyApi.buy({tobuyId, ...newStock, lastUpdate: today}); + fetchTasks(); // 削除後の買うもの一覧を再取得 } catch (error) { - console.error(`${TASK_ERRORS.DELETE_FAILED}:`, error); + console.error(`${TOBUY_ERRORS.BUY_FAILED}:`, error); } }; @@ -131,10 +152,10 @@ const TaskListPage: React.FC = () => { console.log(newToBuy) await toBuyApi.addToBuy(newToBuy); setOpenDialog(false); // ダイアログを閉じる - setNewToBuy(EMPTY_TASK); // 入力内容をリセット + setNewToBuy(EMPTY_TOBUY); // 入力内容をリセット fetchTasks(); // 作成後のタスク一覧を再取得 } catch (error) { - console.error(`${TASK_ERRORS.CREATE_FAILED}:`, error); + console.error(`${TOBUY_ERRORS.CREATE_FAILED}:`, error); } }; @@ -333,6 +354,8 @@ const TaskListPage: React.FC = () => { margin="dense" label="価格" fullWidth + value={newStock.price} + onChange={(e) => setNewStock({...newStock, price: parseInt(e.target.value)})} /> {/* 消費・賞味期限入力フィールド */} { label="消費・賞味期限(yyyy/MM/dd)" fullWidth multiline + value={newStock.expDate} + onChange={(e) => setNewStock({...newStock, expDate: e.target.value})} /> {/* 購入日入力フィールド */} { label="購入日(yyyy/MM/dd)" fullWidth multiline + value={newStock.buyDate} + onChange={(e) => setNewStock({...newStock, buyDate: e.target.value})} /> @@ -354,9 +381,9 @@ const TaskListPage: React.FC = () => {