WIP:バグ修正

dev-backend-tobuy
Masaharu.Kato 5 months ago
parent 067c5783e7
commit a86180989b
  1. 12
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 4
      backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java
  3. 4
      backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java
  4. 6
      backend/src/main/java/com/example/todoapp/dto/ToBuysDTO.java
  5. 5
      backend/src/main/java/com/example/todoapp/model/RecipeStuffs.java
  6. 7
      backend/src/main/java/com/example/todoapp/model/Recipes.java
  7. 7
      backend/src/main/java/com/example/todoapp/model/Stocks.java
  8. 7
      backend/src/main/java/com/example/todoapp/model/Stuffs.java
  9. 19
      backend/src/main/java/com/example/todoapp/model/ToBuys.java
  10. 6
      backend/src/main/java/com/example/todoapp/repository/StocksRepository.java
  11. 2
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  12. 8
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  13. 34
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -10,6 +10,7 @@ package com.example.todoapp.controller;
import com.example.todoapp.dto.DeleteToBuyRequest; import com.example.todoapp.dto.DeleteToBuyRequest;
import com.example.todoapp.dto.ToBuyResponse; import com.example.todoapp.dto.ToBuyResponse;
import com.example.todoapp.dto.ToBuysDTO; import com.example.todoapp.dto.ToBuysDTO;
import com.example.todoapp.model.Stuffs;
import com.example.todoapp.model.ToBuys; import com.example.todoapp.model.ToBuys;
import com.example.todoapp.model.User; import com.example.todoapp.model.User;
import com.example.todoapp.repository.UserRepository; import com.example.todoapp.repository.UserRepository;
@ -89,15 +90,16 @@ public class ToBuysController {
User user = userRepository.findByUsername(username) User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found")); .orElseThrow(() -> new UsernameNotFoundException("User not found"));
List<ToBuys> toBuysList = toBuysService.getToBuysByUserId(user.getId()); List<ToBuys> toBuysList = toBuysService.getToBuysByUser(user);
// DTO形式に変換して返す // DTO形式に変換して返す
List<ToBuyResponse> responseList = toBuysList.stream() List<ToBuyResponse> responseList = toBuysList.stream()
.map(toBuy -> { .map(toBuy -> {
ToBuyResponse resp = new ToBuyResponse(); ToBuyResponse resp = new ToBuyResponse();
resp.setTobuy_id(toBuy.getTobuy_id()); Stuffs stuff = toBuy.getStuff();
resp.setStuff_id(toBuy.getStuff_id()); resp.setTobuyId(toBuy.getTobuyId());
resp.setStuff_name(stuffsRepository.findByStuff_id(toBuy.getStuff_id()).getStuff_name()); resp.setStuffId(stuff.getStuffId());
resp.setStuff_name(stuff.getStuffName());
resp.setAmount(toBuy.getAmount()); resp.setAmount(toBuy.getAmount());
resp.setShop(toBuy.getStore()); resp.setShop(toBuy.getStore());
return resp; return resp;
@ -128,7 +130,7 @@ public class ToBuysController {
User user = userRepository.findByUsername(username) User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found")); .orElseThrow(() -> new UsernameNotFoundException("User not found"));
int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuy_id()); int deletedCount = toBuysService.deleteToBuyByIds(user.getId(), request.getTobuyId());
Map<String, Boolean> response = new HashMap<>(); Map<String, Boolean> response = new HashMap<>();

@ -4,6 +4,6 @@ import lombok.Data;
@Data @Data
public class DeleteToBuyRequest { public class DeleteToBuyRequest {
private Long user_id; private Long userId;
private int tobuy_id; private int tobuyId;
} }

@ -4,8 +4,8 @@ import lombok.Data;
@Data @Data
public class ToBuyResponse { public class ToBuyResponse {
private int tobuy_id; private int tobuyId;
private Long stuff_id; private Long stuffId;
private String stuff_name; private String stuff_name;
private int amount; private int amount;
private String shop; private String shop;

@ -13,9 +13,9 @@ import lombok.Data;
@Data @Data
public class ToBuysDTO { public class ToBuysDTO {
private int tobuy_id; private int tobuyId;
private Long stuff_id; private Long stuffId;
private Long user_id; private Long userId;
private int price; private int price;
private int amount; private int amount;
private String shop; private String shop;

@ -37,7 +37,8 @@ public class RecipeStuffs {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int recipe_stuffs_id ; @Column(name="recipe_stuffs_id")
private int recipeStuffsId ;
/** /**
* 料理の一意識別子 FK * 料理の一意識別子 FK
@ -62,7 +63,7 @@ public class RecipeStuffs {
referencedColumnName = "stuff_id", referencedColumnName = "stuff_id",
nullable = false nullable = false
) )
private Stuffs stuffs; private Stuffs stuff;
/** /**
* 材料の数量 * 材料の数量

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

@ -39,7 +39,8 @@ public class Stocks {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long stock_id ; @Column(name="stock_id")
private Long stockId ;
/** /**
@ -52,7 +53,7 @@ public class Stocks {
referencedColumnName = "stuff_id", referencedColumnName = "stuff_id",
nullable = false nullable = false
) )
private Long stuff_id; private Stuffs stuff;
/** /**
@ -65,7 +66,7 @@ public class Stocks {
referencedColumnName = "id", referencedColumnName = "id",
nullable = false nullable = false
) )
private User user_id; private User user;
/** /**
* 在庫数量デフォルト値: 1 * 在庫数量デフォルト値: 1

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

@ -36,7 +36,8 @@ public class ToBuys {
*/ */
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int tobuy_id ; @Column(name = "tobuy_id")
private int tobuyId ;
/** /**
* 材料の一意識別子 FK * 材料の一意識別子 FK
@ -48,18 +49,20 @@ public class ToBuys {
referencedColumnName = "stuff_id", referencedColumnName = "stuff_id",
nullable = false nullable = false
) )
private Long stuff_id; private Stuffs stuff;
/** /**
* ユーザーテーブル参照用の外部キー * ユーザーテーブル参照用の外部キー
*/ */
// @NotNull // @NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn( /**
name = "user_id", * タスクの所有者ユーザー
referencedColumnName = "id" * 多対一の関係で遅延ロードを使用
) */
private User user_id; @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
/** /**
* 購入する数量 * 購入する数量

@ -42,9 +42,9 @@ public interface StocksRepository extends JpaRepository<Stocks, Long> {
/** /**
* 在庫リストから指定した食材を削除する * 在庫リストから指定した食材を削除する
* *
* @param stock_id 削除する在庫 * @param stockId 削除する在庫
* @param user_id 削除するユーザー * @param userId 削除するユーザー
* @return 削除した場合true * @return 削除した場合true
*/ */
boolean DeleteStockByStock_id(int stock_id, Long user_id); boolean DeleteStockByStock_id(int 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 findByStuff_id(Long stuff_id); Stuffs findByStuffId(Long stuff_id);
} }

@ -33,8 +33,8 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
* @param userId ユーザーID * @param userId ユーザーID
* @return 買うものリスト * @return 買うものリスト
*/ */
@Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1") @Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1")
List<ToBuys> findByUserId(Long user_id); List<ToBuys> findByUser(Long user_id);
/** /**
* 指定されたユーザーIDに基づいて買うものリストを取得する * 指定されたユーザーIDに基づいて買うものリストを取得する
@ -44,6 +44,6 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
* @return * @return
*/ */
@Modifying @Modifying
@Query("DELETE FROM ToBuys t WHERE t.user_id.id = :userId AND t.tobuy_id = :tobuyId") @Query("DELETE FROM ToBuys t WHERE t.user.id = :userId AND t.tobuyId = :tobuyId")
int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId); int deleteByUser_IdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int tobuyId);
} }

@ -70,15 +70,15 @@ public class ToBuysService {
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username));
Stuffs stuff; Stuffs stuff;
if (toBuyDTO.getStuff_id() == null) { if (toBuyDTO.getStuffId() == null) {
// 新しい材料を作成 // 新しい材料を作成
stuff = new Stuffs(); stuff = new Stuffs();
stuff.setStuff_name(toBuyDTO.getStuff_name()); stuff.setStuffName(toBuyDTO.getStuff_name());
stuff.setCategory(toBuyDTO.getCategory()); stuff.setCategory(toBuyDTO.getCategory());
stuff = stuffsRepository.save(stuff); stuff = stuffsRepository.save(stuff);
} else { } else {
// 材料情報を取得 // 材料情報を取得
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId());
if (!optionalStuffs.isPresent()) { if (!optionalStuffs.isPresent()) {
throw new RuntimeException("材料がありません"); throw new RuntimeException("材料がありません");
} }
@ -86,8 +86,8 @@ public class ToBuysService {
} }
ToBuys toBuys = new ToBuys(); ToBuys toBuys = new ToBuys();
toBuys.setUser_id(user); toBuys.setUser(user);
toBuys.setStuff_id(stuff.getStuff_id()); toBuys.setStuff(stuff);
toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop()); toBuys.setStore(toBuyDTO.getShop());
@ -109,15 +109,15 @@ public class ToBuysService {
.orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username)); .orElseThrow(() -> new RuntimeException("ユーザーが見つかりません: " + username));
Stuffs stuffs; Stuffs stuffs;
if (toBuyDTO.getStuff_id() == null) { if (toBuyDTO.getStuffId() == null) {
// 新しい材料を作成 // 新しい材料を作成
stuffs = new Stuffs(); stuffs = new Stuffs();
stuffs.setStuff_name(toBuyDTO.getStuff_name()); stuffs.setStuffName(toBuyDTO.getStuff_name());
stuffs.setCategory(toBuyDTO.getCategory()); stuffs.setCategory(toBuyDTO.getCategory());
stuffs = stuffsRepository.save(stuffs); stuffs = stuffsRepository.save(stuffs);
} else { } else {
// 材料情報を取得 // 材料情報を取得
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuff_id()); Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId());
if (!optionalStuffs.isPresent()) { if (!optionalStuffs.isPresent()) {
throw new RuntimeException("材料がありません"); throw new RuntimeException("材料がありません");
} }
@ -132,8 +132,8 @@ public class ToBuysService {
ToBuys toBuys = new ToBuys(); ToBuys toBuys = new ToBuys();
toBuys.setUser_id(user); toBuys.setUser(user);
toBuys.setStuff_id(stuffs.getStuff_id()); toBuys.setStuff(stuffs);
toBuys.setAmount(toBuyDTO.getAmount()); toBuys.setAmount(toBuyDTO.getAmount());
toBuys.setStore(toBuyDTO.getShop()); toBuys.setStore(toBuyDTO.getShop());
@ -148,8 +148,8 @@ public class ToBuysService {
* @param userId ユーザーID * @param userId ユーザーID
* @return ユーザーに紐づく買うものリスト * @return ユーザーに紐づく買うものリスト
*/ */
public List<ToBuys> getToBuysByUserId(Long userId) { public List<ToBuys> getToBuysByUser(User user) {
return toBuysRepository.findByUserId(userId); return toBuysRepository.findByUser(user.getId());
} }
/** /**
@ -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.deleteByUserIdAndTobuyId(userId, tobuyId); return toBuysRepository.deleteByUser_IdAndTobuyId(userId, tobuyId);
} }
public Stocks buyToBuys(String username, ToBuysDTO dto) { public Stocks buyToBuys(String username, ToBuysDTO dto) {
@ -168,14 +168,16 @@ public class ToBuysService {
User user = getUserByUsername(username); User user = getUserByUsername(username);
// 新しい在庫を作成 // 新しい在庫を作成
Stocks stock = new Stocks(); Stocks stock = new Stocks();
stock.setStuff_id(dto.getStuff_id()); Stuffs stuff = new Stuffs();
stock.setUser_id(user); stuff.setStuffId(dto.getStuffId());
stock.setStuff(stuff);
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.setLast_update(dto.getLast_update());
stock.setBuy_date(dto.getBuy_date()); stock.setBuy_date(dto.getBuy_date());
stock.setExp_date(dto.getExp_date()); stock.setExp_date(dto.getExp_date());
deleteToBuyByIds(dto.getUser_id(), dto.getTobuy_id()); deleteToBuyByIds(dto.getUserId(), dto.getTobuyId());
// データベースに保存 // データベースに保存
return stocksRepository.save(stock); return stocksRepository.save(stock);
} }

Loading…
Cancel
Save