blob: 0ab86edaea53fbdd6681710ac080f479f05390e4 [file] [log] [blame]
22301102aa5adbc2025-05-18 17:51:55 +08001package com.pt.controller;
2
3import com.pt.constant.Constants;
4import com.pt.entity.Post;
5import com.pt.service.PostService;
6import com.pt.utils.JWTUtils;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.ResponseEntity;
9import org.springframework.web.bind.annotation.*;
10
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14
15@RestController
16@RequestMapping("/api/posts")
17@CrossOrigin(origins = "*")
18public class PostController {
19
20 @Autowired
21 private PostService postService;
22
23 /*
24 * 创建一个新帖子
25 * @param token 用户的JWT令牌
26 * @param title 帖子标题
27 * @param content 帖子内容
28 * @param author 帖子作者(用户名)
29 * @return 创建成功的响应
30 */
31 @PostMapping("/create")
32 public ResponseEntity<?> createPost(
33 @RequestHeader("token") String token,
34 @RequestParam("title") String title,
35 @RequestParam("content") String content,
36 @RequestParam("author") String author
37 ) {
38
39 Map<String, Object> ans = new HashMap<>();
40
41 if(!JWTUtils.checkToken(token, author, Constants.UserRole.USER)){
42 ans.put("result", "Invalid token");
43 return ResponseEntity.badRequest().body(ans);
44 }
45
46 Post existingPost = postService.findPostByTitle(title);
47 if (existingPost != null) {
48 ans.put("result", "Post with this title already exists");
49 return ResponseEntity.badRequest().body(ans);
50 }
51
52 postService.createPost(title, content, author);
53 ans.put("result", "Post created successfully");
54 return ResponseEntity.ok(ans);
55 }
56
57 /*
58 * 获取帖子列表
59 * @param token 用户的JWT令牌
60 * @param username 用户名
61 * @param title 帖子标题(可选)
62 * @param author 帖子作者(可选)
63 * @param date 帖子发布日期(可选)
22301102aa5adbc2025-05-18 17:51:55 +080064 */
65 @GetMapping("/list")
66 public ResponseEntity<?> getPost(
67 @RequestHeader("token") String token,
68 @RequestParam("username") String username,
69 @RequestParam(value = "title", required = false) String title,
70 @RequestParam(value = "author", required = false) String author,
71 @RequestParam(value = "date", required = false) String date
72 ) {
73
74 Map<String, Object> ans = new HashMap<>();
75
76 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
77 ans.put("result", "Invalid token");
78 return ResponseEntity.badRequest().body(ans);
79 }
80
81 List<Post> posts = postService.listAll();
82
83 if(title != null){
84 posts.removeIf(post -> !post.getTitle().startsWith(title));
85 }
86
87 if(author != null){
88 posts.removeIf(post -> !post.getAuthor().startsWith(author));
89 }
90
91 if(date != null){
92 posts.removeIf(post -> !post.getPublishDate().toString().equals(date));
93 }
94 ans.put("result", "Post retrieved successfully");
22301102aadb0ac2025-06-05 18:02:21 +080095 ans.put("data", Map.of(
96 "post", posts
97 ));
22301102aa5adbc2025-05-18 17:51:55 +080098 return ResponseEntity.ok(ans);
99 }
100
101 /*
102 * 删除帖子
103 * @param token 用户的JWT令牌
104 * @param username 用户名
105 * @param pid 帖子ID
106 * @return 删除成功的响应
107 */
108 @DeleteMapping("/delete")
109 public ResponseEntity<?> deletePost(
110 @RequestHeader("token") String token,
111 @RequestParam("username") String username,
112 @RequestParam("pid") int pid
113 ) {
114
115 Map<String, Object> ans = new HashMap<>();
116
117 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
118 ans.put("result", "Invalid token");
119 return ResponseEntity.badRequest().body(ans);
120 }
121
122 Post post = postService.findPostById(pid);
123 if (post == null) {
124 ans.put("result", "Post not found");
125 return ResponseEntity.badRequest().body(ans);
126 }
127
128 postService.deletePost(post);
129 ans.put("result", "Post deleted successfully");
130 return ResponseEntity.ok(ans);
131 }
132}