エラーに対処

dev-backend-tobuy
Masaharu.Kato 5 months ago
parent 99d34a0eed
commit 184c3846cf
  1. 2
      backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java
  2. 2
      backend/src/main/java/com/example/todoapp/model/Stocks.java
  3. 19
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -14,7 +14,7 @@ import lombok.Data;
@Data @Data
public class ToBuysDTO { public class ToBuysDTO {
private int tobuy_id; private int tobuy_id;
private Integer stuff_id; private Long stuff_id;
private Long user_id; private Long user_id;
private int price; private int price;
private int amount; private int amount;

@ -52,7 +52,7 @@ public class Stocks {
referencedColumnName = "stuff_id", referencedColumnName = "stuff_id",
nullable = false nullable = false
) )
private int stuff_id; private Long stuff_id;
/** /**

@ -69,26 +69,25 @@ public class ToBuysService {
User user = userRepository.findByUsername(username) User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username));
Stuffs stuffs; Stuffs stuff;
if (toBuyDTO.getStuff_id() == null) { if (toBuyDTO.getStuff_id() == null) {
// 新しい材料を作成 // 新しい材料を作成
stuffs = new Stuffs(); stuff = new Stuffs();
stuffs.setStuff_name(toBuyDTO.getStuff_name()); stuff.setStuff_name(toBuyDTO.getStuff_name());
stuffs.setCategory(toBuyDTO.getCategory()); stuff.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs); stuff = stuffsRepository.save(stuff);
} else { } else {
// 材料情報を取得 // 材料情報を取得
int stuff_id = toBuyDTO.getStuff_id(); Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id());
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(stuff_id);
if (!optionalStuffs.isPresent()) { if (!optionalStuffs.isPresent()) {
throw new RuntimeException("材料がありません"); throw new RuntimeException("材料がありません");
} }
stuffs = optionalStuffs.get(); stuff = optionalStuffs.get();
} }
ToBuys toBuys = new ToBuys(); ToBuys toBuys = new ToBuys();
toBuys.setUser_id(user); toBuys.setUser_id(user);
toBuys.setStuffs(stuffs); toBuys.setStuff_id(stuff.getStuff_id());
toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop()); toBuys.setStore(toBuyDTO.getShop());
@ -134,7 +133,7 @@ public class ToBuysService {
ToBuys toBuys = new ToBuys(); ToBuys toBuys = new ToBuys();
toBuys.setUser_id(user); toBuys.setUser_id(user);
toBuys.setStuffs(stuffs); toBuys.setStuff_id(stuffs.getStuff_id());
toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop()); toBuys.setStore(toBuyDTO.getShop());

Loading…
Cancel
Save