|
|
|
@ -9,7 +9,7 @@ |
|
|
|
|
package com.example.todoapp.service; |
|
|
|
|
|
|
|
|
|
import com.example.todoapp.util.MessageUtils; |
|
|
|
|
|
|
|
|
|
import com.example.todoapp.dto.BuyRequestDTO; |
|
|
|
|
import com.example.todoapp.dto.ToBuysDTO; |
|
|
|
|
import com.example.todoapp.model.Stocks; |
|
|
|
|
import com.example.todoapp.model.Stuffs; |
|
|
|
@ -148,9 +148,9 @@ public class ToBuysService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する |
|
|
|
|
* 指定されたユーザーに基づいてすべての「買うもの」リストを取得する |
|
|
|
|
* |
|
|
|
|
* @param userId ユーザーID |
|
|
|
|
* @param user ユーザー |
|
|
|
|
* @return ユーザーに紐づく「買うもの」リスト |
|
|
|
|
*/ |
|
|
|
|
public List<ToBuys> getToBuysByUser(User user) { |
|
|
|
@ -158,31 +158,43 @@ public class ToBuysService { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 指定されたユーザーIDと購入リストIDに基づいて「買うもの」を削除する |
|
|
|
|
* 指定された購入リストIDに基づいて「買うもの」を削除する |
|
|
|
|
* |
|
|
|
|
* @param userId ユーザーID |
|
|
|
|
* @param tobuyId 購入リストID |
|
|
|
|
*/ |
|
|
|
|
@Transactional |
|
|
|
|
public int deleteToBuyByIds(Long userId, Long tobuyId) { |
|
|
|
|
return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); |
|
|
|
|
public int deleteToBuyById(Long tobuyId) { |
|
|
|
|
return toBuysRepository.deleteByTobuyId(tobuyId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Stocks buyToBuys(String username, ToBuysDTO dto) { |
|
|
|
|
/** |
|
|
|
|
* 指定されたユーザーIDと購入データに基づいて「買うもの」を購入する |
|
|
|
|
* |
|
|
|
|
* @param username ユーザーID |
|
|
|
|
* @param dto 購入データ |
|
|
|
|
*/ |
|
|
|
|
@Transactional |
|
|
|
|
public Stocks buyToBuys(String username, BuyRequestDTO dto) { |
|
|
|
|
// ユーザー情報を取得
|
|
|
|
|
User user = getUserByUsername(username); |
|
|
|
|
|
|
|
|
|
// Tobuy情報の取得
|
|
|
|
|
ToBuys tobuy = toBuysRepository.findById(dto.getTobuyId()); |
|
|
|
|
|
|
|
|
|
// 新しい在庫を作成
|
|
|
|
|
Stocks stock = new Stocks(); |
|
|
|
|
Stuffs stuff = new Stuffs(); |
|
|
|
|
stuff.setStuffId(dto.getStuffId()); |
|
|
|
|
stock.setStuff(stuff); |
|
|
|
|
stock.setStuff(tobuy.getStuff()); |
|
|
|
|
stock.setUser(user); |
|
|
|
|
stock.setAmount(dto.getAmount()); |
|
|
|
|
stock.setAmount(tobuy.getAmount()); |
|
|
|
|
stock.setPrice(dto.getPrice()); |
|
|
|
|
stock.setLastUpdate(dto.getLastUpdate()); |
|
|
|
|
stock.setBuyDate(dto.getBuyDate()); |
|
|
|
|
stock.setExpDate(dto.getExpDate()); |
|
|
|
|
deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); |
|
|
|
|
|
|
|
|
|
// 買うものリストから削除
|
|
|
|
|
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); |
|
|
|
|
deleteToBuyById(tobuy.getTobuyId()); |
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
|
return stocksRepository.save(stock); |
|
|
|
|
} |
|
|
|
|