diff --git a/backend/src/main/java/com/example/todoapp/controller/StocksController.java b/backend/src/main/java/com/example/todoapp/controller/StocksController.java new file mode 100644 index 0000000..8bdf482 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/controller/StocksController.java @@ -0,0 +1,106 @@ +package com.example.todoapp.controller; + +import com.example.todoapp.dto.StockDTO; +import com.example.todoapp.model.Stocks; +import com.example.todoapp.service.StocksService; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 在庫管理のコントローラー + *
+ * このコントローラーは在庫の取得、作成、更新、削除などの + * エンドポイントを提供します。すべてのエンドポイントは認証が必要です。 + *
+ */ +@RestController +@RequestMapping("/stocks") +public class StocksController { + + @Autowired + private StocksService stockService; + + /** + * ログインユーザーのすべての在庫を取得する + * + * @param authentication 認証情報 + * @return ユーザーの在庫リスト + */ + @GetMapping + public ResponseEntity+ * このクラスはクライアントとサーバー間で在庫情報をやり取りするために使用されます。 + * エンティティとは異なり、必要な情報のみを含み、関連エンティティへの参照ではなくIDのみを保持します。 + *
+ */ + +@Data +public class StockDTO { + private Long stock_id; + private Long stuff_id; + private Long user_id; + private int amount; + private int price; + private LocalDate buy_date; + private LocalDate last_update; + private LocalDate exp_date; + + /** + * 在庫エンティティからDTOを作成する + * + * @param stock 変換元の在庫エンティティ + * @return 変換されたStockDTOオブジェクト + */ + public static StockDTO fromEntity(Stocks stock) { + StockDTO dto = new StockDTO(); + dto.setStock_id(stock.getStock_id()); + dto.setStuff_id(stock.getStuff_id()); + dto.setUser_id(stock.getUser_id().getId()); + dto.setAmount(stock.getAmount()); + dto.setPrice(stock.getPrice()); + dto.setBuy_date(stock.getBuy_date()); + dto.setLast_update(stock.getLast_update()); + dto.setExp_date(stock.getExp_date()); + return dto; + } +} \ No newline at end of file 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 8c7eca6..1b5b067 100644 --- a/backend/src/main/java/com/example/todoapp/model/Stocks.java +++ b/backend/src/main/java/com/example/todoapp/model/Stocks.java @@ -39,8 +39,7 @@ public class Stocks { */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name="stockId") - private Long stockId ; + private Long stockI ; /** diff --git a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java index 35b2dd4..b121415 100644 --- a/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/StocksRepository.java @@ -1,5 +1,5 @@ //-------------------------------- -// StocksRepository.java +// ToBuysRepository.java // // // 更新履歴:2025/06/05 新規作成 diff --git a/backend/src/main/java/com/example/todoapp/service/StocksService.java b/backend/src/main/java/com/example/todoapp/service/StocksService.java new file mode 100644 index 0000000..85580d2 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/service/StocksService.java @@ -0,0 +1,112 @@ +package com.example.todoapp.service; + +import com.example.todoapp.model.Stocks; +import com.example.todoapp.util.MessageUtils; +import com.example.todoapp.model.User; +import com.example.todoapp.repository.StocksRepository; +import com.example.todoapp.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Stocksテーブルのサービスクラス + *+ * このクラスは在庫の追加、取得、更新、削除などのビジネスロジックを提供します。 + *
+ */ +@Service +public class StocksService { + @Autowired + private StocksRepository stocksRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private MessageUtils messageUtils; + + /** + * 新たな在庫を追加する + * + * @param username ユーザー名 + * @param stock 追加する在庫情報 + * @return 保存された在庫エンティティ + */ + public Stocks createStock(String username, Stocks stock) { + User user = getUserByUsername(username); + stock.setUser_id(user); + return stocksRepository.save(stock); + + } + + /** + * 指定されたユーザのすべての在庫を取得する + * @param username ユーザー名 + * @return ユーザーの在庫リスト(stock_id昇順) + */ + public List