build-passed

dev-backend-tobuy
Amagasu 5 months ago
parent a86180989b
commit c0c025dc40
  1. 4
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 2
      backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java
  3. 8
      backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java
  4. 8
      backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java
  5. 4
      backend/src/main/java/com/example/todoapp/model/Recipes.java
  6. 14
      backend/src/main/java/com/example/todoapp/model/Stocks.java
  7. 4
      backend/src/main/java/com/example/todoapp/model/Stuffs.java
  8. 10
      backend/src/main/java/com/example/todoapp/model/ToBuys.java
  9. 24
      backend/src/main/java/com/example/todoapp/repository/StocksRepository.java
  10. 2
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  11. 8
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  12. 16
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -32,7 +32,7 @@ import org.springframework.web.bind.annotation.*;
/** /**
* 購入リストに関するRESTコントローラー * 購入リストに関するRESTコントローラー
* <p> * <p>
* このコントローラーは購入リスト (to_buys) へのアイテム追加機能を提供します * このコントローラーは購入リスト (toBuys) へのアイテム追加機能を提供します
* リクエストボディには ToBuyDTO 形式のデータが期待されます * リクエストボディには ToBuyDTO 形式のデータが期待されます
* </p> * </p>
*/ */
@ -99,7 +99,7 @@ public class ToBuysController {
Stuffs stuff = toBuy.getStuff(); Stuffs stuff = toBuy.getStuff();
resp.setTobuyId(toBuy.getTobuyId()); resp.setTobuyId(toBuy.getTobuyId());
resp.setStuffId(stuff.getStuffId()); resp.setStuffId(stuff.getStuffId());
resp.setStuff_name(stuff.getStuffName()); resp.setStuffName(stuff.getStuffName());
resp.setAmount(toBuy.getAmount()); resp.setAmount(toBuy.getAmount());
resp.setShop(toBuy.getStore()); resp.setShop(toBuy.getStore());
return resp; return resp;

@ -6,7 +6,7 @@ import lombok.Data;
public class ToBuyResponse { public class ToBuyResponse {
private int tobuyId; private int tobuyId;
private Long stuffId; private Long stuffId;
private String stuff_name; private String stuffName;
private int amount; private int amount;
private String shop; private String shop;
} }

@ -19,9 +19,9 @@ public class ToBuysDTO {
private int price; private int price;
private int amount; private int amount;
private String shop; private String shop;
private String stuff_name; private String stuffName;
private String category; private String category;
private LocalDate exp_date; private LocalDate expDate;
private LocalDate last_update; private LocalDate lastUpdate;
private LocalDate buy_date; private LocalDate buyDate;
} }

@ -46,8 +46,8 @@ public class RecipeStuffs {
@NotBlank @NotBlank
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( @JoinColumn(
name = "recipe_id", name = "recipeId",
referencedColumnName = "recipe_id", referencedColumnName = "recipeId",
nullable = false nullable = false
) )
private Recipes recipes; private Recipes recipes;
@ -59,8 +59,8 @@ public class RecipeStuffs {
@NotBlank @NotBlank
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( @JoinColumn(
name = "stuff_id", name = "stuffId",
referencedColumnName = "stuff_id", referencedColumnName = "stuffId",
nullable = false nullable = false
) )
private Stuffs stuff; private Stuffs stuff;

@ -35,14 +35,14 @@ public class Recipes {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="recipe_id") @Column(name="recipeId")
private int recipeId ; private int recipeId ;
/** /**
* カテゴリ名 * カテゴリ名
*/ */
@NotNull @NotNull
@Column(name="recipe_name", unique = true, length = 255, nullable = false) @Column(name="recipeName", unique = true, length = 255, nullable = false)
private String recipieName; private String recipieName;
/** /**

@ -39,7 +39,7 @@ public class Stocks {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="stock_id") @Column(name="stockId")
private Long stockId ; private Long stockId ;
@ -49,8 +49,8 @@ public class Stocks {
@NotNull @NotNull
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( @JoinColumn(
name = "stuff_id", name = "stuffId",
referencedColumnName = "stuff_id", referencedColumnName = "stuffId",
nullable = false nullable = false
) )
private Stuffs stuff; private Stuffs stuff;
@ -62,7 +62,7 @@ public class Stocks {
@NotNull @NotNull
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( @JoinColumn(
name = "user_id", name = "userId",
referencedColumnName = "id", referencedColumnName = "id",
nullable = false nullable = false
) )
@ -84,18 +84,18 @@ public class Stocks {
* 購入日 * 購入日
*/ */
@Column(nullable = false) @Column(nullable = false)
private LocalDate buy_date; private LocalDate buyDate;
/** /**
* 最後の操作時間 * 最後の操作時間
*/ */
@Column(nullable = false) @Column(nullable = false)
private LocalDate last_update; private LocalDate lastUpdate;
/** /**
* 賞味期限 * 賞味期限
*/ */
@Column(nullable = false) @Column(nullable = false)
private LocalDate exp_date; private LocalDate expDate;
} }

@ -35,14 +35,14 @@ import lombok.NoArgsConstructor;
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "stuff_id") @Column(name = "stuffId")
private Long stuffId; private Long stuffId;
/** /**
* カテゴリ名 * カテゴリ名
*/ */
@NotNull @NotNull
@Column(name = "stuff_name", unique = true, length = 255, nullable = false) @Column(name = "stuffName", unique = true, length = 255, nullable = false)
private String stuffName; private String stuffName;
/** /**

@ -28,7 +28,7 @@ import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Entity @Entity
@Table(name = "to_buys") @Table(name = "toBuys")
public class ToBuys { public class ToBuys {
/** /**
@ -36,7 +36,7 @@ public class ToBuys {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tobuy_id") @Column(name = "tobuyId")
private int tobuyId ; private int tobuyId ;
/** /**
@ -45,8 +45,8 @@ public class ToBuys {
@NotNull @NotNull
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( @JoinColumn(
name = "stuff_id", name = "stuffId",
referencedColumnName = "stuff_id", referencedColumnName = "stuffId",
nullable = false nullable = false
) )
private Stuffs stuff; private Stuffs stuff;
@ -61,7 +61,7 @@ public class ToBuys {
* 多対一の関係で遅延ロードを使用 * 多対一の関係で遅延ロードを使用
*/ */
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false) @JoinColumn(name = "userId", nullable = false)
private User user; private User user;
/** /**

@ -8,7 +8,13 @@
package com.example.todoapp.repository; package com.example.todoapp.repository;
import com.example.todoapp.model.Stocks; import com.example.todoapp.model.Stocks;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@ -24,12 +30,12 @@ import java.util.List;
@Repository @Repository
public interface StocksRepository extends JpaRepository<Stocks, Long> { public interface StocksRepository extends JpaRepository<Stocks, Long> {
/** /**
* user_idから在庫一覧をstock_id順で取得する * userIdから在庫一覧をstockId順で取得する
* *
* @param user_id 検索するユーザーID * @param userId 検索するユーザーID
* @return 在庫リスト * @return 在庫リスト
*/ */
List<Stocks> findStocksByUser_id(Long user_id); List<Stocks> findStocksByUserId(Long userId);
/** /**
* 在庫情報を更新する * 在庫情報を更新する
@ -37,14 +43,20 @@ public interface StocksRepository extends JpaRepository<Stocks, Long> {
* @param stock 編集する新たな情報が入ったstockオブジェクト * @param stock 編集する新たな情報が入ったstockオブジェクト
* @return 編集に成功したらtrue * @return 編集に成功したらtrue
*/ */
boolean UpdateStockByStock_id(Stocks stock); // boolean updateStocksByStockId(Stocks stock);
//updateをクエリ作成にて実装
@Modifying
@Transactional
@Query("UPDATE Stocks s SET s.amount = :#{#stock.amount}, s.price = :#{#stock.price}, s.buyDate = :#{#stock.buyDate}, s.lastUpdate = :#{#stock.lastUpdate}, s.expDate = :#{#stock.expDate} WHERE s.stockId = :#{#stock.stockId}")
int updateStocksById(@Param("stock") Stocks stock);
/** /**
* 在庫リストから指定した食材を削除する * 在庫リストから指定した食材を削除する
* *
* @param stockId 削除する在庫 * @param stockId 削除する在庫
* @param userId 削除するユーザー * @param userId 削除するユーザー
* @return 削除した場合true
*/ */
boolean DeleteStockByStock_id(int stockId, Long userId); void deleteStocksByStockIdAndUserId(Long stockId, Long userId);
} }

@ -24,6 +24,6 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface StuffsRepository extends JpaRepository<Stuffs, Long> { public interface StuffsRepository extends JpaRepository<Stuffs, Long> {
// 材料情報を主キーで取得するメソッド(必要に応じて追加) // 材料情報を主キーで取得するメソッド(必要に応じて追加)
Stuffs findByStuffId(Long stuff_id); Stuffs findByStuffId(Long stuffId);
} }

@ -18,9 +18,9 @@ import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* to_buys テーブルへのアクセスを行うリポジトリ * toBuys テーブルへのアクセスを行うリポジトリ
* <p> * <p>
* このクラスは to_buys テーブルに対する基本的なCRUD操作を提供します * このクラスは toBuys テーブルに対する基本的なCRUD操作を提供します
* Spring Data JPAによって自動的に実装されます * Spring Data JPAによって自動的に実装されます
* </p> * </p>
*/ */
@ -34,7 +34,7 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
* @return 買うものリスト * @return 買うものリスト
*/ */
@Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1") @Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1")
List<ToBuys> findByUser(Long user_id); List<ToBuys> findByUser(Long userId);
/** /**
* 指定されたユーザーIDに基づいて買うものリストを取得する * 指定されたユーザーIDに基づいて買うものリストを取得する
@ -45,5 +45,5 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
*/ */
@Modifying @Modifying
@Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId") @Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId")
int deleteByUser_IdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId); int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId);
} }

@ -33,7 +33,7 @@ import java.util.Optional;
/** /**
* 購入リストのサービスクラス * 購入リストのサービスクラス
* <p> * <p>
* このクラスは購入リスト (to_buys) の登録処理を提供します * このクラスは購入リスト (toBuys) の登録処理を提供します
* 材料 (stuffs) の存在確認と新規作成ユーザー情報の取得などを行います * 材料 (stuffs) の存在確認と新規作成ユーザー情報の取得などを行います
* </p> * </p>
*/ */
@ -73,7 +73,7 @@ public class ToBuysService {
if (toBuyDTO.getStuffId() == null) { if (toBuyDTO.getStuffId() == null) {
// 新しい材料を作成 // 新しい材料を作成
stuff = new Stuffs(); stuff = new Stuffs();
stuff.setStuffName(toBuyDTO.getStuff_name()); stuff.setStuffName(toBuyDTO.getStuffName());
stuff.setCategory(toBuyDTO.getCategory()); stuff.setCategory(toBuyDTO.getCategory());
stuff = stuffsRepository.save(stuff); stuff = stuffsRepository.save(stuff);
} else { } else {
@ -112,7 +112,7 @@ public class ToBuysService {
if (toBuyDTO.getStuffId() == null) { if (toBuyDTO.getStuffId() == null) {
// 新しい材料を作成 // 新しい材料を作成
stuffs = new Stuffs(); stuffs = new Stuffs();
stuffs.setStuffName(toBuyDTO.getStuff_name()); stuffs.setStuffName(toBuyDTO.getStuffName());
stuffs.setCategory(toBuyDTO.getCategory()); stuffs.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs); stuffs = stuffsRepository.save(stuffs);
} else { } else {
@ -124,7 +124,7 @@ public class ToBuysService {
stuffs = optionalStuffs.get(); stuffs = optionalStuffs.get();
//update //update
// stuffs.setStuff_name(toBuyDTO.getStuff_name()); // stuffs.setStuffName(toBuyDTO.getStuffName());
// stuffs.setCategory(toBuyDTO.getCategory()); // stuffs.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs); stuffs = stuffsRepository.save(stuffs);
} }
@ -160,7 +160,7 @@ public class ToBuysService {
*/ */
@Transactional @Transactional
public int deleteToBuyByIds(Long userId, int tobuyId) { public int deleteToBuyByIds(Long userId, int tobuyId) {
return toBuysRepository.deleteByUser_IdAndTobuyId(userId, tobuyId); return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId);
} }
public Stocks buyToBuys(String username, ToBuysDTO dto) { public Stocks buyToBuys(String username, ToBuysDTO dto) {
@ -174,9 +174,9 @@ public class ToBuysService {
stock.setUser(user); stock.setUser(user);
stock.setAmount(dto.getAmount()); stock.setAmount(dto.getAmount());
stock.setPrice(dto.getPrice()); stock.setPrice(dto.getPrice());
stock.setLast_update(dto.getLast_update()); stock.setLastUpdate(dto.getLastUpdate());
stock.setBuy_date(dto.getBuy_date()); stock.setBuyDate(dto.getBuyDate());
stock.setExp_date(dto.getExp_date()); stock.setExpDate(dto.getExpDate());
deleteToBuyByIds(dto.getUserId(), dto.getTobuyId()); deleteToBuyByIds(dto.getUserId(), dto.getTobuyId());
// データベースに保存 // データベースに保存
return stocksRepository.save(stock); return stocksRepository.save(stock);

Loading…
Cancel
Save