删除种子功能

Change-Id: Iae783e3b7568ef0a7d521ef6fa837bf07b8d1ac8
diff --git a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
index 2c507fc..7994287 100644
--- a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
+++ b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
@@ -152,6 +152,28 @@
         );
     }
 }
+@DeleteMapping("/delete/{torrentId}")
+public ResponseEntity<?> deleteTorrent(
+        @RequestParam("userid") Long userid,
+        @PathVariable Long torrentId) {
+        System.out.println("到这里了");
+
+    try {
+        // 验证权限并删除
+        torrentService.deleteTorrent(userid,torrentId);
+        return ResponseEntity.ok().body(
+                Map.of("success", true, "message", "种子删除成功")
+        );
+    } catch (SecurityException e) {
+        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(
+                Map.of("success", false, "message", e.getMessage())
+        );
+    } catch (Exception e) {
+        return ResponseEntity.internalServerError().body(
+                Map.of("success", false, "message", "删除失败: " + e.getMessage())
+        );
+    }
+}
 //@PostMapping("/upload")
 //public ResponseEntity<?> uploadTorrent(
 //        @RequestParam("userid") Long userid,
diff --git a/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDeleteControllerTest.java b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDeleteControllerTest.java
new file mode 100644
index 0000000..ad0bcdb
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDeleteControllerTest.java
@@ -0,0 +1,104 @@
+package com.pt5.pthouduan.ControllerTest;
+
+
+import com.pt5.pthouduan.controller.TorrentController;
+import com.pt5.pthouduan.service.TorrentService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+class TorrentDeleteControllerTest {
+
+    @Mock
+    private TorrentService torrentService;
+
+    @InjectMocks
+    private TorrentController torrentController;
+
+    private MockMvc mockMvc;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+        mockMvc = MockMvcBuilders.standaloneSetup(torrentController).build();
+    }
+
+    @Test
+    void deleteTorrent_WithPermission_ShouldReturnSuccess() throws Exception {
+        // Arrange
+        Long userid = 1L;
+        Long torrentId = 101L;
+
+        // 模拟服务层调用成功
+        doNothing().when(torrentService).deleteTorrent(userid, torrentId);
+
+        // Act & Assert
+        mockMvc.perform(delete("/torrent/delete/{torrentId}", torrentId)
+                        .param("userid", userid.toString()))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.success").value(true))
+                .andExpect(jsonPath("$.message").value("种子删除成功"));
+
+        // 验证服务层方法被调用
+        verify(torrentService, times(1)).deleteTorrent(userid, torrentId);
+    }
+
+    @Test
+    void deleteTorrent_WithoutPermission_ShouldReturnForbidden() throws Exception {
+        // Arrange
+        Long userid = 2L;
+        Long torrentId = 101L;
+
+        // 模拟服务层抛出SecurityException
+        doThrow(new SecurityException("无权删除此种子")).when(torrentService).deleteTorrent(userid, torrentId);
+
+        // Act & Assert
+        mockMvc.perform(delete("/torrent/delete/{torrentId}", torrentId)
+                        .param("userid", userid.toString()))
+                .andExpect(status().isForbidden())
+                .andExpect(jsonPath("$.success").value(false))
+                .andExpect(jsonPath("$.message").value("无权删除此种子"));
+
+        // 验证服务层方法被调用
+        verify(torrentService, times(1)).deleteTorrent(userid, torrentId);
+    }
+
+    @Test
+    void deleteTorrent_WhenServiceThrowsException_ShouldReturnInternalServerError() throws Exception {
+        // Arrange
+        Long userid = 1L;
+        Long torrentId = 101L;
+
+        // 模拟服务层抛出其他异常
+        doThrow(new RuntimeException("数据库错误")).when(torrentService).deleteTorrent(userid, torrentId);
+
+        // Act & Assert
+        mockMvc.perform(delete("/torrent/delete/{torrentId}", torrentId)
+                        .param("userid", userid.toString()))
+                .andExpect(status().isInternalServerError())
+                .andExpect(jsonPath("$.success").value(false))
+                .andExpect(jsonPath("$.message").value("删除失败: 数据库错误"));
+
+        // 验证服务层方法被调用
+        verify(torrentService, times(1)).deleteTorrent(userid, torrentId);
+    }
+
+    @Test
+    void deleteTorrent_WithoutUserIdParameter_ShouldReturnBadRequest() throws Exception {
+        // Arrange
+        Long torrentId = 101L;
+
+        // Act & Assert
+        mockMvc.perform(delete("/torrent/delete/{torrentId}", torrentId))
+                .andExpect(status().isBadRequest());
+    }
+}
\ No newline at end of file