blob: c61bd3e35608829dfef51b17e1dc5b15283e3fa3 [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,
22301102f69709e2025-06-08 14:10:02 +080034 @RequestBody Map<String, String> request
22301102aa5adbc2025-05-18 17:51:55 +080035 ) {
22301102f69709e2025-06-08 14:10:02 +080036 String title = request.get("title");
37 String content = request.get("content");
38 String author = request.get("author");
22301102aa5adbc2025-05-18 17:51:55 +080039
40 Map<String, Object> ans = new HashMap<>();
41
42 if(!JWTUtils.checkToken(token, author, Constants.UserRole.USER)){
43 ans.put("result", "Invalid token");
44 return ResponseEntity.badRequest().body(ans);
45 }
46
22301102aa5adbc2025-05-18 17:51:55 +080047 postService.createPost(title, content, author);
22301102f69709e2025-06-08 14:10:02 +080048 ans.put("message", "Post created successfully");
22301102aa5adbc2025-05-18 17:51:55 +080049 return ResponseEntity.ok(ans);
50 }
51
52 /*
53 * 获取帖子列表
54 * @param token 用户的JWT令牌
55 * @param username 用户名
56 * @param title 帖子标题(可选)
57 * @param author 帖子作者(可选)
58 * @param date 帖子发布日期(可选)
22301102aa5adbc2025-05-18 17:51:55 +080059 */
60 @GetMapping("/list")
61 public ResponseEntity<?> getPost(
62 @RequestHeader("token") String token,
63 @RequestParam("username") String username,
64 @RequestParam(value = "title", required = false) String title,
65 @RequestParam(value = "author", required = false) String author,
66 @RequestParam(value = "date", required = false) String date
67 ) {
22301102aa5adbc2025-05-18 17:51:55 +080068 Map<String, Object> ans = new HashMap<>();
69
70 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
22301102f69709e2025-06-08 14:10:02 +080071 ans.put("message", "Invalid token");
22301102aa5adbc2025-05-18 17:51:55 +080072 return ResponseEntity.badRequest().body(ans);
73 }
74
75 List<Post> posts = postService.listAll();
76
77 if(title != null){
78 posts.removeIf(post -> !post.getTitle().startsWith(title));
79 }
80
81 if(author != null){
82 posts.removeIf(post -> !post.getAuthor().startsWith(author));
83 }
84
85 if(date != null){
86 posts.removeIf(post -> !post.getPublishDate().toString().equals(date));
87 }
22301102039b52a2025-06-08 14:10:02 +080088
89 posts.sort(
90 // 按发布日期降序排序
91 (p1, p2) -> p2.getPublishDate().compareTo(p1.getPublishDate())
92 );
93
22301102f69709e2025-06-08 14:10:02 +080094 ans.put("message", "Post retrieved successfully");
22301102aadb0ac2025-06-05 18:02:21 +080095 ans.put("data", Map.of(
22301102039b52a2025-06-08 14:10:02 +080096 "posts", posts
22301102aadb0ac2025-06-05 18:02:21 +080097 ));
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,
22301102039b52a2025-06-08 14:10:02 +0800111 @RequestParam("username") String username,
112 @RequestParam("pid") int pid
22301102aa5adbc2025-05-18 17:51:55 +0800113 ) {
22301102aa5adbc2025-05-18 17:51:55 +0800114 Map<String, Object> ans = new HashMap<>();
115
116 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
22301102f69709e2025-06-08 14:10:02 +0800117 ans.put("message", "Invalid token");
22301102aa5adbc2025-05-18 17:51:55 +0800118 return ResponseEntity.badRequest().body(ans);
119 }
120
121 Post post = postService.findPostById(pid);
122 if (post == null) {
22301102f69709e2025-06-08 14:10:02 +0800123 ans.put("message", "Post not found");
22301102aa5adbc2025-05-18 17:51:55 +0800124 return ResponseEntity.badRequest().body(ans);
125 }
126
127 postService.deletePost(post);
22301102f69709e2025-06-08 14:10:02 +0800128 ans.put("message", "Post deleted successfully");
22301102aa5adbc2025-05-18 17:51:55 +0800129 return ResponseEntity.ok(ans);
130 }
131}