blob: 844dc650497074295f143f4a255ddf202ebdd781 [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 帖子发布日期(可选)
64 * @return 帖子列表的响应
65 */
66 @GetMapping("/list")
67 public ResponseEntity<?> getPost(
68 @RequestHeader("token") String token,
69 @RequestParam("username") String username,
70 @RequestParam(value = "title", required = false) String title,
71 @RequestParam(value = "author", required = false) String author,
72 @RequestParam(value = "date", required = false) String date
73 ) {
74
75 Map<String, Object> ans = new HashMap<>();
76
77 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
78 ans.put("result", "Invalid token");
79 return ResponseEntity.badRequest().body(ans);
80 }
81
82 List<Post> posts = postService.listAll();
83
84 if(title != null){
85 posts.removeIf(post -> !post.getTitle().startsWith(title));
86 }
87
88 if(author != null){
89 posts.removeIf(post -> !post.getAuthor().startsWith(author));
90 }
91
92 if(date != null){
93 posts.removeIf(post -> !post.getPublishDate().toString().equals(date));
94 }
95 ans.put("result", "Post retrieved successfully");
96 ans.put("post", posts);
97 return ResponseEntity.ok(ans);
98 }
99
100 /*
101 * 删除帖子
102 * @param token 用户的JWT令牌
103 * @param username 用户名
104 * @param pid 帖子ID
105 * @return 删除成功的响应
106 */
107 @DeleteMapping("/delete")
108 public ResponseEntity<?> deletePost(
109 @RequestHeader("token") String token,
110 @RequestParam("username") String username,
111 @RequestParam("pid") int pid
112 ) {
113
114 Map<String, Object> ans = new HashMap<>();
115
116 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
117 ans.put("result", "Invalid token");
118 return ResponseEntity.badRequest().body(ans);
119 }
120
121 Post post = postService.findPostById(pid);
122 if (post == null) {
123 ans.put("result", "Post not found");
124 return ResponseEntity.badRequest().body(ans);
125 }
126
127 postService.deletePost(post);
128 ans.put("result", "Post deleted successfully");
129 return ResponseEntity.ok(ans);
130 }
131}