|
|
|
@ -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 }))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |