后端更新
Change-Id: I43c6dd58e1a21aa790357cdaee43477579abdc6d
diff --git a/src/main/java/com/ptp/ptplatform/controller/HelpCommentController.java b/src/main/java/com/ptp/ptplatform/controller/HelpCommentController.java
index 15504be..c937428 100644
--- a/src/main/java/com/ptp/ptplatform/controller/HelpCommentController.java
+++ b/src/main/java/com/ptp/ptplatform/controller/HelpCommentController.java
@@ -1,24 +1,56 @@
package com.ptp.ptplatform.controller;
+import com.ptp.ptplatform.entity.HelpComment;
import com.ptp.ptplatform.service.HelpCommentService;
import com.ptp.ptplatform.utils.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
+import java.time.LocalDateTime;
+import java.util.List;
+
@RestController
@RequestMapping("/help/comments")
@AllArgsConstructor
public class HelpCommentController {
-
private final HelpCommentService commentService;
- /**
- * 点赞评论
- * POST /help/comments/{commentId}/like
- */
@PostMapping("/{commentId}/like")
public Result like(@PathVariable int commentId) {
commentService.incrementLike(commentId);
return Result.ok();
}
+
+ // 获取评论的回复
+ @GetMapping("/{commentId}/replies")
+ public Result getReplies(@PathVariable int commentId) {
+ List<HelpComment> replies = commentService.getReplies(commentId);
+ return Result.ok().data("replies", replies);
+ }
+
+ @PostMapping("/{commentId}/replies")
+ public Result addReply(@PathVariable int commentId,
+ @RequestBody HelpComment reply) {
+ HelpComment parentComment = commentService.getById(commentId);
+ if (parentComment == null) {
+ return Result.error(404).setMessage("被回复的评论不存在");
+ }
+
+ // 设置完整字段
+ reply.setPostId(parentComment.getPostId());
+ reply.setParentId(commentId);
+ reply.setAuthorId(reply.getAuthorId());
+ reply.setReplyTo(parentComment.getAuthorId());
+ reply.setCreateTime(LocalDateTime.now()); // 设置创建时间
+ reply.setLikeCount(0); // 初始化点赞数
+
+ commentService.save(reply);
+
+ // 返回完整的评论数据
+ return Result.ok()
+ .data("reply", reply)
+ .data("parentCommentId", commentId); // 同时返回父评论ID
+ }
+
+
}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/HelpPostController.java b/src/main/java/com/ptp/ptplatform/controller/HelpPostController.java
index 5fb626c..646ed76 100644
--- a/src/main/java/com/ptp/ptplatform/controller/HelpPostController.java
+++ b/src/main/java/com/ptp/ptplatform/controller/HelpPostController.java
@@ -3,16 +3,19 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ptp.ptplatform.entity.HelpComment;
import com.ptp.ptplatform.entity.HelpPost;
-import com.ptp.ptplatform.entity.SeedPost;
import com.ptp.ptplatform.service.HelpCommentService;
import com.ptp.ptplatform.service.HelpPostService;
import com.ptp.ptplatform.utils.Result;
import lombok.AllArgsConstructor;
-import lombok.Data;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
@RestController
@RequestMapping("/help/posts")
@@ -28,56 +31,91 @@
return Result.ok().data("post", post);
}
-
-
// 列表分页
@GetMapping
public Result listPosts(@RequestParam(defaultValue = "1") int page,
- @RequestParam(defaultValue = "10") int size) {
+ @RequestParam(defaultValue = "5") int size) {
IPage<HelpPost> ipage = postService.page(
new Page<>(page, size),
new QueryWrapper<HelpPost>().orderByDesc("create_time")
);
return Result.ok()
.data("records", ipage.getRecords())
- .data("total", ipage.getTotal());
+ .data("total", ipage.getTotal());
}
- // 帖子详情 + 评论
- @GetMapping("/{postId}")
- public Result getPost(@PathVariable int postId) {
- HelpPost post = postService.getById(postId);
- List<HelpComment> comments = commentService.list(
+ @GetMapping("/{Id}")
+ public Result getPost(@PathVariable int Id) {
+ HelpPost post = postService.getById(Id);
+ if (post == null) {
+ return Result.error(404).setMessage("帖子不存在");
+ }
+
+ // 获取所有评论(按创建时间排序)
+ List<HelpComment> allComments = commentService.list(
new QueryWrapper<HelpComment>()
- .eq("post_id", postId)
+ .eq("post_id", Id)
.orderByAsc("create_time")
);
+
+ // 构建评论树形结构
+ List<HelpComment> rootComments = new ArrayList<>();
+ Map<Integer, HelpComment> commentMap = new HashMap<>();
+
+ // 第一遍:初始化所有评论到map中
+ for (HelpComment comment : allComments) {
+ comment.setReplies(new ArrayList<>()); // 初始化replies列表
+ commentMap.put(comment.getId(), comment);
+ }
+
+ // 第二遍:构建父子关系
+ for (HelpComment comment : allComments) {
+ if (comment.getParentId() == 0) {
+ rootComments.add(comment);
+ } else {
+ HelpComment parent = commentMap.get(comment.getParentId());
+ if (parent != null) {
+ parent.getReplies().add(comment);
+ }
+ }
+ }
+
return Result.ok()
- .data("post", post)
- .data("comments", comments);
+ .data("post", post)
+ .data("comments", rootComments);
}
// 点赞帖子
- @PostMapping("/{postId}/like")
- public Result likePost(@PathVariable int postId) {
- postService.incrementLike(postId);
+ @PostMapping("/{Id}/like")
+ public Result likePost(@PathVariable int Id) {
+ postService.incrementLike(Id);
return Result.ok();
}
- // 发布评论
- @PostMapping("/{postId}/comments")
- public Result comment(@PathVariable int postId,
- @RequestBody HelpComment comment) {
- comment.setPostId(postId);
- commentService.save(comment);
- postService.incrementReplyCount(postId);
- return Result.ok()
- .data("commentId", comment.getId());
- }
- @Data
- @AllArgsConstructor
- static class HelpDetailResponse {
- private HelpPost post;
- private List<HelpComment> comments;
+ @PostMapping("/{Id}/comments")
+ public Result comment(@PathVariable int Id,
+ @RequestBody HelpComment comment) {
+ // 设置评论信息
+ comment.setPostId(Id);
+ comment.setCreateTime(LocalDateTime.now());
+ comment.setLikeCount(0); // 初始化点赞数
+ comment.setParentId(0); // 默认父评论ID
+ comment.setReplyTo(null); // 主评论 replyTo=null
+
+ // 保存评论
+ boolean saved = commentService.save(comment);
+ if (!saved) {
+ return Result.error(404).setMessage("评论保存失败");
+ }
+
+ // 更新回复数
+ postService.incrementReplyCount(Id);
+
+ // 获取更新后的完整评论(包含数据库生成的ID和时间)
+ HelpComment newComment = commentService.getById(comment.getId());
+
+ return Result.ok()
+ .data("comment", newComment) // 返回完整评论数据
+ .data("newReplyCount", postService.getById(Id).getReplyCount());
}
}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/SeedCommentController.java b/src/main/java/com/ptp/ptplatform/controller/SeedCommentController.java
deleted file mode 100644
index 635080f..0000000
--- a/src/main/java/com/ptp/ptplatform/controller/SeedCommentController.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.ptp.ptplatform.controller;
-
-import com.ptp.ptplatform.service.SeedCommentService;
-import com.ptp.ptplatform.utils.Result;
-import lombok.AllArgsConstructor;
-import org.springframework.web.bind.annotation.*;
-
-@RestController
-@RequestMapping("/seed/comments")
-@AllArgsConstructor
-public class SeedCommentController {
- private final SeedCommentService commentService;
-
- @PostMapping("/{commentId}/like")
- public Result like(@PathVariable int commentId) {
- commentService.incrementLike(commentId);
- return Result.ok();
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/SeedPostController.java b/src/main/java/com/ptp/ptplatform/controller/SeedPostController.java
deleted file mode 100644
index f481b9b..0000000
--- a/src/main/java/com/ptp/ptplatform/controller/SeedPostController.java
+++ /dev/null
@@ -1,82 +0,0 @@
-package com.ptp.ptplatform.controller;
-
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.ptp.ptplatform.entity.HelpPost;
-import com.ptp.ptplatform.entity.SeedComment;
-import com.ptp.ptplatform.entity.SeedPost;
-import com.ptp.ptplatform.service.SeedCommentService;
-import com.ptp.ptplatform.service.SeedPostService;
-import com.ptp.ptplatform.utils.Result;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import org.springframework.web.bind.annotation.*;
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import java.util.List;
-
-@RestController
-@RequestMapping("/seed/posts")
-@AllArgsConstructor
-public class SeedPostController {
- private final SeedPostService postService;
- private final SeedCommentService commentService;
-
- // 创建帖子
- @PostMapping
- public Result createPost(@RequestBody SeedPost post) {
- postService.save(post);
- return Result.ok().data("post", post);
- }
-
-
- // 列表分页
- @GetMapping
- public Result listPosts(@RequestParam(defaultValue = "1") int page,
- @RequestParam(defaultValue = "10") int size) {
- IPage<SeedPost> ipage = postService.page(
- new Page<>(page, size),
- new QueryWrapper<SeedPost>().orderByDesc("create_time")
- );
- return Result.ok()
- .data("records", ipage.getRecords())
- .data("total", ipage.getTotal());
- }
-
- // 帖子详情 + 评论
- @GetMapping("/{postId}")
- public Result getPost(@PathVariable int postId) {
- SeedPost post = postService.getById(postId);
- List<SeedComment> comments = commentService.list(
- new QueryWrapper<SeedComment>()
- .eq("post_id", postId)
- .orderByAsc("create_time")
- );
- return Result.ok()
- .data("post", post)
- .data("comments", comments);
- }
-
- // 点赞帖子
- @PostMapping("/{postId}/like")
- public Result likePost(@PathVariable int postId) {
- postService.incrementLike(postId);
- return Result.ok();
- }
- // 发布评论
- @PostMapping("/{postId}/comments")
- public Result comment(@PathVariable int postId,
- @RequestBody SeedComment comment) {
- comment.setPostId(postId);
- commentService.save(comment);
- postService.incrementReplyCount(postId);
- return Result.ok()
- .data("commentId", comment.getId());
- }
-
- @Data
- @AllArgsConstructor
- static class SeedDetailResponse {
- private SeedPost post;
- private List<SeedComment> comments;
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/TorrentCommentController.java b/src/main/java/com/ptp/ptplatform/controller/TorrentCommentController.java
new file mode 100644
index 0000000..1140ff7
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/controller/TorrentCommentController.java
@@ -0,0 +1,55 @@
+package com.ptp.ptplatform.controller;
+
+import com.ptp.ptplatform.entity.TorrentComment;
+import com.ptp.ptplatform.service.TorrentCommentService;
+import com.ptp.ptplatform.utils.Result;
+import lombok.AllArgsConstructor;
+import org.springframework.web.bind.annotation.*;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+@RestController
+@RequestMapping("/torrent/comments")
+@AllArgsConstructor
+public class TorrentCommentController {
+ private final TorrentCommentService commentService;
+
+ @PostMapping("/{commentId}/like")
+ public Result like(@PathVariable int commentId) {
+ commentService.incrementLike(commentId);
+ return Result.ok();
+ }
+
+ // 获取评论的回复
+ @GetMapping("/{commentId}/replies")
+ public Result getReplies(@PathVariable int commentId) {
+ List<TorrentComment> replies = commentService.getReplies(commentId);
+ return Result.ok().data("replies", replies);
+ }
+
+ @PostMapping("/{commentId}/replies")
+ public Result addReply(@PathVariable int commentId,
+ @RequestBody TorrentComment reply) {
+ TorrentComment parentComment = commentService.getById(commentId);
+ if (parentComment == null) {
+ return Result.error(404).setMessage("被回复的评论不存在");
+ }
+
+ // 设置完整字段
+ reply.setPostId(parentComment.getPostId());
+ reply.setParentId(commentId);
+ reply.setAuthorId(reply.getAuthorId());
+ reply.setReplyTo(parentComment.getAuthorId());
+ reply.setCreateTime(LocalDateTime.now()); // 设置创建时间
+ reply.setLikeCount(0); // 初始化点赞数
+
+ commentService.save(reply);
+
+ // 返回完整的评论数据
+ return Result.ok()
+ .data("reply", reply)
+ .data("parentCommentId", commentId); // 同时返回父评论ID
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/TorrentController.java b/src/main/java/com/ptp/ptplatform/controller/TorrentController.java
new file mode 100644
index 0000000..3c71ba1
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/controller/TorrentController.java
@@ -0,0 +1,121 @@
+package com.ptp.ptplatform.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ptp.ptplatform.entity.TorrentComment;
+import com.ptp.ptplatform.entity.Torrent;
+import com.ptp.ptplatform.service.TorrentCommentService;
+import com.ptp.ptplatform.service.TorrentService;
+import com.ptp.ptplatform.utils.Result;
+import lombok.AllArgsConstructor;
+import org.springframework.web.bind.annotation.*;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/torrent")
+@AllArgsConstructor
+public class TorrentController {
+ private final TorrentService postService;
+ private final TorrentCommentService commentService;
+
+ // 创建帖子
+ @PostMapping
+ public Result createTorrent(@RequestBody Torrent torrent) {
+ postService.save(torrent);
+ return Result.ok().data("post", torrent);
+ }
+
+ // 列表分页
+ @GetMapping
+ public Result listPosts(@RequestParam(defaultValue = "1") int page,
+ @RequestParam(defaultValue = "5") int size) {
+ IPage<Torrent> ipage = postService.page(
+ new Page<>(page, size),
+ new QueryWrapper<Torrent>().orderByDesc("create_time")
+ );
+ return Result.ok()
+ .data("records", ipage.getRecords())
+ .data("total", ipage.getTotal());
+ }
+
+ @GetMapping("/{Id}")
+ public Result getPost(@PathVariable int Id) {
+ Torrent torrent = postService.getById(Id);
+ if (torrent == null) {
+ return Result.error(404).setMessage("种子不存在"); // 明确设置404状态码
+ }
+
+ // 获取所有评论(按创建时间排序)
+ List<TorrentComment> allComments = commentService.list(
+ new QueryWrapper<TorrentComment>()
+ .eq("post_id", Id)
+ .orderByAsc("create_time")
+ );
+
+ // 构建评论树形结构
+ List<TorrentComment> rootComments = new ArrayList<>();
+ Map<Integer, TorrentComment> commentMap = new HashMap<>();
+
+ // 第一遍:初始化所有评论到map中
+ for (TorrentComment comment : allComments) {
+ comment.setReplies(new ArrayList<>()); // 初始化replies列表
+ commentMap.put(comment.getId(), comment);
+ }
+
+ // 第二遍:构建父子关系
+ for (TorrentComment comment : allComments) {
+ if (comment.getParentId() == 0) {
+ rootComments.add(comment);
+ } else {
+ TorrentComment parent = commentMap.get(comment.getParentId());
+ if (parent != null) {
+ parent.getReplies().add(comment);
+ }
+ }
+ }
+
+ return Result.ok()
+ .data("torrent", torrent)
+ .data("comments", rootComments);
+ }
+
+ // 点赞帖子
+ @PostMapping("/{Id}/like")
+ public Result likePost(@PathVariable int Id) {
+ postService.incrementLike(Id);
+ return Result.ok();
+ }
+
+ @PostMapping("/{Id}/comments")
+ public Result comment(@PathVariable int Id,
+ @RequestBody TorrentComment comment) {
+ // 设置评论信息
+ comment.setPostId(Id);
+ comment.setCreateTime(LocalDateTime.now());
+ comment.setLikeCount(0); // 初始化点赞数
+ comment.setParentId(0); // 默认父评论ID
+ comment.setReplyTo(null); // 主评论 replyTo=null
+
+ // 保存评论
+ boolean saved = commentService.save(comment);
+ if (!saved) {
+ return Result.error(404).setMessage("评论保存失败");
+ }
+
+ // 更新回复数
+ postService.incrementReplyCount(Id);
+
+ // 获取更新后的完整评论(包含数据库生成的ID和时间)
+ TorrentComment newComment = commentService.getById(comment.getId());
+
+ return Result.ok()
+ .data("comment", newComment) // 返回完整评论数据
+ .data("newReplyCount", postService.getById(Id).getReplyCount());
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/controller/UserController.java b/src/main/java/com/ptp/ptplatform/controller/UserController.java
index 0b1dcb2..9265ce7 100644
--- a/src/main/java/com/ptp/ptplatform/controller/UserController.java
+++ b/src/main/java/com/ptp/ptplatform/controller/UserController.java
@@ -42,10 +42,10 @@
String token = JwtUtils.generateToken(user.getUsername());
return Result.ok().data("token", token); // 返回令牌给前端
} else {
- return Result.error().setMessage("密码错误");
+ return Result.error(404).setMessage("密码错误");
}
} else {
- return Result.error().setMessage("用户不存在");
+ return Result.error(404).setMessage("用户不存在");
}
}
@@ -67,15 +67,15 @@
return Result.ok().setMessage("新建用户成功");
} else {
- return Result.error().setMessage("邀请码已经被使用,注册失败");
+ return Result.error(404).setMessage("邀请码已经被使用,注册失败");
}
} else {
- return Result.error().setMessage("邀请码不存在,注册失败");
+ return Result.error(404).setMessage("邀请码不存在,注册失败");
}
} else {
- return Result.error().setMessage("用户名已存在,注册失败");
+ return Result.error(404).setMessage("用户名已存在,注册失败");
}
diff --git a/src/main/java/com/ptp/ptplatform/entity/HelpComment.java b/src/main/java/com/ptp/ptplatform/entity/HelpComment.java
index 405e91d..5821868 100644
--- a/src/main/java/com/ptp/ptplatform/entity/HelpComment.java
+++ b/src/main/java/com/ptp/ptplatform/entity/HelpComment.java
@@ -1,10 +1,13 @@
package com.ptp.ptplatform.entity;
import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
@Data
@TableName("help_comments")
@@ -12,8 +15,22 @@
@TableId(value = "id", type = IdType.AUTO)
private Integer id; // 使用包装类型以支持 null
private Integer postId;
- private Integer authorId;
+ private String authorId;
private String content;
private Integer likeCount;
private LocalDateTime createTime;
+ private Integer parentId; // 新增字段,用于回复功能,0表示主评论,非0表示回复某条评论
+ private String replyTo;
+
+ @TableField(exist = false)
+ private List<HelpComment> replies = new ArrayList<>(); // 必须初始化
+
+ // 确保有getter和setter
+ public List<HelpComment> getReplies() {
+ return replies;
+ }
+
+ public void setReplies(List<HelpComment> replies) {
+ this.replies = replies;
+ }
}
diff --git a/src/main/java/com/ptp/ptplatform/entity/HelpPost.java b/src/main/java/com/ptp/ptplatform/entity/HelpPost.java
index b77fe0b..be0bd02 100644
--- a/src/main/java/com/ptp/ptplatform/entity/HelpPost.java
+++ b/src/main/java/com/ptp/ptplatform/entity/HelpPost.java
@@ -11,7 +11,7 @@
public class HelpPost {
@TableId(value = "id", type = IdType.AUTO)
private Integer id; // 使用包装类型以支持 null
- private Integer authorId;
+ private String authorId;
private String title;
private String content;
private Integer likeCount;
diff --git a/src/main/java/com/ptp/ptplatform/entity/SeedComment.java b/src/main/java/com/ptp/ptplatform/entity/SeedComment.java
deleted file mode 100644
index 48121b5..0000000
--- a/src/main/java/com/ptp/ptplatform/entity/SeedComment.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.ptp.ptplatform.entity;
-
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import lombok.Data;
-import java.time.LocalDateTime;
-
-@Data
-@TableName("seed_comments")
-public class SeedComment {
- @TableId(value = "id", type = IdType.AUTO)
- private Integer id; // 使用包装类型以支持 null
- private Integer postId;
- private Integer authorId;
- private String content;
- private Integer likeCount;
- private LocalDateTime createTime;
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/SeedPost.java b/src/main/java/com/ptp/ptplatform/entity/SeedPost.java
deleted file mode 100644
index b8f74ef..0000000
--- a/src/main/java/com/ptp/ptplatform/entity/SeedPost.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.ptp.ptplatform.entity;
-
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import lombok.Data;
-import java.time.LocalDateTime;
-
-@Data
-@TableName("seed_posts")
-public class SeedPost {
- @TableId(value = "id", type = IdType.AUTO)
- private Integer id; // 使用包装类型以支持 null
- private Integer authorId;
- private String title;
- private String content;
- private Integer likeCount;
- private Integer replyCount;
- private LocalDateTime createTime;
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/Torrent.java b/src/main/java/com/ptp/ptplatform/entity/Torrent.java
new file mode 100644
index 0000000..2c6c6d3
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/entity/Torrent.java
@@ -0,0 +1,39 @@
+package com.ptp.ptplatform.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+import java.time.LocalDateTime;
+
+@Data
+@TableName("torrent")
+public class Torrent {
+ @TableId(value = "id", type = IdType.AUTO)
+ private Integer id;
+
+ @TableField("torrentName") // 明确指定数据库列名
+ private String torrentName;
+
+ private String description;
+ private String category;
+ private String region;
+ private String resolution;
+ private String subtitle;
+ private Long size;
+ private String hash;
+
+ private LocalDateTime createTime;
+
+ private String username;
+
+ @TableField("filePath") // 明确指定数据库列名
+ private String filePath;
+
+ private Integer likeCount;
+ private Integer replyCount;
+
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/TorrentComment.java b/src/main/java/com/ptp/ptplatform/entity/TorrentComment.java
new file mode 100644
index 0000000..d2b4694
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/entity/TorrentComment.java
@@ -0,0 +1,36 @@
+package com.ptp.ptplatform.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
+
+@Data
+@TableName("torrent_comments")
+public class TorrentComment {
+ @TableId(value = "id", type = IdType.AUTO)
+ private Integer id;
+ private Integer postId;
+ private String authorId;
+ private String content;
+ private Integer likeCount;
+ private LocalDateTime createTime;
+ private Integer parentId;
+ private String replyTo;
+
+ @TableField(exist = false)
+ private List<TorrentComment> replies = new ArrayList<>(); // 必须初始化
+
+ // 确保有getter和setter
+ public List<TorrentComment> getReplies() {
+ return replies;
+ }
+
+ public void setReplies(List<TorrentComment> replies) {
+ this.replies = replies;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/entity/USER.java b/src/main/java/com/ptp/ptplatform/entity/USER.java
index 84e5c33..a33847c 100644
--- a/src/main/java/com/ptp/ptplatform/entity/USER.java
+++ b/src/main/java/com/ptp/ptplatform/entity/USER.java
@@ -2,13 +2,13 @@
// @GeneratedValue(strategy = GenerationType.IDENTITY) 对于一些需要自动生成的主键id进行注解
package com.ptp.ptplatform.entity;
-
import jakarta.persistence.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
+
@Table(name = "user")
public class USER {
diff --git a/src/main/java/com/ptp/ptplatform/mapper/SeedCommentMapper.java b/src/main/java/com/ptp/ptplatform/mapper/SeedCommentMapper.java
deleted file mode 100644
index d72619b..0000000
--- a/src/main/java/com/ptp/ptplatform/mapper/SeedCommentMapper.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.ptp.ptplatform.mapper;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.ptp.ptplatform.entity.SeedComment;
-import org.apache.ibatis.annotations.Mapper;
-
-@Mapper
-public interface SeedCommentMapper extends BaseMapper<SeedComment> {}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/mapper/SeedPostMapper.java b/src/main/java/com/ptp/ptplatform/mapper/SeedPostMapper.java
deleted file mode 100644
index 134212d..0000000
--- a/src/main/java/com/ptp/ptplatform/mapper/SeedPostMapper.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.ptp.ptplatform.mapper;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.ptp.ptplatform.entity.SeedPost;
-import org.apache.ibatis.annotations.Mapper;
-
-@Mapper
-public interface SeedPostMapper extends BaseMapper<SeedPost> {}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/mapper/TorrentCommentMapper.java b/src/main/java/com/ptp/ptplatform/mapper/TorrentCommentMapper.java
new file mode 100644
index 0000000..b330ce6
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/mapper/TorrentCommentMapper.java
@@ -0,0 +1,8 @@
+package com.ptp.ptplatform.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ptp.ptplatform.entity.TorrentComment;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface TorrentCommentMapper extends BaseMapper<TorrentComment> {}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/mapper/TorrentMapper.java b/src/main/java/com/ptp/ptplatform/mapper/TorrentMapper.java
new file mode 100644
index 0000000..ff612b3
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/mapper/TorrentMapper.java
@@ -0,0 +1,8 @@
+package com.ptp.ptplatform.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ptp.ptplatform.entity.Torrent;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface TorrentMapper extends BaseMapper<Torrent> {}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/HelpCommentService.java b/src/main/java/com/ptp/ptplatform/service/HelpCommentService.java
index dcab060..14622ab 100644
--- a/src/main/java/com/ptp/ptplatform/service/HelpCommentService.java
+++ b/src/main/java/com/ptp/ptplatform/service/HelpCommentService.java
@@ -3,6 +3,9 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.ptp.ptplatform.entity.HelpComment;
+import java.util.List;
+
public interface HelpCommentService extends IService<HelpComment> {
void incrementLike(int commentId);
+ List<HelpComment> getReplies(int parentId); // 新增方法
}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/SeedCommentService.java b/src/main/java/com/ptp/ptplatform/service/SeedCommentService.java
deleted file mode 100644
index dec7f16..0000000
--- a/src/main/java/com/ptp/ptplatform/service/SeedCommentService.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.ptp.ptplatform.service;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.ptp.ptplatform.entity.SeedComment;
-
-public interface SeedCommentService extends IService<SeedComment> {
- void incrementLike(int commentId);
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/SeedPostService.java b/src/main/java/com/ptp/ptplatform/service/SeedPostService.java
deleted file mode 100644
index 40605a8..0000000
--- a/src/main/java/com/ptp/ptplatform/service/SeedPostService.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.ptp.ptplatform.service;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.ptp.ptplatform.entity.SeedPost;
-
-public interface SeedPostService extends IService<SeedPost> {
- void incrementLike(int postId);
- void incrementReplyCount(int postId);
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/TorrentCommentService.java b/src/main/java/com/ptp/ptplatform/service/TorrentCommentService.java
new file mode 100644
index 0000000..3b60257
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/service/TorrentCommentService.java
@@ -0,0 +1,11 @@
+package com.ptp.ptplatform.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ptp.ptplatform.entity.TorrentComment;
+
+import java.util.List;
+
+public interface TorrentCommentService extends IService<TorrentComment> {
+ void incrementLike(int commentId);
+ List<TorrentComment> getReplies(int parentId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/TorrentService.java b/src/main/java/com/ptp/ptplatform/service/TorrentService.java
new file mode 100644
index 0000000..f3d0222
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/service/TorrentService.java
@@ -0,0 +1,11 @@
+package com.ptp.ptplatform.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ptp.ptplatform.entity.Torrent;
+
+public interface TorrentService extends IService<Torrent> {
+ Torrent getTorrentById(Integer id); // Add this method
+ void incrementLike(int postId);
+ void incrementReplyCount(int postId);
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/impl/HelpCommentServiceImpl.java b/src/main/java/com/ptp/ptplatform/service/impl/HelpCommentServiceImpl.java
index dbaed2a..4f47e78 100644
--- a/src/main/java/com/ptp/ptplatform/service/impl/HelpCommentServiceImpl.java
+++ b/src/main/java/com/ptp/ptplatform/service/impl/HelpCommentServiceImpl.java
@@ -1,14 +1,19 @@
package com.ptp.ptplatform.service.impl;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ptp.ptplatform.entity.HelpComment;
import com.ptp.ptplatform.mapper.HelpCommentMapper;
import com.ptp.ptplatform.service.HelpCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import java.util.List;
@Service
-public class HelpCommentServiceImpl extends ServiceImpl<HelpCommentMapper, HelpComment> implements HelpCommentService {
+public class HelpCommentServiceImpl
+ extends ServiceImpl<HelpCommentMapper, HelpComment>
+ implements HelpCommentService {
+
@Override
@Transactional
public void incrementLike(int commentId) {
@@ -18,4 +23,14 @@
.setSql("like_count = like_count + 1")
);
}
+
+ @Override
+ @Transactional(readOnly = true)
+ public List<HelpComment> getReplies(int parentId) {
+ return this.baseMapper.selectList(
+ new QueryWrapper<HelpComment>()
+ .eq("parent_id", parentId)
+ .orderByAsc("create_time")
+ );
+ }
}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/impl/SeedCommentServiceImpl.java b/src/main/java/com/ptp/ptplatform/service/impl/SeedCommentServiceImpl.java
deleted file mode 100644
index 537194a..0000000
--- a/src/main/java/com/ptp/ptplatform/service/impl/SeedCommentServiceImpl.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.ptp.ptplatform.service.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ptp.ptplatform.entity.SeedComment;
-import com.ptp.ptplatform.mapper.SeedCommentMapper;
-import com.ptp.ptplatform.service.SeedCommentService;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
-@Service
-public class SeedCommentServiceImpl extends ServiceImpl<SeedCommentMapper, SeedComment> implements SeedCommentService {
- @Override
- @Transactional
- public void incrementLike(int commentId) {
- this.update(null,
- new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<SeedComment>()
- .eq("id", commentId)
- .setSql("like_count = like_count + 1")
- );
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/impl/TorrentCommentServiceImpl.java b/src/main/java/com/ptp/ptplatform/service/impl/TorrentCommentServiceImpl.java
new file mode 100644
index 0000000..540df71
--- /dev/null
+++ b/src/main/java/com/ptp/ptplatform/service/impl/TorrentCommentServiceImpl.java
@@ -0,0 +1,34 @@
+package com.ptp.ptplatform.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ptp.ptplatform.entity.TorrentComment;
+import com.ptp.ptplatform.mapper.TorrentCommentMapper;
+import com.ptp.ptplatform.service.TorrentCommentService;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+
+@Service
+public class TorrentCommentServiceImpl extends ServiceImpl<TorrentCommentMapper, TorrentComment> implements TorrentCommentService {
+ @Override
+ @Transactional
+ public void incrementLike(int commentId) {
+ this.update(null,
+ new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<TorrentComment>()
+ .eq("id", commentId)
+ .setSql("like_count = like_count + 1")
+ );
+ }
+
+ @Override
+ @Transactional(readOnly = true)
+ public List<TorrentComment> getReplies(int parentId) {
+ return this.baseMapper.selectList(
+ new QueryWrapper<TorrentComment>()
+ .eq("parent_id", parentId)
+ .orderByAsc("create_time")
+ );
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/service/impl/SeedPostServiceImpl.java b/src/main/java/com/ptp/ptplatform/service/impl/TorrentServiceImpl.java
similarity index 64%
rename from src/main/java/com/ptp/ptplatform/service/impl/SeedPostServiceImpl.java
rename to src/main/java/com/ptp/ptplatform/service/impl/TorrentServiceImpl.java
index 05cf06c..a6eba54 100644
--- a/src/main/java/com/ptp/ptplatform/service/impl/SeedPostServiceImpl.java
+++ b/src/main/java/com/ptp/ptplatform/service/impl/TorrentServiceImpl.java
@@ -1,19 +1,24 @@
package com.ptp.ptplatform.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ptp.ptplatform.entity.SeedPost;
-import com.ptp.ptplatform.mapper.SeedPostMapper;
-import com.ptp.ptplatform.service.SeedPostService;
+import com.ptp.ptplatform.entity.Torrent;
+import com.ptp.ptplatform.mapper.TorrentMapper;
+import com.ptp.ptplatform.service.TorrentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
-public class SeedPostServiceImpl extends ServiceImpl<SeedPostMapper, SeedPost> implements SeedPostService {
+public class TorrentServiceImpl extends ServiceImpl<TorrentMapper, Torrent> implements TorrentService {
+
+ @Override
+ public Torrent getTorrentById(Integer id) {
+ return this.getById(id); // 直接调用 MyBatis-Plus 提供的 getById 方法
+ }
@Override
@Transactional
public void incrementLike(int postId) {
this.update(null,
- new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<SeedPost>()
+ new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Torrent>()
.eq("id", postId)
.setSql("like_count = like_count + 1")
);
@@ -23,9 +28,10 @@
@Transactional
public void incrementReplyCount(int postId) {
this.update(null,
- new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<SeedPost>()
+ new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<Torrent>()
.eq("id", postId)
.setSql("reply_count = reply_count + 1")
);
}
+
}
\ No newline at end of file
diff --git a/src/main/java/com/ptp/ptplatform/utils/Result.java b/src/main/java/com/ptp/ptplatform/utils/Result.java
index 04a3290..0bda7eb 100644
--- a/src/main/java/com/ptp/ptplatform/utils/Result.java
+++ b/src/main/java/com/ptp/ptplatform/utils/Result.java
@@ -44,7 +44,7 @@
return result;
}
- public static Result error() {
+ public static Result error(int i) {
Result result = new Result();
result.setSuccess(false);
result.setCode(resultCode.ERROR);
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 588221c..730af5c 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -15,10 +15,9 @@
# database setting
spring.datasourse.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
-spring.datasource.url=jdbc:mysql://localhost:3306/ptpdata?useUnicode=true&characterEncoding=utf8
-spring.datasource.username=root
-#spring.datasource.password=yumu1412
-spring.datasource.password=root
+spring.datasource.url=jdbc:mysql://202.205.102.121:3306/G2ptpdatadevelop?useUnicode=true&characterEncoding=utf8
+spring.datasource.username=team2
+spring.datasource.password=123456
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# Hibernate properties
diff --git a/src/test/java/com/ptp/ptplatform/controller/HelpCommentControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/HelpCommentControllerTest.java
new file mode 100644
index 0000000..2450c6e
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/HelpCommentControllerTest.java
@@ -0,0 +1,135 @@
+package com.ptp.ptplatform.controller;
+
+import com.ptp.ptplatform.entity.HelpComment;
+import com.ptp.ptplatform.service.HelpCommentService;
+import com.ptp.ptplatform.utils.Result;
+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.http.ResponseEntity;
+
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class HelpCommentControllerTest {
+
+ @Mock
+ private HelpCommentService commentService;
+
+ @InjectMocks
+ private HelpCommentController commentController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void likeComment_ShouldReturnSuccess() {
+ // Arrange
+ int commentId = 1;
+ doNothing().when(commentService).incrementLike(commentId);
+
+ // Act
+ Result result = commentController.like(commentId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals("成功", result.getMessage());
+ verify(commentService, times(1)).incrementLike(commentId);
+ }
+
+ @Test
+ void getReplies_ShouldReturnReplies() {
+ // Arrange
+ int commentId = 1;
+
+ HelpComment reply1 = new HelpComment();
+ reply1.setId(2);
+ reply1.setPostId(1);
+ reply1.setParentId(commentId);
+ reply1.setAuthorId("user2");
+ reply1.setContent("Reply 1");
+ reply1.setCreateTime(LocalDateTime.now());
+ reply1.setLikeCount(0);
+ reply1.setReplyTo("user1");
+
+ HelpComment reply2 = new HelpComment();
+ reply2.setId(3);
+ reply2.setPostId(1);
+ reply2.setParentId(commentId);
+ reply2.setAuthorId("user3");
+ reply2.setContent("Reply 2");
+ reply2.setCreateTime(LocalDateTime.now());
+ reply2.setLikeCount(0);
+ reply2.setReplyTo("user1");
+
+ List<HelpComment> mockReplies = Arrays.asList(reply1, reply2);
+ when(commentService.getReplies(commentId)).thenReturn(mockReplies);
+
+ // Act
+ Result result = commentController.getReplies(commentId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals(mockReplies, result.getData().get("replies"));
+ verify(commentService, times(1)).getReplies(commentId);
+ }
+
+ @Test
+ void addReply_ShouldReturnSuccess_WhenParentCommentExists() {
+ // Arrange
+ int commentId = 1;
+
+ HelpComment parentComment = new HelpComment();
+ parentComment.setId(commentId);
+ parentComment.setPostId(1);
+ parentComment.setParentId(0);
+ parentComment.setAuthorId("user1");
+ parentComment.setContent("Parent comment");
+ parentComment.setCreateTime(LocalDateTime.now());
+ parentComment.setLikeCount(5);
+ parentComment.setReplyTo(null);
+
+ HelpComment reply = new HelpComment();
+ reply.setAuthorId("user2");
+ reply.setContent("Test reply");
+
+ when(commentService.getById(commentId)).thenReturn(parentComment);
+ when(commentService.save(any(HelpComment.class))).thenReturn(true);
+
+ // Act
+ Result result = commentController.addReply(commentId, reply);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertNotNull(result.getData().get("reply"));
+ assertEquals(commentId, result.getData().get("parentCommentId"));
+ verify(commentService, times(1)).save(any(HelpComment.class));
+ }
+
+ @Test
+ void addReply_ShouldReturnError_WhenParentCommentNotExists() {
+ // Arrange
+ int commentId = 1;
+ HelpComment reply = new HelpComment();
+ reply.setAuthorId("user2");
+ reply.setContent("Test reply");
+
+ when(commentService.getById(commentId)).thenReturn(null);
+
+ // Act
+ Result result = commentController.addReply(commentId, reply);
+
+ // Assert
+ assertEquals(500, result.getCode());
+ assertEquals("被回复的评论不存在", result.getMessage());
+ verify(commentService, never()).save(any(HelpComment.class));
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
index 61d6aff..877289b 100644
--- a/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
+++ b/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
@@ -1,104 +1,179 @@
-// src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
-package com.ptp.ptplatform.controller;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.ptp.ptplatform.entity.HelpComment;
-import com.ptp.ptplatform.entity.HelpPost;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.http.MediaType;
-import org.springframework.test.web.servlet.MockMvc;
-
-import java.util.List;
-
-import static org.hamcrest.Matchers.*;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
-
-@SpringBootTest
-@AutoConfigureMockMvc
-public class HelpPostControllerTest {
-
- @Autowired MockMvc mockMvc;
- @Autowired ObjectMapper objectMapper;
-
- private int postId;
-
- @BeforeEach
- void setup() throws Exception {
- // 1. 创建一个帖子
- HelpPost post = new HelpPost();
- post.setAuthorId(1001);
- post.setTitle("测试帮助帖");
- post.setContent("帮助区内容示例");
- String body = objectMapper.writeValueAsString(post);
-
- // 2. 调接口,拿到完整的 JSON
- String json = mockMvc.perform(post("/help/posts")
- .contentType(MediaType.APPLICATION_JSON)
- .content(body))
- .andExpect(status().isOk())
- .andReturn().getResponse().getContentAsString();
-
- // 3. 解析 JSON,取到 data.post.id
- JsonNode node = objectMapper.readTree(json);
- postId = node.path("data").path("post").path("id").asInt();
- }
-
- @Test
- void testHelpPostLifecycle() throws Exception {
- // —— 列表分页 ——
- mockMvc.perform(get("/help/posts")
- .param("page", "1")
- .param("size", "5"))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.records[0].id", is(postId)));
-
- // —— 详情(此时无评论) ——
- mockMvc.perform(get("/help/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.post.id", is(postId)))
- .andExpect(jsonPath("$.data.comments", hasSize(0)));
-
- // —— 点赞帖子 ——
- mockMvc.perform(post("/help/posts/{id}/like", postId))
- .andExpect(status().isOk());
-
- mockMvc.perform(get("/help/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.post.likeCount", is(1)));
-
- // —— 发布评论 ——
- HelpComment comment = new HelpComment();
- comment.setAuthorId(2002);
- comment.setContent("测试帮助评论");
- String cbody = objectMapper.writeValueAsString(comment);
-
- String cres = mockMvc.perform(post("/help/posts/{id}/comments", postId)
- .contentType(MediaType.APPLICATION_JSON)
- .content(cbody))
- .andExpect(status().isOk())
- .andReturn().getResponse().getContentAsString();
-
- // 解析 commentId
- JsonNode cnode = objectMapper.readTree(cres);
- int commentId = cnode.path("data").path("commentId").asInt();
-
- // 列详情确认评论出现
- mockMvc.perform(get("/help/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.comments[0].id", is(commentId)));
-
- // —— 评论点赞 ——
- mockMvc.perform(post("/help/comments/{id}/like", commentId))
- .andExpect(status().isOk());
-
- mockMvc.perform(get("/help/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.comments[0].likeCount", is(1)));
- }
-}
+package com.ptp.ptplatform.controller;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ptp.ptplatform.entity.HelpComment;
+import com.ptp.ptplatform.entity.HelpPost;
+import com.ptp.ptplatform.service.HelpCommentService;
+import com.ptp.ptplatform.service.HelpPostService;
+import com.ptp.ptplatform.utils.Result;
+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 java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+
+class HelpPostControllerTest {
+
+ @Mock
+ private HelpPostService postService;
+
+ @Mock
+ private HelpCommentService commentService;
+
+ @InjectMocks
+ private HelpPostController postController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void getPost_ShouldReturnPostWithComments_WhenPostExists() {
+ // Arrange
+ int postId = 1;
+
+ HelpPost post = new HelpPost();
+ post.setId(postId);
+ post.setTitle("Test Post");
+ post.setContent("Test Content");
+ post.setAuthorId("user1");
+ post.setCreateTime(LocalDateTime.now());
+
+ HelpComment comment1 = new HelpComment();
+ comment1.setId(1);
+ comment1.setPostId(postId);
+ comment1.setParentId(0);
+ comment1.setAuthorId("user2");
+ comment1.setContent("Comment 1");
+ comment1.setCreateTime(LocalDateTime.now());
+
+ HelpComment comment2 = new HelpComment();
+ comment2.setId(2);
+ comment2.setPostId(postId);
+ comment2.setParentId(0);
+ comment2.setAuthorId("user3");
+ comment2.setContent("Comment 2");
+ comment2.setCreateTime(LocalDateTime.now());
+
+ List<HelpComment> allComments = Arrays.asList(comment1, comment2);
+
+ when(postService.getById(postId)).thenReturn(post);
+ // 明确指定使用 Wrapper 参数的 list 方法
+ when(commentService.list(any(Wrapper.class))).thenReturn(allComments);
+
+ // Act
+ Result result = postController.getPost(postId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals(post, result.getData().get("post"));
+ List<?> comments = (List<?>) result.getData().get("comments");
+ assertNotNull(comments);
+ assertEquals(2, comments.size());
+ verify(postService, times(1)).getById(postId);
+ verify(commentService, times(1)).list(any(Wrapper.class));
+ }
+
+ @Test
+ void listPosts_ShouldReturnPaginatedPosts() {
+ // Arrange
+ int page = 1;
+ int size = 5;
+
+ HelpPost post1 = new HelpPost();
+ post1.setId(1);
+ post1.setTitle("Post 1");
+ post1.setContent("Content 1");
+ post1.setAuthorId("user1");
+ post1.setCreateTime(LocalDateTime.now());
+
+ HelpPost post2 = new HelpPost();
+ post2.setId(2);
+ post2.setTitle("Post 2");
+ post2.setContent("Content 2");
+ post2.setAuthorId("user2");
+ post2.setCreateTime(LocalDateTime.now());
+
+ List<HelpPost> posts = Arrays.asList(post1, post2);
+ Page<HelpPost> pageResult = new Page<>(page, size, posts.size());
+ pageResult.setRecords(posts);
+
+ when(postService.page(any(Page.class), any())).thenReturn(pageResult);
+
+ // Act
+ Result result = postController.listPosts(page, size);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals(posts, result.getData().get("records"));
+ assertEquals((long)posts.size(), ((Long)result.getData().get("total")).longValue()); // 修改为比较long值
+ verify(postService, times(1)).page(any(Page.class), any());
+ }
+
+ @Test
+ void getPost_ShouldReturnError_WhenPostNotExists() {
+ // Arrange
+ int postId = 1;
+ when(postService.getById(postId)).thenReturn(null);
+
+ // Act
+ Result result = postController.getPost(postId);
+
+ // Assert
+ assertEquals(500, result.getCode());
+ assertEquals("帖子不存在", result.getMessage());
+ verify(postService, times(1)).getById(postId);
+
+ }
+
+ @Test
+ void likePost_ShouldReturnSuccess() {
+ // Arrange
+ int postId = 1;
+ doNothing().when(postService).incrementLike(postId);
+
+ // Act
+ Result result = postController.likePost(postId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ verify(postService, times(1)).incrementLike(postId);
+ }
+
+ @Test
+ void addComment_ShouldReturnSuccess() {
+ // Arrange
+ int postId = 1;
+
+ HelpComment comment = new HelpComment();
+ comment.setId(1); // 添加ID设置
+ comment.setContent("Test comment");
+ comment.setAuthorId("user2");
+ comment.setCreateTime(LocalDateTime.now());
+
+ when(postService.getById(postId)).thenReturn(new HelpPost());
+ when(commentService.save(any(HelpComment.class))).thenReturn(true);
+ when(commentService.getById(anyInt())).thenReturn(comment);
+ when(postService.getById(anyInt())).thenReturn(new HelpPost()); // 添加这行
+
+ // Act
+ Result result = postController.comment(postId, comment);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertNotNull(result.getData().get("comment"));
+ verify(commentService, times(1)).save(any(HelpComment.class));
+ verify(postService, times(1)).incrementReplyCount(postId);
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java
deleted file mode 100644
index 95c431f..0000000
--- a/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java
+++ /dev/null
@@ -1,105 +0,0 @@
-package com.ptp.ptplatform.controller;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.ptp.ptplatform.entity.SeedComment;
-import com.ptp.ptplatform.entity.SeedPost;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.http.MediaType;
-import org.springframework.test.web.servlet.MockMvc;
-
-import static org.hamcrest.Matchers.*;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
-
-@SpringBootTest
-@AutoConfigureMockMvc
-public class SeedPostControllerTest {
-
- @Autowired
- private MockMvc mockMvc;
-
- @Autowired
- private ObjectMapper objectMapper;
-
- private int postId;
-
- @BeforeEach
- void setup() throws Exception {
- SeedPost post = new SeedPost();
- post.setAuthorId(3003);
- post.setTitle("测试求种帖");
- post.setContent("求种区内容示例");
- String json = objectMapper.writeValueAsString(post);
-
- String result = mockMvc.perform(post("/seed/posts")
- .contentType(MediaType.APPLICATION_JSON)
- .content(json))
- .andExpect(status().isOk())
- .andReturn()
- .getResponse()
- .getContentAsString();
-
- // 从 Result.data.post.id 里取出刚创建的 postId
- JsonNode node = objectMapper.readTree(result);
- postId = node.path("data").path("post").path("id").asInt();
- }
-
- @Test
- void testSeedPostLifecycle() throws Exception {
- // 列表分页
- mockMvc.perform(get("/seed/posts")
- .param("page", "1")
- .param("size", "5"))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.records[0].id", is(postId)));
-
- // 详情(无评论)
- mockMvc.perform(get("/seed/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.post.id", is(postId)))
- .andExpect(jsonPath("$.data.comments", hasSize(0)));
-
- // 点赞
- mockMvc.perform(post("/seed/posts/{id}/like", postId))
- .andExpect(status().isOk());
-
- mockMvc.perform(get("/seed/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.post.likeCount", is(1)));
-
- // 发布评论
- SeedComment comment = new SeedComment();
- comment.setAuthorId(4004);
- comment.setContent("测试求种评论");
- String cjson = objectMapper.writeValueAsString(comment);
-
- String cres = mockMvc.perform(post("/seed/posts/{id}/comments", postId)
- .contentType(MediaType.APPLICATION_JSON)
- .content(cjson))
- .andExpect(status().isOk())
- .andReturn()
- .getResponse()
- .getContentAsString();
-
- // 从 Result.data.commentId 中取出 commentId
- JsonNode cnode = objectMapper.readTree(cres);
- int commentId = cnode.path("data").path("commentId").asInt();
-
- mockMvc.perform(get("/seed/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.comments[0].id", is(commentId)));
-
- // 点赞评论
- mockMvc.perform(post("/seed/comments/{id}/like", commentId))
- .andExpect(status().isOk());
-
- mockMvc.perform(get("/seed/posts/{id}", postId))
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.data.comments[0].likeCount", is(1)));
- }
-}
diff --git a/src/test/java/com/ptp/ptplatform/controller/TorrentCommentControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/TorrentCommentControllerTest.java
new file mode 100644
index 0000000..585a546
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/TorrentCommentControllerTest.java
@@ -0,0 +1,134 @@
+package com.ptp.ptplatform.controller;
+
+import com.ptp.ptplatform.entity.TorrentComment;
+import com.ptp.ptplatform.service.TorrentCommentService;
+import com.ptp.ptplatform.utils.Result;
+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 java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class TorrentCommentControllerTest {
+
+ @Mock
+ private TorrentCommentService commentService;
+
+ @InjectMocks
+ private TorrentCommentController commentController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void likeComment_ShouldReturnSuccess() {
+ // Arrange
+ int commentId = 1;
+ doNothing().when(commentService).incrementLike(commentId);
+
+ // Act
+ Result result = commentController.like(commentId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals("成功", result.getMessage());
+ verify(commentService, times(1)).incrementLike(commentId);
+ }
+
+ @Test
+ void getReplies_ShouldReturnReplies() {
+ // Arrange
+ int commentId = 1;
+ List<TorrentComment> mockReplies = Arrays.asList(
+ createComment(2, 1, commentId, "user2", "Reply 1", LocalDateTime.now(), 0, "user1"),
+ createComment(3, 1, commentId, "user3", "Reply 2", LocalDateTime.now(), 0, "user1")
+ );
+ when(commentService.getReplies(commentId)).thenReturn(mockReplies);
+
+ // Act
+ Result result = commentController.getReplies(commentId);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertEquals(mockReplies, result.getData().get("replies"));
+ verify(commentService, times(1)).getReplies(commentId);
+ }
+
+ @Test
+ void addReply_ShouldReturnSuccess_WhenParentCommentExists() {
+ // Arrange
+ int commentId = 1;
+ // 使用 createComment 创建父评论
+ TorrentComment parentComment = createComment(
+ commentId, // id
+ 1, // postId
+ 0, // parentId (0表示是顶级评论)
+ "user1", // authorId
+ "Parent comment", // content
+ LocalDateTime.now(), // createTime
+ 5, // likeCount
+ null // replyTo
+ );
+
+ // 创建回复评论
+ TorrentComment reply = new TorrentComment();
+ reply.setAuthorId("user2");
+ reply.setContent("Test reply");
+
+ when(commentService.getById(commentId)).thenReturn(parentComment);
+ when(commentService.save(any(TorrentComment.class))).thenReturn(true);
+
+ // Act
+ Result result = commentController.addReply(commentId, reply);
+
+ // Assert
+ assertEquals(200, result.getCode());
+ assertNotNull(result.getData().get("reply"));
+ assertEquals(commentId, result.getData().get("parentCommentId"));
+ verify(commentService, times(1)).save(any(TorrentComment.class));
+ }
+
+ @Test
+ void addReply_ShouldReturnError_WhenParentCommentNotExists() {
+ // Arrange
+ int commentId = 1;
+ TorrentComment reply = new TorrentComment();
+ reply.setAuthorId("user2");
+ reply.setContent("Test reply");
+
+ when(commentService.getById(commentId)).thenReturn(null);
+
+ // Act
+ Result result = commentController.addReply(commentId, reply);
+
+ // Assert
+ assertEquals(500, result.getCode());
+ assertEquals("被回复的评论不存在", result.getMessage());
+ verify(commentService, never()).save(any(TorrentComment.class));
+ }
+
+ // 辅助方法
+ private TorrentComment createComment(Integer id, Integer postId, Integer parentId,
+ String authorId, String content, LocalDateTime createTime,
+ Integer likeCount, String replyTo) {
+ TorrentComment comment = new TorrentComment();
+ comment.setId(id);
+ comment.setPostId(postId);
+ comment.setParentId(parentId);
+ comment.setAuthorId(authorId);
+ comment.setContent(content);
+ comment.setCreateTime(createTime);
+ comment.setLikeCount(likeCount);
+ comment.setReplyTo(replyTo);
+ return comment;
+ }
+}
diff --git a/src/test/java/com/ptp/ptplatform/controller/TorrentControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/TorrentControllerTest.java
new file mode 100644
index 0000000..9c4fbfa
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/TorrentControllerTest.java
@@ -0,0 +1,102 @@
+package com.ptp.ptplatform.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ptp.ptplatform.entity.Torrent;
+import com.ptp.ptplatform.entity.TorrentComment;
+import com.ptp.ptplatform.service.TorrentCommentService;
+import com.ptp.ptplatform.service.TorrentService;
+import com.ptp.ptplatform.utils.Result;
+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 java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+
+class TorrentControllerTest {
+
+ @Mock
+ private TorrentService torrentService;
+
+ @Mock
+ private TorrentCommentService commentService;
+
+ @InjectMocks
+ private TorrentController torrentController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void getTorrentById_ShouldReturnTorrent_WhenTorrentExists() {
+ // 1. 构造测试数据
+ Torrent mockTorrent = new Torrent();
+ mockTorrent.setId(1);
+ mockTorrent.setTorrentName("Ubuntu 22.04 ISO");
+ mockTorrent.setDescription("Official Ubuntu Linux distribution");
+ mockTorrent.setCategory("Software");
+ mockTorrent.setFilePath("/downloads/ubuntu-22.04.iso");
+ mockTorrent.setCreateTime(LocalDateTime.of(2023, 5, 1, 10, 0));
+ mockTorrent.setRegion("美国");
+ mockTorrent.setLikeCount(200);
+
+ // 模拟空评论列表
+ List<TorrentComment> emptyComments = new ArrayList<>();
+
+ // 2. 模拟服务层行为
+ when(torrentService.getById(1)).thenReturn(mockTorrent);
+ when(commentService.list(any(QueryWrapper.class))).thenReturn(emptyComments);
+
+ // 3. 调用控制器方法
+ Result result = torrentController.getPost(1);
+
+ // 4. 验证结果
+ assertEquals(200, result.getCode());
+ assertNotNull(result.getData().get("torrent"));
+ Torrent returnedTorrent = (Torrent) result.getData().get("torrent");
+ assertEquals("Ubuntu 22.04 ISO", returnedTorrent.getTorrentName());
+ assertEquals(200, returnedTorrent.getLikeCount());
+
+ // 5. 验证服务层调用
+ verify(torrentService, times(1)).getById(1);
+ verify(commentService, times(1)).list(any(QueryWrapper.class));
+ }
+
+ @Test
+ void getTorrentDetails_ShouldReturnError_WhenTorrentNotExists() {
+ when(torrentService.getById(999)).thenReturn(null);
+
+ Result result = torrentController.getPost(999);
+
+ assertEquals(500, result.getCode());
+ assertEquals("种子不存在", result.getMessage());
+ verify(torrentService, times(1)).getById(999);
+ verify(commentService, never()).list(any(QueryWrapper.class));
+ }
+
+ @Test
+ void createTorrent_ShouldSuccess_WithValidData() {
+ Torrent newTorrent = new Torrent();
+ newTorrent.setTorrentName("New Torrent");
+ newTorrent.setDescription("Test creation");
+ newTorrent.setFilePath("/downloads/test.torrent");
+ newTorrent.setRegion("中国");
+
+ when(torrentService.save(any(Torrent.class))).thenReturn(true);
+
+ Result result = torrentController.createTorrent(newTorrent);
+
+ assertEquals(200, result.getCode());
+ assertEquals("成功", result.getMessage());
+ verify(torrentService, times(1)).save(any(Torrent.class));
+ }
+}
\ No newline at end of file