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