Api debug

Change-Id: Ie29ca8487dd8269e7708f53ba93e89bc0169a54f
diff --git a/src/main/java/api/ApiController.java b/src/main/java/api/ApiController.java
index b8ee0c1..326255d 100644
--- a/src/main/java/api/ApiController.java
+++ b/src/main/java/api/ApiController.java
@@ -26,6 +26,7 @@
 import entity.Seed;
 import entity.User;
 import entity.Post;
+import entity.PostReply;
 
 import java.util.UUID;
 
@@ -478,6 +479,7 @@
     }
 
     @Override
+    @CrossOrigin(origins = "*", allowedHeaders = "*")
     public ResponseEntity<String> getForum() {
         try {
             Post[] posts = db1.GetPostList();
@@ -494,4 +496,173 @@
             return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR);
         }
     }
+
+    @Override
+    @CrossOrigin(origins = "*", allowedHeaders = "*")
+    public ResponseEntity<String> getPostById(
+        @RequestParam("postid") String postid
+    ) {
+        try {
+            Post post = db1.GetPost(postid);
+            PostReply[] replies = db1.GetPostReplyList(postid);
+            if (post == null) {
+                return new ResponseEntity<>("", HttpStatus.NOT_FOUND); // 返回 404 表示帖子未找到
+            }
+            
+            // 创建合并的 JSON 对象
+            com.fasterxml.jackson.databind.node.ObjectNode resultJson = mapper.createObjectNode();
+            
+            // 添加 post 的所有字段
+            resultJson.put("postid", post.postid);
+            resultJson.put("posttitle", post.posttitle);
+            resultJson.put("postcontent", post.postcontent);
+            resultJson.put("postuserid", post.postuserid);
+            resultJson.put("replytime", post.replytime);
+            resultJson.put("readtime", post.readtime);
+            
+            if (post.posttime != null) {
+                resultJson.put("posttime", post.posttime.getTime());
+            }
+            
+            // 添加作者信息
+            if (post.author != null) {
+                com.fasterxml.jackson.databind.node.ObjectNode authorJson = mapper.createObjectNode();
+                authorJson.put("userid", post.author.userid);
+                authorJson.put("username", post.author.username);
+                authorJson.put("email", post.author.email);
+                authorJson.put("sex", post.author.sex);
+                authorJson.put("school", post.author.school);
+                authorJson.put("pictureurl", post.author.pictureurl);
+                authorJson.put("profile", post.author.profile);
+                authorJson.put("accountstate", post.author.accountstate);
+                authorJson.put("invitetimes", post.author.invitetimes);
+                if (post.author.lastDetectedTime != null) {
+                    authorJson.put("lastDetectedTime", post.author.lastDetectedTime.getTime());
+                }
+                if (post.author.fakeLastDetectedTime != null) {
+                    authorJson.put("fakeLastDetectedTime", post.author.fakeLastDetectedTime.getTime());
+                }
+                resultJson.set("author", authorJson);
+            }
+            
+            // 添加 replies 数组
+            com.fasterxml.jackson.databind.node.ArrayNode repliesArray = mapper.createArrayNode();
+            if (replies != null) {
+                System.out.println("Replies count: " + replies.length);
+                for (PostReply reply : replies) {
+                    com.fasterxml.jackson.databind.node.ObjectNode replyJson = mapper.createObjectNode();
+                    replyJson.put("replyid", reply.replyid);
+                    replyJson.put("postid", reply.postid);
+                    replyJson.put("authorid", reply.authorid);
+                    replyJson.put("content", reply.content);
+                    if (reply.createdAt != null) {
+                        replyJson.put("createdAt", reply.createdAt.getTime());
+                    }
+                    
+                    // 添加回复作者信息
+                    if (reply.author != null) {
+                        com.fasterxml.jackson.databind.node.ObjectNode replyAuthorJson = mapper.createObjectNode();
+                        replyAuthorJson.put("userid", reply.author.userid);
+                        replyAuthorJson.put("username", reply.author.username);
+                        replyAuthorJson.put("email", reply.author.email);
+                        replyAuthorJson.put("sex", reply.author.sex);
+                        replyAuthorJson.put("school", reply.author.school);
+                        replyAuthorJson.put("pictureurl", reply.author.pictureurl);
+                        replyAuthorJson.put("profile", reply.author.profile);
+                        replyAuthorJson.put("accountstate", reply.author.accountstate);
+                        replyAuthorJson.put("invitetimes", reply.author.invitetimes);
+                        if (reply.author.lastDetectedTime != null) {
+                            replyAuthorJson.put("lastDetectedTime", reply.author.lastDetectedTime.getTime());
+                        }
+                        if (reply.author.fakeLastDetectedTime != null) {
+                            replyAuthorJson.put("fakeLastDetectedTime", reply.author.fakeLastDetectedTime.getTime());
+                        }
+                        replyJson.set("author", replyAuthorJson);
+                    }
+                    
+                    repliesArray.add(replyJson);
+                }
+            }
+            resultJson.set("replies", repliesArray);
+            
+            String json = mapper.writeValueAsString(resultJson);
+            return new ResponseEntity<>(json, HttpStatus.OK);
+        } catch (JsonProcessingException e) {
+            // e.printStackTrace();
+            return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR);
+        } catch (Exception e) {
+            // e.printStackTrace();
+            return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @Override
+    @CrossOrigin(origins = "*", allowedHeaders = "*")
+    public ResponseEntity<Integer> addPostReply(
+        @RequestBody String requestBody
+    ) {
+        try {
+            System.out.println("Add reply request body: " + requestBody);
+            // 解析 JSON 数据
+            com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+            com.fasterxml.jackson.databind.JsonNode postidNode = jsonNode.get("postid");
+            com.fasterxml.jackson.databind.JsonNode authoridNode = jsonNode.get("replyuserid");
+            com.fasterxml.jackson.databind.JsonNode contentNode = jsonNode.get("replycontent");
+
+            if (postidNode == null || authoridNode == null || contentNode == null) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
+            }
+
+            String postid = postidNode.asText();
+            String authorid = authoridNode.asText();
+            String content = contentNode.asText();
+
+            System.out.println("Post ID: " + postid);
+            System.out.println("Author ID: " + authorid);
+            System.out.println("Content: " + content);
+
+            // 参数验证
+            if (postid == null || postid.trim().isEmpty() || 
+                authorid == null || authorid.trim().isEmpty() || 
+                content == null || content.trim().isEmpty()) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
+            }
+            
+            int ret = db1.AddComment(postid, authorid, content);
+            System.out.println("Add comment result: " + ret);
+            if (ret == 0) {
+                return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
+            } else {
+                return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
+            }
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+            return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @Override
+    public ResponseEntity<String> searchSeeds(
+        @RequestParam("tag") String tag,
+        @RequestParam("keyword") String query
+    ) {
+        return null;
+    //     try {
+    //         Seed[] seeds = db1.SearchSeeds(query);
+    //         if (seeds == null || seeds.length == 0) {
+    //             return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
+    //         }
+    //         String json = mapper.writeValueAsString(seeds);
+    //         return new ResponseEntity<>(json, headers, HttpStatus.OK);
+    //     } catch (JsonProcessingException e) {
+    //         e.printStackTrace();
+    //         return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+    //     } catch (Exception e) {
+    //         e.printStackTrace();
+    //         return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+    //     }
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/api/ApiInterface.java b/src/main/java/api/ApiInterface.java
index 4a2c02e..d2e804b 100644
--- a/src/main/java/api/ApiInterface.java
+++ b/src/main/java/api/ApiInterface.java
@@ -75,4 +75,20 @@
 
     @GetMapping("/forum")
     ResponseEntity<String> getForum();
+
+    @GetMapping("/forum-detail")
+    ResponseEntity<String> getPostById(
+        @RequestParam("postid") String postid
+    );
+
+    @PostMapping("/forum-reply")
+    ResponseEntity<Integer> addPostReply(
+        @RequestBody String requestBody
+    );
+
+    @GetMapping("/search-seeds")
+    ResponseEntity<String> searchSeeds(
+        @RequestParam("tag") String tag,
+        @RequestParam("keyword") String query
+    );
 }
\ No newline at end of file
diff --git a/src/main/java/database/DataManagerInterface.java b/src/main/java/database/DataManagerInterface.java
index c1df413..357b81f 100644
--- a/src/main/java/database/DataManagerInterface.java
+++ b/src/main/java/database/DataManagerInterface.java
@@ -7,6 +7,7 @@
 import entity.Seed;
 import entity.User;
 import entity.UserPT;
+import entity.PostReply;
 public interface DataManagerInterface{
 //DB1
 
@@ -51,6 +52,8 @@
     public void SettleBeg();//结算所有求种信息,求种信息中需要增加Beg截止日期,默认14天,期间投票>的则Beg成功,否则Beg失败,并发放对应奖励
 
     public Post[] GetPostList();//获取用户的帖子列表
+    public Post GetPost(String postid);//获取一个帖子的详细信息
+    public PostReply[] GetPostReplyList(String postid);//获取一个帖子的回复列表
     public int AddPost(Post post);//添加一个新的帖子,返回状态:0 success,1 重复,2其他原因
     public int UpdatePost(Post post);//更新一个帖子,返回状态:0 success,1 不存在,2其他原因
     public int DeletePost(String postid);//删除一个帖子,返回状态:0 success,1 不存在,2其他原因
diff --git a/src/main/java/database/Database1.java b/src/main/java/database/Database1.java
index 030abdd..0689e69 100644
--- a/src/main/java/database/Database1.java
+++ b/src/main/java/database/Database1.java
@@ -26,6 +26,8 @@
 import entity.UserPT;
 import entity.UserStar;
 import entity.config;
+import entity.PostReply;
+import entity.QPostReply;
 
 public class Database1 implements DataManagerInterface {
     @PersistenceContext
@@ -608,5 +610,20 @@
         return posts.toArray(new Post[0]);
     }
 
+    @Override
+    public Post GetPost(String postid) {
+        JPAQuery<Post> query = new JPAQuery<>(entitymanager);
+        QPost p = QPost.post;
+        Post post = query.select(p).from(p).where(p.postid.eq(postid)).fetchOne();
+        return post;
+    }
+
+    @Override
+    public PostReply[] GetPostReplyList(String postid) {
+        JPAQuery<PostReply> query = new JPAQuery<>(entitymanager);
+        QPostReply p = QPostReply.postReply;
+        List<PostReply> replies = query.select(p).from(p).where(p.postid.eq(postid)).fetch();
+        return replies.toArray(new PostReply[0]);
+    }
 }
 
diff --git a/src/main/java/database/Database2.java b/src/main/java/database/Database2.java
index 7a58953..d7c5eee 100644
--- a/src/main/java/database/Database2.java
+++ b/src/main/java/database/Database2.java
@@ -1106,4 +1106,14 @@
     public Post[] GetPostList(){
         return null;
     }
+
+    @Override
+    public Post GetPost(String postid) {
+        return null;
+    }
+
+    @Override
+    public PostReply[] GetPostReplyList(String postid) {
+        return null;
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/entity/Post.java b/src/main/java/entity/Post.java
index 5245d1c..009061e 100644
--- a/src/main/java/entity/Post.java
+++ b/src/main/java/entity/Post.java
@@ -1,6 +1,7 @@
 package entity;
 
 import java.util.Date;
+import java.util.List;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -13,6 +14,9 @@
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import com.querydsl.core.annotations.QueryEntity;
+import javax.persistence.OneToMany;
+import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
 
 @QueryEntity
 @Entity
@@ -35,7 +39,7 @@
     @ManyToOne(optional = false)
     @JoinColumn(name = "author_id", referencedColumnName = "user_id", foreignKey = @ForeignKey(name = "fk_post_user"), insertable = false, updatable = false)
     public User author;
-
+    
     @Column(name = "created_at", nullable = false)
     @Temporal(TemporalType.TIMESTAMP)
     public Date posttime;