torrentdetail功能
Change-Id: I9607bfd760b1e5ce02fa78a4e18a3fdb268ba388
diff --git a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
index e864f20..4cffef8 100644
--- a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
+++ b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
@@ -57,6 +57,58 @@
+ // 获取单个种子详情
+ @GetMapping("/{id}")
+ public ResponseEntity<?> getTorrentById(@PathVariable Long id) {
+ try {
+ Torrent torrent = torrentService.getTorrentById(id);
+ if (torrent == null) {
+ return ResponseEntity.notFound().build(); // 如果种子不存在,返回 404
+ }
+ System.out.println(torrent);
+ return ResponseEntity.ok(torrent); // 返回种子详情
+ } catch (Exception e) {
+ return ResponseEntity.badRequest().body("获取种子详情失败: " + e.getMessage());
+ }
+ }
+//未更新前端前
+// @PostMapping("/upload")
+// public ResponseEntity<?> uploadTorrent(
+// @RequestParam("file") MultipartFile torrentFile,
+// @RequestParam("title") String title,
+// @RequestParam("description") String description
+// //@AuthenticationPrincipal User user
+// //User user
+// ){
+// // 创建临时用户对象
+// User user = new User(1L,"testuser");
+// //user.setUserid(1L); // 设置测试用户ID
+// try{
+// //torrentService.Upload(torrentFile,title,description,user);
+// return torrentService.Upload(torrentFile, title, description, user);
+// }catch(Exception e){
+// return ResponseEntity.badRequest().body("Upload failed:" + e.getMessage());
+// }
+// }
+//@PostMapping("/upload")
+//public ResponseEntity<?> uploadTorrent(
+// @RequestParam("file") MultipartFile torrentFile,
+// @RequestParam("title") String title,
+// @RequestParam("description") String description,
+// @RequestParam("categoryId") Integer categoryId,
+// @RequestParam(value = "dpi",required = false) String dpi,
+// @RequestParam(value = "caption", required = false) String caption){
+// // 创建临时用户对象
+// User user = new User(1L,"testuser");
+// //user.setUserid(1L); // 设置测试用户ID
+// try{
+// //torrentService.Upload(torrentFile,title,description,user);
+// return torrentService.Upload(torrentFile, title, description, categoryId, dpi,caption,user);
+// }catch(Exception e){
+// return ResponseEntity.badRequest().body("Upload failed:" + e.getMessage());
+// }
+//}
+
@PostMapping("/upload")
public ResponseEntity<?> uploadTorrent(
@RequestParam("userid") Long userid,
diff --git a/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDetailControllerTest.java b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDetailControllerTest.java
new file mode 100644
index 0000000..6c0c33e
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentDetailControllerTest.java
@@ -0,0 +1,83 @@
+package com.pt5.pthouduan.ControllerTest;
+
+import com.pt5.pthouduan.controller.TorrentController;
+import com.pt5.pthouduan.entity.Torrent;
+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.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+class TorrentDetailControllerTest {
+
+ @Mock
+ private TorrentService torrentService;
+
+ @InjectMocks
+ private TorrentController torrentController;
+
+ private MockMvc mockMvc;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(torrentController).build();
+ }
+
+ @Test
+ void getTorrentById_ExistingId_ShouldReturnTorrent() throws Exception {
+ // Arrange
+ Long id = 1L;
+ Torrent torrent = new Torrent();
+ torrent.setTorrentid(id);
+
+ when(torrentService.getTorrentById(id)).thenReturn(torrent);
+
+ // Act & Assert
+ mockMvc.perform(get("/torrent/{id}", id))
+ .andExpect(status().isOk());
+ //.andExpect(jsonPath("$.id").value(id));
+
+ // 验证服务层方法被调用
+ verify(torrentService, times(1)).getTorrentById(id);
+ }
+
+ @Test
+ void getTorrentById_NonExistingId_ShouldReturnNotFound() throws Exception {
+ // Arrange
+ Long id = 999L;
+
+ when(torrentService.getTorrentById(id)).thenReturn(null);
+
+ // Act & Assert
+ mockMvc.perform(get("/torrent/{id}", id))
+ .andExpect(status().isNotFound());
+
+ // 验证服务层方法被调用
+ verify(torrentService, times(1)).getTorrentById(id);
+ }
+
+ @Test
+ void getTorrentById_WhenServiceThrowsException_ShouldReturnBadRequest() throws Exception {
+ // Arrange
+ Long id = 1L;
+
+ when(torrentService.getTorrentById(id)).thenThrow(new RuntimeException("Database error"));
+
+ // Act & Assert
+ mockMvc.perform(get("/torrent/{id}", id))
+ .andExpect(status().isBadRequest());
+ //.andExpect(jsonPath("$.message").value("获取种子详情失败: Database error"));
+
+ // 验证服务层方法被调用
+ verify(torrentService, times(1)).getTorrentById(id);
+ }
+}
\ No newline at end of file