diff --git a/backend/src/main/java/com/example/todoapp/config/InitTables.java b/backend/src/main/java/com/example/todoapp/config/InitTables.java index 413f9f2..10bfa66 100644 --- a/backend/src/main/java/com/example/todoapp/config/InitTables.java +++ b/backend/src/main/java/com/example/todoapp/config/InitTables.java @@ -36,7 +36,7 @@ public class InitTables { @Autowired private RecipesRepository recipesRepository; @Autowired - private RecipeStuffsRepository recipeStuffsRepository; + private RecipeStuffsRepository RecipeStuffsRepository; @Autowired private StuffsRepository stuffsRepository; @@ -128,6 +128,6 @@ public class InitTables { recipeStuff.setRecipes(recipe); recipeStuff.setRecipeStuffsId(stuffId); recipeStuff.setAmount(amount); - recipeStuffsRepository.save(recipeStuff); + RecipeStuffsRepository.save(recipeStuff); } } 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 901e813..799d3f9 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -175,4 +175,39 @@ public class ToBuysController { return ResponseEntity.ok(response); } + + /** + * レシピから「買うもの」を追加する + * + * @param payload レシピIDを含むペイロード + * @param authentication 認証情報 + * @return 追加された「買うもの」のリスト + */ + @PostMapping("/addByRecipe") + public ResponseEntity> addByRecipe( + @RequestBody Map payload, + Authentication authentication) { + + Long recipeId = payload.get("recipeId"); + List responseList = toBuysService.addByRecipeId(recipeId, authentication); + + //shopのフィールドを削除 + List> filteredList = responseList.stream() + .map(dto -> { + Map map = new HashMap<>(); + map.put("tobuyId", dto.getTobuyId()); + map.put("stuffId", dto.getStuffId()); + map.put("stuffName", dto.getStuffName()); + map.put("amount", dto.getAmount()); + return map; + }) + .collect(Collectors.toList()); + + Map response = new HashMap<>(); + response.put("result", true); + response.put("data", filteredList); + + return ResponseEntity.ok(response); +} + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/RecipeStuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/RecipeStuffsRepository.java index eb053c4..cbbb485 100644 --- a/backend/src/main/java/com/example/todoapp/repository/RecipeStuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/RecipeStuffsRepository.java @@ -1,27 +1,37 @@ -//-------------------------------- -// RecipeStuffsRepository.java -// -// -// 更新履歴:2025/06/10 新規作成 -// Copyright(c) 2025 IVIS All rights reserved. -//-------------------------------------------- package com.example.todoapp.repository; -import com.example.todoapp.model.RecipeStuffs; - +import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; + +import com.example.todoapp.model.RecipeStuffs; /** - * 料理-材料エンティティのリポジトリインターフェース + * レシピ食材関連データのリポジトリインターフェース *

- * このインターフェースは料理-材料データへのアクセスと操作を提供します。 + * このインターフェースはレシピと食材の関連データへのアクセスを提供します。 * Spring Data JPAによって自動的に実装されます。 *

*/ - -@Repository public interface RecipeStuffsRepository extends JpaRepository { - -} \ No newline at end of file + /** + * レシピIDで食材情報を検索する + * + * @param recipeId 検索するレシピID + * @return 関連する食材情報リスト + */ + List findByRecipesRecipeId(Long recipeId); + + /** + * レシピIDと食材IDで関連情報を検索する + * + * @param recipeId 検索するレシピID + * @param stuffId 検索する食材ID + * @return 関連情報(存在する場合) + */ + Optional findByRecipesRecipeIdAndStuffStuffId(Long recipeId, Long stuffId); +} + + + diff --git a/backend/src/main/java/com/example/todoapp/repository/RecipesStuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/RecipesStuffsRepository.java deleted file mode 100644 index 729b825..0000000 --- a/backend/src/main/java/com/example/todoapp/repository/RecipesStuffsRepository.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.example.todoapp.repository; - -import java.util.List; -import java.util.Optional; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.example.todoapp.model.RecipeStuffs; - -/** - * レシピ食材関連データのリポジトリインターフェース - *

- * このインターフェースはレシピと食材の関連データへのアクセスを提供します。 - * Spring Data JPAによって自動的に実装されます。 - *

- */ -public interface RecipesStuffsRepository extends JpaRepository { - /** - * レシピIDで食材情報を検索する - * - * @param recipeId 検索するレシピID - * @return 関連する食材情報リスト - */ - List findByRecipesRecipeId(Long recipeId); - - /** - * レシピIDと食材IDで関連情報を検索する - * - * @param recipeId 検索するレシピID - * @param stuffId 検索する食材ID - * @return 関連情報(存在する場合) - */ - Optional findByRecipesRecipeIdAndStuffStuffId(Long recipeId, Long stuffId); -} - - - 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 7b15b89..d9b6861 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -7,9 +7,12 @@ //-------------------------------------------- package com.example.todoapp.repository; +import com.example.todoapp.model.Stuffs; import com.example.todoapp.model.ToBuys; +import com.example.todoapp.model.User; import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -54,4 +57,15 @@ public interface ToBuysRepository extends JpaRepository { @Modifying @Query("DELETE FROM ToBuys t WHERE t.tobuyId = :tobuyId") int deleteByTobuyId(@Param("tobuyId") Long tobuyId); + + /** + * ユーザーとスタッフに基づいて「買うもの」レコードを取得する + * + * @param user ユーザー + * @param stuff スタッフ + * @return 「買うもの」レコード + */ + @Query("SELECT t FROM ToBuys t WHERE t.user = :user AND t.stuff = :stuff") + Optional findByUserAndStuff(@Param("user") User user, @Param("stuff") Stuffs stuff); + } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/RecipeService.java b/backend/src/main/java/com/example/todoapp/service/RecipeService.java index 2eab2b8..5e23662 100644 --- a/backend/src/main/java/com/example/todoapp/service/RecipeService.java +++ b/backend/src/main/java/com/example/todoapp/service/RecipeService.java @@ -24,7 +24,7 @@ import com.example.todoapp.dto.StuffRequestDTO; import com.example.todoapp.model.RecipeStuffs; import com.example.todoapp.model.Recipes; import com.example.todoapp.model.Stuffs; -import com.example.todoapp.repository.RecipesStuffsRepository; +import com.example.todoapp.repository.RecipeStuffsRepository; import com.example.todoapp.repository.RecipesRepository; import com.example.todoapp.repository.StuffsRepository; @@ -47,7 +47,7 @@ public class RecipeService { private StuffsRepository stuffsRepository; @Autowired - private RecipesStuffsRepository recipeStuffsRepository; + private RecipeStuffsRepository RecipeStuffsRepository; /** * レシピを新規登録する @@ -89,7 +89,7 @@ public class RecipeService { recipeStuffsList.add(recipeStuffs); } - recipeStuffsRepository.saveAll(recipeStuffsList); + RecipeStuffsRepository.saveAll(recipeStuffsList); return recipe; } @@ -113,7 +113,7 @@ public class RecipeService { Recipes recipe = recipesRepository.findById(recipeId) .orElseThrow(() -> new RuntimeException("レシピが見つかりません")); - List recipeStuffsList = recipeStuffsRepository.findByRecipesRecipeId(recipeId); + List recipeStuffsList = RecipeStuffsRepository.findByRecipesRecipeId(recipeId); List stuffList = recipeStuffsList.stream() .map(rs -> { @@ -168,19 +168,19 @@ public class RecipeService { rs.setRecipes(recipe); rs.setStuff(newStuff); rs.setAmount(stuffDTO.getAmount()); - recipeStuffsRepository.save(rs); + RecipeStuffsRepository.save(rs); incomingStuffIds.add(newStuff.getStuffId()); } else { // 材料IDが提供されている場合、既存のRecipeStuffsエントリを検索 - Optional optionalRs = recipeStuffsRepository + Optional optionalRs = RecipeStuffsRepository .findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffDTO.getStuffId()); if (optionalRs.isPresent()) { // RecipeStuffsエントリが存在する場合、数量を更新 RecipeStuffs rs = optionalRs.get(); rs.setAmount(stuffDTO.getAmount()); - recipeStuffsRepository.save(rs); + RecipeStuffsRepository.save(rs); incomingStuffIds.add(rs.getStuff().getStuffId()); } else { // オプション:見つからない場合、新しいRecipeStuffsエントリを作成 @@ -191,17 +191,17 @@ public class RecipeService { rs.setRecipes(recipe); rs.setStuff(existingStuff); rs.setAmount(stuffDTO.getAmount()); - recipeStuffsRepository.save(rs); + RecipeStuffsRepository.save(rs); incomingStuffIds.add(existingStuff.getStuffId()); } } } // 入ってきたリストにないRecipeStuffsエントリを削除 - List existingStuffs = recipeStuffsRepository.findByRecipesRecipeId(dto.getRecipeId()); + List existingStuffs = RecipeStuffsRepository.findByRecipesRecipeId(dto.getRecipeId()); for (RecipeStuffs rs : existingStuffs) { if (!incomingStuffIds.contains(rs.getStuff().getStuffId())) { - recipeStuffsRepository.delete(rs); + RecipeStuffsRepository.delete(rs); } } 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 971fa4b..8172606 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -10,11 +10,14 @@ package com.example.todoapp.service; import com.example.todoapp.util.MessageUtils; import com.example.todoapp.dto.BuyRequestDTO; +import com.example.todoapp.dto.ToBuyResponseDTO; import com.example.todoapp.dto.ToBuysDTO; +import com.example.todoapp.model.RecipeStuffs; 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.RecipeStuffsRepository; import com.example.todoapp.repository.StocksRepository; import com.example.todoapp.repository.StuffsRepository; import com.example.todoapp.repository.ToBuysRepository; @@ -27,6 +30,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -56,6 +60,8 @@ public class ToBuysService { @Autowired private MessageUtils messageUtils; + @Autowired + private RecipeStuffsRepository RecipeStuffsRepository; /** * 購入リストに新しいアイテムを追加する @@ -201,6 +207,58 @@ public class ToBuysService { return stocksRepository.save(stock); } + /** + * 指定されたレシピIDに基づいて「買うもの」を追加する + * + * @param recipeId レシピID + * @param authentication 認証情報 + * @return 追加された「買うもの」のリスト + */ + public List addByRecipeId(Long recipeId, Authentication authentication) { + // ユーザー情報を取得 + String username = authentication.getName(); + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); + + // 料理情報を取得 + List recipeStuffsList = RecipeStuffsRepository.findByRecipesRecipeId(recipeId); + + List result = new ArrayList<>(); + + for (RecipeStuffs rs : recipeStuffsList) { + Stuffs stuff = rs.getStuff(); + int requiredAmount = rs.getAmount(); + + Optional existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff); + + ToBuys toBuy; + if (existingToBuyOpt.isPresent()) { + toBuy = existingToBuyOpt.get(); + toBuy.setAmount(toBuy.getAmount() + requiredAmount); // 既存の数量を更新 + } else { + toBuy = new ToBuys(); + toBuy.setUser(user); + toBuy.setStuff(stuff); + toBuy.setAmount(requiredAmount); + toBuy.setStore(""); + } + + toBuy = toBuysRepository.save(toBuy); + + // ToBuyResponseDTOに変換 + ToBuyResponseDTO dto = new ToBuyResponseDTO(); + dto.setTobuyId(toBuy.getTobuyId()); + dto.setStuffId(toBuy.getStuff().getStuffId()); + dto.setStuffName(toBuy.getStuff().getStuffName()); + dto.setAmount(toBuy.getAmount()); + + result.add(dto); + } + + return result; + + } + /** * ユーザー名からユーザーエンティティを取得する * @@ -213,4 +271,7 @@ public class ToBuysService { .orElseThrow(() -> new UsernameNotFoundException(messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[]{username}))); } + + + } \ No newline at end of file