22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
| 3 | import com.pt.constant.Constants; |
| 4 | import com.pt.entity.Post; |
| 5 | import com.pt.service.PostService; |
| 6 | import com.pt.utils.JWTUtils; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.http.ResponseEntity; |
| 9 | import org.springframework.web.bind.annotation.*; |
| 10 | |
| 11 | import java.util.HashMap; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | @RestController |
| 16 | @RequestMapping("/api/posts") |
| 17 | @CrossOrigin(origins = "*") |
| 18 | public 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, |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 34 | @RequestBody Map<String, String> request |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 35 | ) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 36 | String title = request.get("title"); |
| 37 | String content = request.get("content"); |
| 38 | String author = request.get("author"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 39 | |
| 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 | |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 47 | postService.createPost(title, content, author); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 48 | ans.put("message", "Post created successfully"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 49 | return ResponseEntity.ok(ans); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * 获取帖子列表 |
| 54 | * @param token 用户的JWT令牌 |
| 55 | * @param username 用户名 |
| 56 | * @param title 帖子标题(可选) |
| 57 | * @param author 帖子作者(可选) |
| 58 | * @param date 帖子发布日期(可选) |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 59 | */ |
| 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 | ) { |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 68 | Map<String, Object> ans = new HashMap<>(); |
| 69 | |
| 70 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 71 | ans.put("message", "Invalid token"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 72 | 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 | } |
22301102 | f567030 | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 88 | |
| 89 | posts.sort( |
| 90 | // 按发布日期降序排序 |
| 91 | (p1, p2) -> p2.getPublishDate().compareTo(p1.getPublishDate()) |
| 92 | ); |
| 93 | |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 94 | ans.put("message", "Post retrieved successfully"); |
22301102 | aadb0ac | 2025-06-05 18:02:21 +0800 | [diff] [blame] | 95 | ans.put("data", Map.of( |
22301102 | f567030 | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 96 | "posts", posts |
22301102 | aadb0ac | 2025-06-05 18:02:21 +0800 | [diff] [blame] | 97 | )); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 98 | 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, |
22301102 | f567030 | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 111 | @RequestParam("username") String username, |
| 112 | @RequestParam("pid") int pid |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 113 | ) { |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 114 | Map<String, Object> ans = new HashMap<>(); |
| 115 | |
| 116 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 117 | ans.put("message", "Invalid token"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 118 | return ResponseEntity.badRequest().body(ans); |
| 119 | } |
| 120 | |
| 121 | Post post = postService.findPostById(pid); |
| 122 | if (post == null) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 123 | ans.put("message", "Post not found"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 124 | return ResponseEntity.badRequest().body(ans); |
| 125 | } |
| 126 | |
| 127 | postService.deletePost(post); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 128 | ans.put("message", "Post deleted successfully"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 129 | return ResponseEntity.ok(ans); |
| 130 | } |
| 131 | } |