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, |
| 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 帖子发布日期(可选) |
22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame] | 64 | */ |
| 65 | @GetMapping("/list") |
| 66 | public ResponseEntity<?> getPost( |
| 67 | @RequestHeader("token") String token, |
| 68 | @RequestParam("username") String username, |
| 69 | @RequestParam(value = "title", required = false) String title, |
| 70 | @RequestParam(value = "author", required = false) String author, |
| 71 | @RequestParam(value = "date", required = false) String date |
| 72 | ) { |
| 73 | |
| 74 | Map<String, Object> ans = new HashMap<>(); |
| 75 | |
| 76 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)){ |
| 77 | ans.put("result", "Invalid token"); |
| 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 | } |
| 94 | ans.put("result", "Post retrieved successfully"); |
| 95 | ans.put("post", posts); |
| 96 | return ResponseEntity.ok(ans); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * 删除帖子 |
| 101 | * @param token 用户的JWT令牌 |
| 102 | * @param username 用户名 |
| 103 | * @param pid 帖子ID |
| 104 | * @return 删除成功的响应 |
| 105 | */ |
| 106 | @DeleteMapping("/delete") |
| 107 | public ResponseEntity<?> deletePost( |
| 108 | @RequestHeader("token") String token, |
| 109 | @RequestParam("username") String username, |
| 110 | @RequestParam("pid") int pid |
| 111 | ) { |
| 112 | |
| 113 | Map<String, Object> ans = new HashMap<>(); |
| 114 | |
| 115 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){ |
| 116 | ans.put("result", "Invalid token"); |
| 117 | return ResponseEntity.badRequest().body(ans); |
| 118 | } |
| 119 | |
| 120 | Post post = postService.findPostById(pid); |
| 121 | if (post == null) { |
| 122 | ans.put("result", "Post not found"); |
| 123 | return ResponseEntity.badRequest().body(ans); |
| 124 | } |
| 125 | |
| 126 | postService.deletePost(post); |
| 127 | ans.put("result", "Post deleted successfully"); |
| 128 | return ResponseEntity.ok(ans); |
| 129 | } |
| 130 | } |