同じ名前の食材がある場合でもエラーにならないように修正

feature-frontend-dishedit-kato
Masaharu.Kato 4 months ago
parent 4279750e3b
commit 7dc49cacdb
  1. 21
      backend/src/main/java/com/example/todoapp/service/RecipeService.java
  2. 50
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -64,17 +64,17 @@ public class RecipeService {
List<RecipeStuffs> recipeStuffsList = new ArrayList<>();
for (StuffRequestDTO stuffDTO : dto.getStuffAndAmountArray()) {
Optional<Stuffs> 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<Stuffs> 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<RecipeStuffs> optionalRs = recipeStuffsRepository
.findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffDTO.getStuffId());
.findByRecipesRecipeIdAndStuffStuffId(dto.getRecipeId(), stuffId);
if (optionalRs.isPresent()) {
// RecipeStuffsエントリが存在する場合、数量を更新

@ -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<Stuffs> 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<Stuffs> 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<ToBuyResponseDTO> addByRecipeId(Long recipeId, Authentication authentication) {
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Authentication authentication) {
// ユーザー情報を取得
String username = authentication.getName();
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username));
// 料理情報を取得
// 料理情報を取得
List<RecipeStuffs> recipeStuffsList = RecipeStuffsRepository.findByRecipesRecipeId(recipeId);
List<ToBuyResponseDTO> 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 })));
}
}
Loading…
Cancel
Save