削除を実装しました

backend-tobuy-adddish
zhang.pengcheng 5 months ago
parent e57d01de15
commit 2e2214c908
  1. 22
      backend/src/main/java/com/example/todoapp/controller/ToBuysController.java
  2. 9
      backend/src/main/java/com/example/todoapp/dto/DeleteToBuyRequest.java
  3. 1
      backend/src/main/java/com/example/todoapp/dto/ToBuyResponse.java
  4. 15
      backend/src/main/java/com/example/todoapp/repository/ToBuysRepository.java
  5. 14
      backend/src/main/java/com/example/todoapp/service/ToBuysService.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<Map<String, Boolean>> deleteToBuy(@RequestBody DeleteToBuyRequest request) {
int deletedCount = toBuysService.deleteToBuyByIds(request.getUser_id(), request.getTobuy_id());
Map<String, Boolean> response = new HashMap<>();
if (deletedCount > 0) {
response.put("result", true);
} else {
response.put("result", false);
}
return ResponseEntity.ok(response);
}
}

@ -0,0 +1,9 @@
package com.example.todoapp.dto;
import lombok.Data;
@Data
public class DeleteToBuyRequest {
private Long user_id;
private int tobuy_id;
}

@ -1,4 +1,3 @@
// src/main/java/com/example/todoapp/dto/ToBuyResponse.java
package com.example.todoapp.dto;
import lombok.Data;

@ -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<ToBuys, Integer> {
/**
* 指定されたユーザーIDに基づいて買うものリストを取得する
*
* @param userId ユーザーID
* @return 買うものリスト
*/
@Query("SELECT t FROM ToBuys t WHERE t.user_id.id = ?1")
List<ToBuys> 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);
}

@ -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<ToBuys> 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);
}
}
Loading…
Cancel
Save