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

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

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

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

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

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

@ -39,7 +39,8 @@ public class Stocks {
*/
@Id
@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",
nullable = false
)
private Long stuff_id;
private Stuffs stuff;
/**
@ -65,7 +66,7 @@ public class Stocks {
referencedColumnName = "id",
nullable = false
)
private User user_id;
private User user;
/**
* 在庫数量デフォルト値: 1

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

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

@ -42,9 +42,9 @@ public interface StocksRepository extends JpaRepository<Stocks, Long> {
/**
* 在庫リストから指定した食材を削除する
*
* @param stock_id 削除する在庫
* @param user_id 削除するユーザー
* @param stockId 削除する在庫
* @param userId 削除するユーザー
* @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
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
* @return 買うものリスト
*/
@Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1")
List<ToBuys> findByUserId(Long user_id);
@Query("SELECT t FROM ToBuys t WHERE t.user.id = ?1")
List<ToBuys> findByUser(Long user_id);
/**
* 指定されたユーザーIDに基づいて買うものリストを取得する
@ -44,6 +44,6 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
* @return
*/
@Modifying
@Query("DELETE FROM ToBuys t WHERE t.user_id.id = :userId AND t.tobuy_id = :tobuyId")
int deleteByUserIdAndTobuyId(@Param("userId") Long userId, @Param("tobuyId") int 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);
}

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

Loading…
Cancel
Save