blob: 3723dbdf8df1e5cdfac26a6d1e8c1975ed65bde5 [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
47 Post existingPost = postService.findPostByTitle(title);
48 if (existingPost != null) {
49 ans.put("result", "Post with this title already exists");
50 return ResponseEntity.badRequest().body(ans);
51 }
52
53 postService.createPost(title, content, author);
22301102f69709e2025-06-08 14:10:02 +080054 ans.put("message", "Post created successfully");
22301102aa5adbc2025-05-18 17:51:55 +080055 return ResponseEntity.ok(ans);
56 }
57
58 /*
59 * 获取帖子列表
60 * @param token 用户的JWT令牌
61 * @param username 用户名
62 * @param title 帖子标题(可选)
63 * @param author 帖子作者(可选)
64 * @param date 帖子发布日期(可选)
22301102aa5adbc2025-05-18 17:51:55 +080065 */
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 ) {
22301102aa5adbc2025-05-18 17:51:55 +080074 Map<String, Object> ans = new HashMap<>();
75
76 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){
22301102f69709e2025-06-08 14:10:02 +080077 ans.put("message", "Invalid token");
22301102aa5adbc2025-05-18 17:51:55 +080078 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 }
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(
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,
22301102f69709e2025-06-08 14:10:02 +0800111 @RequestBody Map<String, String> request
22301102aa5adbc2025-05-18 17:51:55 +0800112 ) {
22301102f69709e2025-06-08 14:10:02 +0800113 String username = request.get("username");
114 int pid = Integer.parseInt(request.get("pid"));
22301102aa5adbc2025-05-18 17:51:55 +0800115
116 Map<String, Object> ans = new HashMap<>();
117
118 if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
22301102f69709e2025-06-08 14:10:02 +0800119 ans.put("message", "Invalid token");
22301102aa5adbc2025-05-18 17:51:55 +0800120 return ResponseEntity.badRequest().body(ans);
121 }
122
123 Post post = postService.findPostById(pid);
124 if (post == null) {
22301102f69709e2025-06-08 14:10:02 +0800125 ans.put("message", "Post not found");
22301102aa5adbc2025-05-18 17:51:55 +0800126 return ResponseEntity.badRequest().body(ans);
127 }
128
129 postService.deletePost(post);
22301102f69709e2025-06-08 14:10:02 +0800130 ans.put("message", "Post deleted successfully");
22301102aa5adbc2025-05-18 17:51:55 +0800131 return ResponseEntity.ok(ans);
132 }
133}