From 7dc49cacdbba03c5548e9405055faf67acab7e4a Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Thu, 12 Jun 2025 16:03:44 +0900 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E3=81=98=E5=90=8D=E5=89=8D=E3=81=AE?= =?UTF-8?q?=E9=A3=9F=E6=9D=90=E3=81=8C=E3=81=82=E3=82=8B=E5=A0=B4=E5=90=88?= =?UTF-8?q?=E3=81=A7=E3=82=82=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=82=89=E3=81=AA=E3=81=84=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/service/RecipeService.java | 21 +++++--- .../todoapp/service/ToBuysService.java | 50 +++++++++---------- 2 files changed, 36 insertions(+), 35 deletions(-) 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 c2a6a41..3329420 100644 --- a/backend/src/main/java/com/example/todoapp/service/RecipeService.java +++ b/backend/src/main/java/com/example/todoapp/service/RecipeService.java @@ -64,17 +64,17 @@ public class RecipeService { List recipeStuffsList = new ArrayList<>(); for (StuffRequestDTO stuffDTO : dto.getStuffAndAmountArray()) { + Optional optStuff = stuffsRepository.findByStuffName(stuffDTO.getStuffName()); Stuffs stuff; - if (stuffDTO.getStuffId() != null && !stuffDTO.getStuffId().isEmpty()) { - // stuffIdが存在する - stuff = stuffsRepository.findById(Long.valueOf(stuffDTO.getStuffId())) - .orElseThrow(() -> new RuntimeException("材料がありません")); - } else { + + if (optStuff.isEmpty()) { // stuffIdが存在しない stuff = new Stuffs(); stuff.setStuffName(stuffDTO.getStuffName()); stuff.setCategory(stuffDTO.getCategory()); stuff = stuffsRepository.save(stuff); + } else { + stuff = optStuff.get(); } RecipeStuffs recipeStuffs = new RecipeStuffs(); @@ -158,11 +158,14 @@ public class RecipeService { // 提供された材料の詳細を繰り返し処理 for (StuffDetailDTO stuffDTO : dto.getStuffAndAmountArray()) { - if (stuffDTO.getStuffId() == null) { + + Optional optStuff = stuffsRepository.findByStuffName(stuffDTO.getStuffName()); + + if (optStuff.isEmpty()) { // 材料IDがnullの場合、新しい材料を作成 Stuffs newStuff = new Stuffs(); newStuff.setStuffName(stuffDTO.getStuffName()); - newStuff.setCategory("その他"); + newStuff.setCategory(stuffDTO.getCategory()); newStuff = stuffsRepository.save(newStuff); // 新しいRecipeStuffsエントリを作成 @@ -174,9 +177,11 @@ public class RecipeService { incomingStuffIds.add(newStuff.getStuffId()); } else { + Long stuffId = optStuff.get().getStuffId(); + // 材料IDが提供されている場合、既存のRecipeStuffsエントリを検索 Optional optionalRs = recipeStuffsRepository - .findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffDTO.getStuffId()); + .findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffId); if (optionalRs.isPresent()) { // RecipeStuffsエントリが存在する場合、数量を更新 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 8172606..678aae6 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -53,7 +53,7 @@ public class ToBuysService { @Autowired private StuffsRepository stuffsRepository; - + @Autowired private StocksRepository stocksRepository; @@ -66,7 +66,7 @@ public class ToBuysService { /** * 購入リストに新しいアイテムを追加する * - * @param toBuyDTO 追加する購入アイテムのデータ(DTO) + * @param toBuyDTO 追加する購入アイテムのデータ(DTO) * @param authentication 認証情報 * @return 追加された購入アイテム */ @@ -77,20 +77,18 @@ public class ToBuysService { User user = userRepository.findByUsername(username) .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); + Optional optStuff = stuffsRepository.findByStuffName(toBuyDTO.getStuffName()); Stuffs stuff; - if (toBuyDTO.getStuffId() == null) { + + if (optStuff.isEmpty()) { // 新しい材料を作成 stuff = new Stuffs(); stuff.setStuffName(toBuyDTO.getStuffName()); stuff.setCategory(toBuyDTO.getCategory()); - stuff = stuffsRepository.save(stuff); + stuff = stuffsRepository.save(stuff); } else { // 材料情報を取得 - Optional optionalStuffs = stuffsRepository.findByStuffId(toBuyDTO.getStuffId()); - if (!optionalStuffs.isPresent()) { - throw new RuntimeException("材料がありません"); - } - stuff = optionalStuffs.get(); + stuff = optStuff.get(); } ToBuys toBuys = new ToBuys(); @@ -101,7 +99,7 @@ public class ToBuysService { // データベースに保存 return toBuysRepository.save(toBuys); - + } /** @@ -109,7 +107,7 @@ public class ToBuysService { * * @param toBuyDTO 変更する購入アイテムのデータ(DTO) */ - public ToBuys updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { + public ToBuys updateToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { // ユーザー情報を取得 String username = authentication.getName(); @@ -138,9 +136,9 @@ public class ToBuysService { if (!optionalStuffs.isPresent()) { throw new RuntimeException("材料がありません"); } - stuffs = optionalStuffs.get(); - - //update + stuffs = optionalStuffs.get(); + + // update stuffs = stuffsRepository.save(stuffs); } @@ -179,7 +177,7 @@ public class ToBuysService { * 指定されたユーザーIDと購入データに基づいて「買うもの」を購入する * * @param username ユーザーID - * @param dto 購入データ + * @param dto 購入データ */ @Transactional public Stocks buyToBuys(String username, BuyRequestDTO dto) { @@ -198,11 +196,11 @@ public class ToBuysService { stock.setLastUpdate(dto.getLastUpdate()); stock.setBuyDate(dto.getBuyDate()); stock.setExpDate(dto.getExpDate()); - - // 買うものリストから削除 + + // 買うものリストから削除 System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); deleteToBuysByTobuyId(tobuy.getTobuyId()); - + // データベースに保存 return stocksRepository.save(stock); } @@ -210,19 +208,19 @@ public class ToBuysService { /** * 指定されたレシピIDに基づいて「買うもの」を追加する * - * @param recipeId レシピID + * @param recipeId レシピID * @param authentication 認証情報 * @return 追加された「買うもの」のリスト */ - public List addByRecipeId(Long recipeId, Authentication authentication) { + 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) { @@ -256,7 +254,7 @@ public class ToBuysService { } return result; - + } /** @@ -268,10 +266,8 @@ public class ToBuysService { */ private User getUserByUsername(String username) { return userRepository.findByUsername(username) - .orElseThrow(() -> new UsernameNotFoundException(messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[]{username}))); + .orElseThrow(() -> new UsernameNotFoundException( + messageUtils.getMessage("error.auth.user.not.found.with.name", new Object[] { username }))); } - - - } \ No newline at end of file