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 | |
| 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); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 54 | ans.put("message", "Post created successfully"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 55 | return ResponseEntity.ok(ans); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * 获取帖子列表 |
| 60 | * @param token 用户的JWT令牌 |
| 61 | * @param username 用户名 |
| 62 | * @param title 帖子标题(可选) |
| 63 | * @param author 帖子作者(可选) |
| 64 | * @param date 帖子发布日期(可选) |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 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 | ) { |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 74 | Map<String, Object> ans = new HashMap<>(); |
| 75 | |
| 76 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 77 | ans.put("message", "Invalid token"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 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 | } |
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( |
| 96 | "post", posts |
| 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 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 111 | @RequestBody Map<String, String> request |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 112 | ) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 113 | String username = request.get("username"); |
| 114 | int pid = Integer.parseInt(request.get("pid")); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 115 | |
| 116 | Map<String, Object> ans = new HashMap<>(); |
| 117 | |
| 118 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){ |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 119 | ans.put("message", "Invalid token"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 120 | return ResponseEntity.badRequest().body(ans); |
| 121 | } |
| 122 | |
| 123 | Post post = postService.findPostById(pid); |
| 124 | if (post == null) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 125 | ans.put("message", "Post not found"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 126 | return ResponseEntity.badRequest().body(ans); |
| 127 | } |
| 128 | |
| 129 | postService.deletePost(post); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame^] | 130 | ans.put("message", "Post deleted successfully"); |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 131 | return ResponseEntity.ok(ans); |
| 132 | } |
| 133 | } |