|
|
|
@ -24,7 +24,7 @@ import com.example.todoapp.dto.StuffRequestDTO; |
|
|
|
import com.example.todoapp.model.RecipeStuffs; |
|
|
|
import com.example.todoapp.model.RecipeStuffs; |
|
|
|
import com.example.todoapp.model.Recipes; |
|
|
|
import com.example.todoapp.model.Recipes; |
|
|
|
import com.example.todoapp.model.Stuffs; |
|
|
|
import com.example.todoapp.model.Stuffs; |
|
|
|
import com.example.todoapp.repository.RecipeStuffsRepository; |
|
|
|
import com.example.todoapp.repository.RecipesStuffsRepository; |
|
|
|
import com.example.todoapp.repository.RecipesRepository; |
|
|
|
import com.example.todoapp.repository.RecipesRepository; |
|
|
|
import com.example.todoapp.repository.StuffsRepository; |
|
|
|
import com.example.todoapp.repository.StuffsRepository; |
|
|
|
|
|
|
|
|
|
|
|
@ -47,7 +47,7 @@ public class RecipeService { |
|
|
|
private StuffsRepository stuffsRepository; |
|
|
|
private StuffsRepository stuffsRepository; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private RecipeStuffsRepository recipeStuffsRepository; |
|
|
|
private RecipesStuffsRepository recipeStuffsRepository; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* レシピを新規登録する |
|
|
|
* レシピを新規登録する |
|
|
|
@ -94,20 +94,20 @@ public class RecipeService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 登録済のレシピを取得する |
|
|
|
* すべてのレシピ情報を取得する |
|
|
|
* |
|
|
|
* |
|
|
|
* @param authentication 認証情報 |
|
|
|
* @return 登録済みレシピ情報リスト |
|
|
|
* @return 登録済のレシピ情報 |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<Recipes> getAllRecipes() { |
|
|
|
public List<Recipes> getAllRecipes() { |
|
|
|
return recipesRepository.findAll(); |
|
|
|
return recipesRepository.findAll(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* レシピをIDで取得する |
|
|
|
* 指定されたIDのレシピ詳細情報を取得する |
|
|
|
* |
|
|
|
* |
|
|
|
* @param recipeId 検索するレシピID |
|
|
|
* @param recipeId 検索するレシピID |
|
|
|
* @return 検索結果のレシピ情報 |
|
|
|
* @return レシピ詳細情報(レシピ基本情報と関連食材情報) |
|
|
|
|
|
|
|
* @throws RuntimeException レシピが見つからない場合 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public RecipeDetailDTO getRecipeDetailsById(Long recipeId) { |
|
|
|
public RecipeDetailDTO getRecipeDetailsById(Long recipeId) { |
|
|
|
Recipes recipe = recipesRepository.findById(recipeId) |
|
|
|
Recipes recipe = recipesRepository.findById(recipeId) |
|
|
|
@ -134,25 +134,36 @@ public class RecipeService { |
|
|
|
return dto; |
|
|
|
return dto; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* レシピ情報を更新する |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param dto 更新するレシピ詳細情報 |
|
|
|
|
|
|
|
* @return 更新されたレシピエンティティ |
|
|
|
|
|
|
|
* @throws RuntimeException レシピまたは材料情報が見つからない場合 |
|
|
|
|
|
|
|
*/ |
|
|
|
@Transactional |
|
|
|
@Transactional |
|
|
|
public Recipes updateRecipe(RecipeDetailDTO dto) { |
|
|
|
public Recipes updateRecipe(RecipeDetailDTO dto) { |
|
|
|
|
|
|
|
// IDでレシピを検索し、見つからない場合は例外をスロー
|
|
|
|
Recipes recipe = recipesRepository.findById(dto.getRecipeId()) |
|
|
|
Recipes recipe = recipesRepository.findById(dto.getRecipeId()) |
|
|
|
.orElseThrow(() -> new RuntimeException("レシピが見つかりません")); |
|
|
|
.orElseThrow(() -> new RuntimeException("レシピが見つかりません")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// レシピ名と概要を更新
|
|
|
|
recipe.setRecipeName(dto.getRecipeName()); |
|
|
|
recipe.setRecipeName(dto.getRecipeName()); |
|
|
|
recipe.setSummary(dto.getSummary()); |
|
|
|
recipe.setSummary(dto.getSummary()); |
|
|
|
recipesRepository.save(recipe); |
|
|
|
recipesRepository.save(recipe); |
|
|
|
|
|
|
|
|
|
|
|
Set<Long> incomingStuffIds = new HashSet<>(); |
|
|
|
Set<Long> incomingStuffIds = new HashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提供された材料の詳細を繰り返し処理
|
|
|
|
for (StuffDetailDTO stuffDTO : dto.getStuffs()) { |
|
|
|
for (StuffDetailDTO stuffDTO : dto.getStuffs()) { |
|
|
|
if (stuffDTO.getStuffId() == null) { |
|
|
|
if (stuffDTO.getStuffId() == null) { |
|
|
|
|
|
|
|
// 材料IDがnullの場合、新しい材料を作成
|
|
|
|
Stuffs newStuff = new Stuffs(); |
|
|
|
Stuffs newStuff = new Stuffs(); |
|
|
|
newStuff.setStuffName(stuffDTO.getStuffName()); |
|
|
|
newStuff.setStuffName(stuffDTO.getStuffName()); |
|
|
|
newStuff.setCategory("その他"); |
|
|
|
newStuff.setCategory("その他"); |
|
|
|
newStuff = stuffsRepository.save(newStuff); |
|
|
|
newStuff = stuffsRepository.save(newStuff); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 新しいRecipeStuffsエントリを作成
|
|
|
|
RecipeStuffs rs = new RecipeStuffs(); |
|
|
|
RecipeStuffs rs = new RecipeStuffs(); |
|
|
|
rs.setRecipes(recipe); |
|
|
|
rs.setRecipes(recipe); |
|
|
|
rs.setStuff(newStuff); |
|
|
|
rs.setStuff(newStuff); |
|
|
|
@ -161,16 +172,18 @@ public class RecipeService { |
|
|
|
|
|
|
|
|
|
|
|
incomingStuffIds.add(newStuff.getStuffId()); |
|
|
|
incomingStuffIds.add(newStuff.getStuffId()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
// 材料IDが提供されている場合、既存のRecipeStuffsエントリを検索
|
|
|
|
Optional<RecipeStuffs> optionalRs = recipeStuffsRepository |
|
|
|
Optional<RecipeStuffs> optionalRs = recipeStuffsRepository |
|
|
|
.findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffDTO.getStuffId()); |
|
|
|
.findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffDTO.getStuffId()); |
|
|
|
|
|
|
|
|
|
|
|
if (optionalRs.isPresent()) { |
|
|
|
if (optionalRs.isPresent()) { |
|
|
|
|
|
|
|
// RecipeStuffsエントリが存在する場合、数量を更新
|
|
|
|
RecipeStuffs rs = optionalRs.get(); |
|
|
|
RecipeStuffs rs = optionalRs.get(); |
|
|
|
rs.setAmount(stuffDTO.getAmount()); |
|
|
|
rs.setAmount(stuffDTO.getAmount()); |
|
|
|
recipeStuffsRepository.save(rs); |
|
|
|
recipeStuffsRepository.save(rs); |
|
|
|
incomingStuffIds.add(rs.getStuff().getStuffId()); |
|
|
|
incomingStuffIds.add(rs.getStuff().getStuffId()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 可选:创建新的 RecipeStuffs
|
|
|
|
// オプション:見つからない場合、新しいRecipeStuffsエントリを作成
|
|
|
|
Stuffs existingStuff = stuffsRepository.findById(stuffDTO.getStuffId()) |
|
|
|
Stuffs existingStuff = stuffsRepository.findById(stuffDTO.getStuffId()) |
|
|
|
.orElseThrow(() -> new RuntimeException("材料が見つかりません")); |
|
|
|
.orElseThrow(() -> new RuntimeException("材料が見つかりません")); |
|
|
|
|
|
|
|
|
|
|
|
@ -184,9 +197,9 @@ public class RecipeService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 入ってきたリストにないRecipeStuffsエントリを削除
|
|
|
|
List<RecipeStuffs> existingStuffs = recipeStuffsRepository.findByRecipesRecipeId(dto.getRecipeId()); |
|
|
|
List<RecipeStuffs> existingStuffs = recipeStuffsRepository.findByRecipesRecipeId(dto.getRecipeId()); |
|
|
|
for (RecipeStuffs rs : existingStuffs) { |
|
|
|
for (RecipeStuffs rs : existingStuffs) { |
|
|
|
System.out.println("dddddwtestst" + rs.getStuff().getStuffId()); |
|
|
|
|
|
|
|
if (!incomingStuffIds.contains(rs.getStuff().getStuffId())) { |
|
|
|
if (!incomingStuffIds.contains(rs.getStuff().getStuffId())) { |
|
|
|
recipeStuffsRepository.delete(rs); |
|
|
|
recipeStuffsRepository.delete(rs); |
|
|
|
} |
|
|
|
} |
|
|
|
|