|
|
@ -70,15 +70,15 @@ public class ToBuysService { |
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); |
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); |
|
|
|
|
|
|
|
|
|
|
|
Stuffs stuff; |
|
|
|
Stuffs stuff; |
|
|
|
if (toBuyDTO.getStuff_id() == null) { |
|
|
|
if (toBuyDTO.getStuffId() == null) { |
|
|
|
// 新しい材料を作成
|
|
|
|
// 新しい材料を作成
|
|
|
|
stuff = new Stuffs(); |
|
|
|
stuff = new Stuffs(); |
|
|
|
stuff.setStuff_name(toBuyDTO.getStuff_name()); |
|
|
|
stuff.setStuffName(toBuyDTO.getStuff_name()); |
|
|
|
stuff.setCategory(toBuyDTO.getCategory()); |
|
|
|
stuff.setCategory(toBuyDTO.getCategory()); |
|
|
|
stuff = stuffsRepository.save(stuff); |
|
|
|
stuff = stuffsRepository.save(stuff); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 材料情報を取得
|
|
|
|
// 材料情報を取得
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); |
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
} |
|
|
|
} |
|
|
@ -86,8 +86,8 @@ public class ToBuysService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
toBuys.setUser_id(user); |
|
|
|
toBuys.setUser(user); |
|
|
|
toBuys.setStuff_id(stuff.getStuff_id()); |
|
|
|
toBuys.setStuff(stuff); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
|
|
|
|
|
|
@ -109,15 +109,15 @@ public class ToBuysService { |
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); |
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); |
|
|
|
|
|
|
|
|
|
|
|
Stuffs stuffs; |
|
|
|
Stuffs stuffs; |
|
|
|
if (toBuyDTO.getStuff_id() == null) { |
|
|
|
if (toBuyDTO.getStuffId() == null) { |
|
|
|
// 新しい材料を作成
|
|
|
|
// 新しい材料を作成
|
|
|
|
stuffs = new Stuffs(); |
|
|
|
stuffs = new Stuffs(); |
|
|
|
stuffs.setStuff_name(toBuyDTO.getStuff_name()); |
|
|
|
stuffs.setStuffName(toBuyDTO.getStuff_name()); |
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
stuffs.setCategory(toBuyDTO.getCategory()); |
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
stuffs = stuffsRepository.save(stuffs); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 材料情報を取得
|
|
|
|
// 材料情報を取得
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); |
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
} |
|
|
|
} |
|
|
@ -132,8 +132,8 @@ public class ToBuysService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
toBuys.setUser_id(user); |
|
|
|
toBuys.setUser(user); |
|
|
|
toBuys.setStuff_id(stuffs.getStuff_id()); |
|
|
|
toBuys.setStuff(stuffs); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
toBuys.setStore(toBuyDTO.getShop()); |
|
|
|
|
|
|
|
|
|
|
@ -148,8 +148,8 @@ public class ToBuysService { |
|
|
|
* @param userId ユーザーID |
|
|
|
* @param userId ユーザーID |
|
|
|
* @return ユーザーに紐づく「買うもの」リスト |
|
|
|
* @return ユーザーに紐づく「買うもの」リスト |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<ToBuys> getToBuysByUserId(Long userId) { |
|
|
|
public List<ToBuys> getToBuysByUser(User user) { |
|
|
|
return toBuysRepository.findByUserId(userId); |
|
|
|
return toBuysRepository.findByUser(user.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -160,7 +160,7 @@ public class ToBuysService { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Transactional |
|
|
|
@Transactional |
|
|
|
public int deleteToBuyByIds(Long userId, int tobuyId) { |
|
|
|
public int deleteToBuyByIds(Long userId, int tobuyId) { |
|
|
|
return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); |
|
|
|
return toBuysRepository.deleteByUser_IdAndTobuyId(userId, tobuyId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Stocks buyToBuys(String username, ToBuysDTO dto) { |
|
|
|
public Stocks buyToBuys(String username, ToBuysDTO dto) { |
|
|
@ -168,14 +168,16 @@ public class ToBuysService { |
|
|
|
User user = getUserByUsername(username); |
|
|
|
User user = getUserByUsername(username); |
|
|
|
// 新しい在庫を作成
|
|
|
|
// 新しい在庫を作成
|
|
|
|
Stocks stock = new Stocks(); |
|
|
|
Stocks stock = new Stocks(); |
|
|
|
stock.setStuff_id(dto.getStuff_id()); |
|
|
|
Stuffs stuff = new Stuffs(); |
|
|
|
stock.setUser_id(user); |
|
|
|
stuff.setStuffId(dto.getStuffId()); |
|
|
|
|
|
|
|
stock.setStuff(stuff); |
|
|
|
|
|
|
|
stock.setUser(user); |
|
|
|
stock.setAmount(dto.getAmount()); |
|
|
|
stock.setAmount(dto.getAmount()); |
|
|
|
stock.setPrice(dto.getPrice()); |
|
|
|
stock.setPrice(dto.getPrice()); |
|
|
|
stock.setLast_update(dto.getLast_update()); |
|
|
|
stock.setLast_update(dto.getLast_update()); |
|
|
|
stock.setBuy_date(dto.getBuy_date()); |
|
|
|
stock.setBuy_date(dto.getBuy_date()); |
|
|
|
stock.setExp_date(dto.getExp_date()); |
|
|
|
stock.setExp_date(dto.getExp_date()); |
|
|
|
deleteToBuyByIds(dto.getUser_id(), dto.getTobuy_id()); |
|
|
|
deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); |
|
|
|
// データベースに保存
|
|
|
|
// データベースに保存
|
|
|
|
return stocksRepository.save(stock); |
|
|
|
return stocksRepository.save(stock); |
|
|
|
} |
|
|
|
} |
|
|
|