コメントを修正しました

develop-banckend-test
zhang.pengcheng 4 months ago
parent 3c0b7596cc
commit fed5a2e923
  1. 2
      backend/src/main/java/com/example/todoapp/controller/StocksController.java
  2. 11
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  3. 1
      backend/src/main/java/com/example/todoapp/model/ToBuys.java
  4. 6
      backend/src/main/java/com/example/todoapp/repository/RecipeStuffsRepository.java
  5. 4
      backend/src/main/java/com/example/todoapp/repository/StuffsRepository.java
  6. 6
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  7. 16
      backend/src/main/java/com/example/todoapp/service/ToBuysService.java

@ -1,6 +1,5 @@
package com.example.todoapp.controller; package com.example.todoapp.controller;
import com.example.todoapp.dto.DeleteStockRequest;
import com.example.todoapp.dto.DeleteStockRequestDTO; import com.example.todoapp.dto.DeleteStockRequestDTO;
import com.example.todoapp.dto.StockResponseDTO; import com.example.todoapp.dto.StockResponseDTO;
import com.example.todoapp.dto.StockDTO; import com.example.todoapp.dto.StockDTO;
@ -10,7 +9,6 @@ import com.example.todoapp.model.User;
import com.example.todoapp.repository.UserRepository; import com.example.todoapp.repository.UserRepository;
import com.example.todoapp.service.StocksService; import com.example.todoapp.service.StocksService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;

@ -96,7 +96,7 @@ public class ToBuysController {
/** /**
* 指定されたユーザーIDに基づいてすべての買うものリストを取得する * 指定されたユーザーIDに基づいてすべての買うものリストを取得する
* *
* @param userId ユーザーID * @param authentication 認証情報
* @return ユーザーに紐づく買うものリスト * @return ユーザーに紐づく買うものリスト
*/ */
@GetMapping("/get") @GetMapping("/get")
@ -135,14 +135,14 @@ public class ToBuysController {
* ユーザーが指定したIDの買うものを削除する * ユーザーが指定したIDの買うものを削除する
* *
* @param request 削除する買うものの情報を含むリクエストボディ * @param request 削除する買うものの情報を含むリクエストボディ
* @param authentication 認証情報
* @return 削除が成功した場合にtrueを含むレスポンス * @return 削除が成功した場合にtrueを含むレスポンス
*/ */
@DeleteMapping("/delete") @DeleteMapping("/delete")
public ResponseEntity<Map<String, Boolean>> deleteToBuy( public ResponseEntity<Map<String, Boolean>> deleteToBuy(
@RequestBody DeleteToBuyRequestDTO request, @RequestBody DeleteToBuyRequestDTO request) {
Authentication authentication) {
int deletedCount = toBuysService.deleteToBuyById(request.getTobuyId()); int deletedCount = toBuysService.deleteToBuysByTobuyId(request.getTobuyId());
Map<String, Boolean> response = new HashMap<>(); Map<String, Boolean> response = new HashMap<>();
@ -160,7 +160,8 @@ public class ToBuysController {
* 買うものリストから削除し追加情報を付加して在庫リストに追加 * 買うものリストから削除し追加情報を付加して在庫リストに追加
* *
* @param request 買うものの情報を含むリクエストボディ * @param request 買うものの情報を含むリクエストボディ
* @return * @param authentication 認証情報
* @return 購入処理が成功した場合にtrueを含むレスポンス
*/ */
@PostMapping("/buy") @PostMapping("/buy")
public ResponseEntity<Map<String, Boolean>> buyToBuys( public ResponseEntity<Map<String, Boolean>> buyToBuys(

@ -74,7 +74,6 @@ public class ToBuys {
/** /**
* 購入するお店 * 購入するお店
*/ */
@NotNull
@Column(nullable = false) @Column(nullable = false)
private String store; private String store;

@ -9,16 +9,10 @@ package com.example.todoapp.repository;
import com.example.todoapp.model.RecipeStuffs; import com.example.todoapp.model.RecipeStuffs;
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;
/** /**
* 料理-材料エンティティのリポジトリインターフェース * 料理-材料エンティティのリポジトリインターフェース
* <p> * <p>

@ -16,6 +16,7 @@ import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
@ -28,7 +29,8 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface StuffsRepository extends JpaRepository<Stuffs, Long> { public interface StuffsRepository extends JpaRepository<Stuffs, Long> {
// 材料情報を主キーで取得するメソッド(必要に応じて追加) // 材料情報を主キーで取得するメソッド(必要に応じて追加)
Stuffs findByStuffId(Long stuffId); @Query("SELECT s FROM Stuffs s WHERE s.stuffId = ?1")
Optional<Stuffs> findByStuffId(Long stuffId);
Optional<Stuffs> findByStuffName(String stuffName); Optional<Stuffs> findByStuffName(String stuffName);

@ -34,7 +34,7 @@ public interface ToBuysRepository extends JpaRepository<ToBuys, Integer> {
* @return 買うものレコード * @return 買うものレコード
*/ */
@Query("SELECT t FROM ToBuys t WHERE t.tobuyId = ?1") @Query("SELECT t FROM ToBuys t WHERE t.tobuyId = ?1")
ToBuys findById(Long tobuyId); ToBuys findByTobuyId(Long tobuyId);
/** /**
* 指定されたユーザーIDに基づいて買うものリストを取得する * 指定されたユーザーIDに基づいて買うものリストを取得する
@ -43,13 +43,13 @@ 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 userId); List<ToBuys> findByUserIdOrderByTobuyIdAsc(Long userId);
/** /**
* 指定された買うものIDに基づいて買うものリストを削除 * 指定された買うものIDに基づいて買うものリストを削除
* *
* @param tobuyId 買うものID * @param tobuyId 買うものID
* @return * @return 削除された行数
*/ */
@Modifying @Modifying
@Query("DELETE FROM ToBuys t WHERE t.tobuyId = :tobuyId") @Query("DELETE FROM ToBuys t WHERE t.tobuyId = :tobuyId")

@ -61,6 +61,8 @@ public class ToBuysService {
* 購入リストに新しいアイテムを追加する * 購入リストに新しいアイテムを追加する
* *
* @param toBuyDTO 追加する購入アイテムのデータDTO * @param toBuyDTO 追加する購入アイテムのデータDTO
* @param authentication 認証情報
* @return 追加された購入アイテム
*/ */
public ToBuys addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) { public ToBuys addToBuys(ToBuysDTO toBuyDTO, Authentication authentication) {
@ -78,7 +80,7 @@ public class ToBuysService {
stuff = stuffsRepository.save(stuff); stuff = stuffsRepository.save(stuff);
} else { } else {
// 材料情報を取得 // 材料情報を取得
Optional<Stuffs> optionalStuffs = stuffsRepository.findById(toBuyDTO.getStuffId()); Optional<Stuffs> optionalStuffs = stuffsRepository.findByStuffId(toBuyDTO.getStuffId());
if (!optionalStuffs.isPresent()) { if (!optionalStuffs.isPresent()) {
throw new RuntimeException("材料がありません"); throw new RuntimeException("材料がありません");
} }
@ -114,11 +116,11 @@ public class ToBuysService {
Optional<Stuffs> existingStuffs = stuffsRepository.findByStuffName(toBuyDTO.getStuffName()); Optional<Stuffs> existingStuffs = stuffsRepository.findByStuffName(toBuyDTO.getStuffName());
// 新しい材料を作成 // 新しい材料を作成
if (existingStuffs.isPresent()) { if (existingStuffs.isPresent()) {
// 如果存在,更新已有材料的属性 // 材料が存在する場合、更新
stuffs = existingStuffs.get(); stuffs = existingStuffs.get();
stuffs.setCategory(toBuyDTO.getCategory()); // 可选:更新分类 stuffs.setCategory(toBuyDTO.getCategory()); // 可选:更新分类
} else { } else {
// 否则新建材料 // 材料が存在しない場合、新規作成
stuffs = new Stuffs(); stuffs = new Stuffs();
stuffs.setStuffName(toBuyDTO.getStuffName()); stuffs.setStuffName(toBuyDTO.getStuffName());
stuffs.setCategory(toBuyDTO.getCategory()); stuffs.setCategory(toBuyDTO.getCategory());
@ -154,7 +156,7 @@ public class ToBuysService {
* @return ユーザーに紐づく買うものリスト * @return ユーザーに紐づく買うものリスト
*/ */
public List<ToBuys> getToBuysByUser(User user) { public List<ToBuys> getToBuysByUser(User user) {
return toBuysRepository.findByUser(user.getId()); return toBuysRepository.findByUserIdOrderByTobuyIdAsc(user.getId());
} }
/** /**
@ -163,7 +165,7 @@ public class ToBuysService {
* @param tobuyId 購入リストID * @param tobuyId 購入リストID
*/ */
@Transactional @Transactional
public int deleteToBuyById(Long tobuyId) { public int deleteToBuysByTobuyId(Long tobuyId) {
return toBuysRepository.deleteByTobuyId(tobuyId); return toBuysRepository.deleteByTobuyId(tobuyId);
} }
@ -179,7 +181,7 @@ public class ToBuysService {
User user = getUserByUsername(username); User user = getUserByUsername(username);
// Tobuy情報の取得 // Tobuy情報の取得
ToBuys tobuy = toBuysRepository.findById(dto.getTobuyId()); ToBuys tobuy = toBuysRepository.findByTobuyId(dto.getTobuyId());
// 新しい在庫を作成 // 新しい在庫を作成
Stocks stock = new Stocks(); Stocks stock = new Stocks();
@ -193,7 +195,7 @@ public class ToBuysService {
// 買うものリストから削除 // 買うものリストから削除
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId());
deleteToBuyById(tobuy.getTobuyId()); deleteToBuysByTobuyId(tobuy.getTobuyId());
// データベースに保存 // データベースに保存
return stocksRepository.save(stock); return stocksRepository.save(stock);

Loading…
Cancel
Save