1. toBuysController, toBuysService, toBuysRepository, stuffsRepositoryを作成

2. recipes, stuffs, tobuys, stocks の NotBlankとNotNullを修正
dev-backend-stocks
zhang.pengcheng 5 months ago
parent 840f236540
commit 44cd5183cd
  1. 23
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 4
      backend/src/main/java/com/example/todoapp/model/Recipes.java
  3. 6
      backend/src/main/java/com/example/todoapp/model/Stocks.java
  4. 8
      backend/src/main/java/com/example/todoapp/model/Stuffs.java
  5. 14
      backend/src/main/java/com/example/todoapp/model/ToBuys.java
  6. 19
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  7. 14
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  8. 57
      backend/src/main/java/com/example/todoapp/service/ToBuysService.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コントローラー
* <p>
* このコントローラーは購入リスト (to_buys) へのアイテム追加機能を提供します
* リクエストボディには ToBuyDTO 形式のデータが期待されます
* </p>
*/
@RestController
@RequestMapping("/api/tobuy/add")
@RequestMapping("/tobuy/add")
public class ToBuysController {
@Autowired
private ToBuysService toBuysService;
/**
* 新しい購入アイテムを追加する
*
* @param dto 追加する購入アイテムのデータDTO
* @return 成功時のレスポンスメッセージ
*/
@PostMapping
public ResponseEntity<String> addToBuys(@Valid @RequestBody ToBuyDTO dto) {
toBuysService.addToBuys(dto);

@ -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;

@ -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",

@ -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 = "その他" ;
}

@ -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;

@ -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) テーブルへのアクセスを行うリポジトリ.
* <p>
* このクラスは材料テーブル (stuffs) に対する基本的なCRUD操作を提供します
* Spring Data JPAによって自動的に実装されます
* </p>
*/
@Repository
public interface StuffsRepository extends JpaRepository<Stuffs, Long> {
Optional<Stuffs> findById(Long id);
// 材料情報を主キーで取得するメソッド(必要に応じて追加)
}

@ -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 テーブルへのアクセスを行うリポジトリ
* <p>
* このクラスは to_buys テーブルに対する基本的なCRUD操作を提供します
* Spring Data JPAによって自動的に実装されます
* </p>
*/
@Repository
public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
}

@ -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;
/**
* 購入リストのサービスクラス
* <p>
* このクラスは購入リスト (to_buys) の登録処理を提供します
* 材料 (stuffs) の存在確認と新規作成ユーザー情報の取得などを行います
* </p>
*/
@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> 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> 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<Stuffs> 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);
}

Loading…
Cancel
Save