エラーに対処

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
public class ToBuysDTO {
private int tobuy_id;
private Integer stuff_id;
private Long stuff_id;
private Long user_id;
private int price;
private int amount;

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

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

Loading…
Cancel
Save