diff --git a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java index fbb0603..ceb10df 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -1,3 +1,10 @@ +//-------------------------------- +// ToBuysController.java +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- + package com.example.todoapp.controller; import com.example.todoapp.dto.ToBuyDTO; @@ -7,13 +14,27 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +/** + * 購入リストに関するRESTコントローラー + *

+ * このコントローラーは、購入リスト (to_buys) へのアイテム追加機能を提供します。 + * リクエストボディには ToBuyDTO 形式のデータが期待されます。 + *

+ */ @RestController -@RequestMapping("/api/tobuy/add") +@RequestMapping("/tobuy/add") public class ToBuysController { @Autowired private ToBuysService toBuysService; + /** + * 新しい購入アイテムを追加する + * + * @param dto 追加する購入アイテムのデータ(DTO) + * @return 成功時のレスポンスメッセージ + */ + @PostMapping public ResponseEntity addToBuys(@Valid @RequestBody ToBuyDTO dto) { toBuysService.addToBuys(dto); diff --git a/backend/src/main/java/com/example/todoapp/model/Recipes.java b/backend/src/main/java/com/example/todoapp/model/Recipes.java index 76de887..98d3936 100644 --- a/backend/src/main/java/com/example/todoapp/model/Recipes.java +++ b/backend/src/main/java/com/example/todoapp/model/Recipes.java @@ -15,7 +15,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @@ -40,7 +40,7 @@ public class Recipes { /** * カテゴリ名 */ - @NotBlank + @NotNull @Column(unique = true, length = 255, nullable = false) private String recipie_name; diff --git a/backend/src/main/java/com/example/todoapp/model/Stocks.java b/backend/src/main/java/com/example/todoapp/model/Stocks.java index 660ab87..d9f3f42 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stocks.java +++ b/backend/src/main/java/com/example/todoapp/model/Stocks.java @@ -20,7 +20,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @@ -45,7 +45,7 @@ public class Stocks { /** * 商品テーブル参照用の外部キー */ - @NotBlank + @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( name = "stuff_id", @@ -58,7 +58,7 @@ public class Stocks { /** * ユーザーテーブル参照用の外部キー */ - @NotBlank + @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( name = "user_id", diff --git a/backend/src/main/java/com/example/todoapp/model/Stuffs.java b/backend/src/main/java/com/example/todoapp/model/Stuffs.java index b0e7939..55fe1f9 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stuffs.java +++ b/backend/src/main/java/com/example/todoapp/model/Stuffs.java @@ -15,7 +15,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @@ -35,12 +35,12 @@ import lombok.NoArgsConstructor; */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private int stuff_id ; + private Long stuff_id ; /** * カテゴリ名 */ - @NotBlank + @NotNull @Column(unique = true, length = 255, nullable = false) private String stuff_name; @@ -53,7 +53,7 @@ import lombok.NoArgsConstructor; /** * カテゴリ */ - @NotBlank + @NotNull @Column(nullable = false, length = 225) private String category = "その他" ; } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/model/ToBuys.java b/backend/src/main/java/com/example/todoapp/model/ToBuys.java index 25bb797..0670392 100644 --- a/backend/src/main/java/com/example/todoapp/model/ToBuys.java +++ b/backend/src/main/java/com/example/todoapp/model/ToBuys.java @@ -1,7 +1,6 @@ //-------------------------------- // ToBuys.java // -// 分類:社員管理システムV2・ビジネスロジック層 // // 更新履歴:2025/06/03 新規作成 // Copyright(c) 2025 IVIS All rights reserved. @@ -18,7 +17,7 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; @@ -42,7 +41,7 @@ public class ToBuys { /** * 材料の一意識別子 FK */ - @NotBlank + @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( name = "stuff_id", @@ -54,26 +53,25 @@ public class ToBuys { /** * ユーザーテーブル参照用の外部キー */ - @NotBlank + // @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn( name = "user_id", - referencedColumnName = "id", - nullable = false + referencedColumnName = "id" ) private User user_id; /** * 購入する数量 */ - @NotBlank + @NotNull @Column(nullable = false) private int amount = 1; /** * 購入するお店 */ - @NotBlank + @NotNull @Column(nullable = false) private String store; diff --git a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java index 3c29f31..1e2fa69 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java @@ -1,12 +1,25 @@ +//-------------------------------- +// StuffsRepository.java +// +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- + package com.example.todoapp.repository; import com.example.todoapp.model.Stuffs; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.Optional; - +/** + * 材料 (stuffs) テーブルへのアクセスを行うリポジトリ. + *

+ * このクラスは材料テーブル (stuffs) に対する基本的なCRUD操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ @Repository public interface StuffsRepository extends JpaRepository { - Optional findById(Long id); + // 材料情報を主キーで取得するメソッド(必要に応じて追加) } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java index ff6469f..368aef8 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -1,9 +1,23 @@ +//-------------------------------- +// ToBuysRepository.java +// +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- package com.example.todoapp.repository; import com.example.todoapp.model.ToBuys; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +/** + * to_buys テーブルへのアクセスを行うリポジトリ + *

+ * このクラスは to_buys テーブルに対する基本的なCRUD操作を提供します。 + * Spring Data JPAによって自動的に実装されます。 + *

+ */ @Repository public interface ToBuysRepository extends JpaRepository { } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java index 2a44acb..475879f 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -1,3 +1,11 @@ +//-------------------------------- +// ToBuysService.java +// +// +// 更新履歴:2025/06/05 新規作成 +// Copyright(c) 2025 IVIS All rights reserved. +//-------------------------------------------- + package com.example.todoapp.service; import com.example.todoapp.dto.ToBuyDTO; @@ -12,6 +20,14 @@ import org.springframework.stereotype.Service; import java.util.Optional; +/** + * 購入リストのサービスクラス + *

+ * このクラスは、購入リスト (to_buys) の登録処理を提供します。 + * 材料 (stuffs) の存在確認と新規作成、ユーザー情報の取得などを行います。 + *

+ */ + @Service public class ToBuysService { @@ -24,27 +40,42 @@ public class ToBuysService { @Autowired private StuffsRepository stuffsRepository; + + /** + * 購入リストに新しいアイテムを追加する + * + * @param toBuyDTO 追加する購入アイテムのデータ(DTO) + */ public void addToBuys(ToBuyDTO toBuyDTO) { - //get user - Optional user = userRepository.findById(toBuyDTO.getUserId()); - if (!user.isPresent()) { - throw new RuntimeException("no: " + toBuyDTO.getUserId()); - } + // ユーザー情報を取得 + User user = userRepository.findById(toBuyDTO.getUser_id()) + .orElseThrow(() -> new RuntimeException("用户不存在: " + toBuyDTO.getUser_id())); - //get stuffs - Optional stuffs = stuffsRepository.findById(toBuyDTO.getStuffsId()); - if (!stuffs.isPresent()) { - throw new RuntimeException("材料不存在"); + Stuffs stuffs; + if (toBuyDTO.getStuff_id() == null) { + // 新しい材料を作成 + stuffs = new Stuffs(); + stuffs.setStuff_name(toBuyDTO.getStuff_name()); + stuffs.setCategory(toBuyDTO.getCategory()); + stuffs = stuffsRepository.save(stuffs); + } else { + // 材料情報を取得 + Optional optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); + if (!optionalStuffs.isPresent()) { + throw new RuntimeException("材料不存在"); + } + stuffs = optionalStuffs.get(); } ToBuys toBuys = new ToBuys(); - toBuys.setUser_id(user.get()); - toBuys.setStuffs(stuffs.get()); + System.out.println("AAAAuser_id: " + toBuyDTO.getUser_id()); + toBuys.setUser_id(user); + toBuys.setStuffs(stuffs); toBuys.setAmount(toBuyDTO.getAmount()); - toBuys.setStore(toBuyDTO.getStore()); + toBuys.setStore(toBuyDTO.getShop()); - // 保存到数据库 + // データベースに保存 toBuysRepository.save(toBuys); }