|
|
@ -8,7 +8,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
package com.example.todoapp.service; |
|
|
|
package com.example.todoapp.service; |
|
|
|
|
|
|
|
|
|
|
|
import com.example.todoapp.dto.ToBuyDTO; |
|
|
|
import com.example.todoapp.dto.ToBuysDTO; |
|
|
|
import com.example.todoapp.model.Stuffs; |
|
|
|
import com.example.todoapp.model.Stuffs; |
|
|
|
import com.example.todoapp.model.ToBuys; |
|
|
|
import com.example.todoapp.model.ToBuys; |
|
|
|
import com.example.todoapp.model.User; |
|
|
|
import com.example.todoapp.model.User; |
|
|
@ -18,6 +18,7 @@ import com.example.todoapp.repository.UserRepository; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -46,11 +47,11 @@ public class ToBuysService { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param toBuyDTO 追加する購入アイテムのデータ(DTO) |
|
|
|
* @param toBuyDTO 追加する購入アイテムのデータ(DTO) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void addToBuys(ToBuyDTO toBuyDTO) { |
|
|
|
public void addToBuys(ToBuysDTO toBuyDTO) { |
|
|
|
|
|
|
|
|
|
|
|
// ユーザー情報を取得
|
|
|
|
// ユーザー情報を取得
|
|
|
|
User user = userRepository.findById(toBuyDTO.getUser_id()) |
|
|
|
User user = userRepository.findById(toBuyDTO.getUser_id()) |
|
|
|
.orElseThrow(() -> new RuntimeException("用户不存在: " + toBuyDTO.getUser_id())); |
|
|
|
.orElseThrow(() -> new RuntimeException("ユーザーがありませ: " + toBuyDTO.getUser_id())); |
|
|
|
|
|
|
|
|
|
|
|
Stuffs stuffs; |
|
|
|
Stuffs stuffs; |
|
|
|
if (toBuyDTO.getStuff_id() == null) { |
|
|
|
if (toBuyDTO.getStuff_id() == null) { |
|
|
@ -63,13 +64,12 @@ public class ToBuysService { |
|
|
|
// 材料情報を取得
|
|
|
|
// 材料情報を取得
|
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); |
|
|
|
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
if (!optionalStuffs.isPresent()) { |
|
|
|
throw new RuntimeException("材料不存在"); |
|
|
|
throw new RuntimeException("材料がありません"); |
|
|
|
} |
|
|
|
} |
|
|
|
stuffs = optionalStuffs.get(); |
|
|
|
stuffs = optionalStuffs.get(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
ToBuys toBuys = new ToBuys(); |
|
|
|
System.out.println("AAAAuser_id: " + toBuyDTO.getUser_id()); |
|
|
|
|
|
|
|
toBuys.setUser_id(user); |
|
|
|
toBuys.setUser_id(user); |
|
|
|
toBuys.setStuffs(stuffs); |
|
|
|
toBuys.setStuffs(stuffs); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
|
toBuys.setAmount(toBuyDTO.getAmount()); |
|
|
@ -79,4 +79,14 @@ public class ToBuysService { |
|
|
|
toBuysRepository.save(toBuys); |
|
|
|
toBuysRepository.save(toBuys); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 指定されたユーザーIDに基づいてすべての「買うもの」リストを取得する |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param userId ユーザーID |
|
|
|
|
|
|
|
* @return ユーザーに紐づく「買うもの」リスト |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public List<ToBuys> getToBuysByUserId(Long userId) { |
|
|
|
|
|
|
|
return toBuysRepository.findByUserId(userId); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |