From 9eebcf670e3d3e8d79755926d958dd93d6ce9363 Mon Sep 17 00:00:00 2001 From: Amagasu Date: Wed, 18 Jun 2025 11:05:50 +0900 Subject: [PATCH] api/tobuy/addByRecipe can select stock-differ option --- .../todoapp/controller/ToBuysController.java | 7 +++--- .../example/todoapp/dto/AddByRecipeDTO.java | 25 +++++++++++++++++++ .../todoapp/service/ToBuysService.java | 19 +++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 backend/src/main/java/com/example/todoapp/dto/AddByRecipeDTO.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 e263330..35844f7 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.AddByRecipeDTO; import com.example.todoapp.dto.BuyRequestDTO; import com.example.todoapp.dto.DeleteToBuyRequestDTO; import com.example.todoapp.dto.ToBuyResponseDTO; @@ -181,12 +182,10 @@ public class ToBuysController { */ @PostMapping("/addByRecipe") public ResponseEntity> addByRecipe( - @RequestBody Map payload, + @RequestBody AddByRecipeDTO addByRecipeDTO, Authentication authentication) { - Long recipeId = payload.get("recipeId"); - Long servings = payload.get("servings"); - List responseList = toBuysService.addByRecipeId(recipeId, servings,authentication); + List responseList = toBuysService.addByRecipeId(addByRecipeDTO.getRecipeId(), addByRecipeDTO.getServings(), addByRecipeDTO.getDifference(), authentication); //shopのフィールドを削除 List> filteredList = responseList.stream() diff --git a/backend/src/main/java/com/example/todoapp/dto/AddByRecipeDTO.java b/backend/src/main/java/com/example/todoapp/dto/AddByRecipeDTO.java new file mode 100644 index 0000000..5fef152 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/AddByRecipeDTO.java @@ -0,0 +1,25 @@ +//-------------------------------- +// AddByRecipeDTO.java +// +// +// 更新履歴:2025/06/18 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- +package com.example.todoapp.dto; + +import lombok.Data; + +/** + * 料理からの買うものリスト追加データ転送オブジェクト(DTO) + *

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

+ */ + +@Data +public class AddByRecipeDTO { + private Long recipeId; + private Long servings; + private Boolean difference; +} \ 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 2c0cc2a..d88d629 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -63,6 +63,9 @@ public class ToBuysService { @Autowired private RecipeStuffsRepository RecipeStuffsRepository; + @Autowired + private StocksService stocksService; + /** * 購入リストに新しいアイテムを追加する * @@ -222,7 +225,7 @@ public class ToBuysService { * @param authentication 認証情報 * @return 追加された「買うもの」のリスト */ - public List addByRecipeId(Long recipeId, Long servings,Authentication authentication) { + public List addByRecipeId(Long recipeId, Long servings, Boolean difference, Authentication authentication) { // ユーザー情報を取得 String username = authentication.getName(); User user = userRepository.findByUsername(username) @@ -236,13 +239,23 @@ public class ToBuysService { for (RecipeStuffs rs : recipeStuffsList) { Stuffs stuff = rs.getStuff(); - // 材料の数量をサービング数に基づいて計算 + // 材料の数量を在庫数とサービング数に基づいて計算 + int stockAmount = stocksService.calcAmountByStuffId(user.getId(), stuff.getStuffId()); int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1); + if (difference) { + // 差分を計算 + requiredAmount = requiredAmount - stockAmount; + if (requiredAmount <= 0) { + continue; // 在庫が十分なので追加しない + } + } else { + // 差分を考慮しない場合はrequiredAmountをそのまま使用 + } Optional existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff); ToBuys toBuy; - if (existingToBuyOpt.isPresent()) { + if (existingToBuyOpt.isPresent()) { // 既存の「買うもの」がある場合 toBuy = existingToBuyOpt.get(); toBuy.setAmount(toBuy.getAmount() + requiredAmount); // 既存の数量を更新 } else {