From 2e2214c90837615005ab5429808871eeb3d9cdc9 Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Thu, 5 Jun 2025 10:52:08 +0900 Subject: [PATCH] =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=82=92=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/ToBuysController.java | 22 +++++++++++++++++++ .../todoapp/dto/DeleteToBuyRequest.java | 9 ++++++++ .../example/todoapp/dto/ToBuyResponse.java | 1 - .../todoapp/repository/ToBuysRepository.java | 15 ++++++++++++- .../todoapp/service/ToBuysService.java | 14 ++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java diff --git a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java index 1efa39b..7398b01 100644 --- a/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java +++ b/backend/src/main/java/com/example/todoapp/controller/ToBuysController.java @@ -7,6 +7,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.ToBuys; @@ -76,4 +77,25 @@ public class ToBuysController { return ResponseEntity.ok(responseList); } + + /** + * ユーザーが指定したIDの「買うもの」を削除する + * + * @param request 削除する「買うもの」の情報を含むリクエストボディ + * @return 削除が成功した場合にtrueを含むレスポンス + */ + @DeleteMapping("/delete") + public ResponseEntity> deleteToBuy(@RequestBody DeleteToBuyRequest request) { + int deletedCount = toBuysService.deleteToBuyByIds(request.getUser_id(), request.getTobuy_id()); + + Map response = new HashMap<>(); + + if (deletedCount > 0) { + response.put("result", true); + } else { + response.put("result", false); + } + + return ResponseEntity.ok(response); + } } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java b/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java new file mode 100644 index 0000000..b19c196 --- /dev/null +++ b/backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java @@ -0,0 +1,9 @@ +package com.example.todoapp.dto; + +import lombok.Data; + +@Data +public class DeleteToBuyRequest { + private Long user_id; + private int tobuy_id; +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java index 2ffab6e..03809dc 100644 --- a/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java +++ b/backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java @@ -1,4 +1,3 @@ -// src/main/java/com/example/todoapp/dto/ToBuyResponse.java package com.example.todoapp.dto; import lombok.Data; diff --git a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java index 49029ea..57cd2cb 100644 --- a/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java +++ b/backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java @@ -12,7 +12,9 @@ import com.example.todoapp.model.ToBuys; import java.util.List; 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; /** @@ -24,13 +26,24 @@ import org.springframework.stereotype.Repository; */ @Repository public interface ToBuysRepository extends JpaRepository { + /** * 指定されたユーザーIDに基づいて「買うもの」リストを取得する * * @param userId ユーザーID * @return 「買うもの」リスト */ - @Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1") List findByUserId(Long user_id); + + /** + * 指定されたユーザーIDに基づいて「買うもの」リストを取得する + * + * @param userId ユーザーID + * @param tobuyId 「買うもの」ID + * @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); } \ No newline at end of file diff --git a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java index d1cd65c..589137c 100644 --- a/backend/src/main/java/com/example/todoapp/service/ToBuysService.java +++ b/backend/src/main/java/com/example/todoapp/service/ToBuysService.java @@ -15,6 +15,9 @@ import com.example.todoapp.model.User; import com.example.todoapp.repository.StuffsRepository; import com.example.todoapp.repository.ToBuysRepository; import com.example.todoapp.repository.UserRepository; + +import jakarta.transaction.Transactional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -89,4 +92,15 @@ public class ToBuysService { public List getToBuysByUserId(Long userId) { return toBuysRepository.findByUserId(userId); } + + /** + * 指定されたユーザーIDと購入リストIDに基づいて「買うもの」を削除する + * + * @param userId ユーザーID + * @param tobuyId 購入リストID + */ + @Transactional + public int deleteToBuyByIds(Long userId, int tobuyId) { + return toBuysRepository.deleteByUserIdAndTobuyId(userId, tobuyId); + } } \ No newline at end of file