wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 2 | |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 5 | import com.example.g8backend.dto.ApiResponse; |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 6 | import com.example.g8backend.dto.PostCreateDTO; |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 7 | import com.example.g8backend.dto.PostHistoryDTO; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 8 | import com.example.g8backend.entity.Post; |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 9 | import com.example.g8backend.entity.PostView; |
| 10 | import com.example.g8backend.mapper.PostViewMapper; |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 11 | import com.example.g8backend.service.IPostRatingService; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 12 | import com.example.g8backend.service.IPostService; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 13 | import org.springframework.beans.factory.annotation.Autowired; |
| 14 | import org.springframework.http.ResponseEntity; |
| 15 | import org.springframework.security.core.Authentication; |
| 16 | import org.springframework.security.core.context.SecurityContextHolder; |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 17 | import org.springframework.validation.annotation.Validated; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 18 | import org.springframework.web.bind.annotation.*; |
夜雨声烦 | 0a3df4a | 2025-05-13 21:26:13 +0800 | [diff] [blame] | 19 | import com.example.g8backend.entity.Report; |
| 20 | import com.example.g8backend.service.IReportService; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 21 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 22 | import java.util.List; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 23 | |
夜雨声烦 | 0a3df4a | 2025-05-13 21:26:13 +0800 | [diff] [blame] | 24 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 25 | @RestController |
| 26 | @RequestMapping("/post") |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 27 | @Validated |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 28 | public class PostController { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 29 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 30 | @Autowired |
| 31 | private IPostService postService; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 32 | |
| 33 | @Autowired |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 34 | private PostViewMapper postViewMapper; |
夜雨声烦 | 0a3df4a | 2025-05-13 21:26:13 +0800 | [diff] [blame] | 35 | @Autowired |
| 36 | private IReportService reportService; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 37 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 38 | @PostMapping("") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 39 | public ResponseEntity<ApiResponse<Void>> createPost(@RequestBody PostCreateDTO postCreateDTO) { |
| 40 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 41 | Post post = postCreateDTO.getPost(); |
| 42 | Long[] tagIds = postCreateDTO.getTagIds(); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 43 | post.setUserId(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 44 | if (tagIds.length > 0) { |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 45 | postService.createPost(post, tagIds); |
| 46 | } else { |
| 47 | postService.createPost(post); |
| 48 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 49 | return ResponseEntity.ok(ApiResponse.message("Post created successfully.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 50 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 51 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 52 | @GetMapping("/{postId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 53 | public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) { |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 54 | Post post = postService.getById(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 55 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 56 | postService.recordViewHistory(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 57 | return ResponseEntity.ok(ApiResponse.success(post)); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 58 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 59 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 60 | @GetMapping("/getAll") |
| 61 | public ResponseEntity<ApiResponse<List<Post>>> getAllPosts() { |
| 62 | return ResponseEntity.ok(ApiResponse.success(postService.list())); |
| 63 | } |
| 64 | |
| 65 | @GetMapping("/getByUserId/{userId}") |
| 66 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByUserId(@PathVariable Long userId) { |
| 67 | return ResponseEntity.ok(ApiResponse.success(postService.getPostsByUserId(userId))); |
| 68 | } |
| 69 | |
| 70 | @PutMapping("/{postId}") |
| 71 | public ResponseEntity<ApiResponse<String>> updatePost(@PathVariable Long postId, @RequestBody Post post) { |
| 72 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 73 | Post existingPost = postService.getById(postId); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 74 | if (existingPost == null) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 75 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 76 | } |
| 77 | if (existingPost.getUserId() != userId) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 78 | return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to update this post.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 79 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 80 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 81 | post.setPostId(postId); |
| 82 | post.setUserId(userId); |
| 83 | postService.updateById(post); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 84 | return ResponseEntity.ok(ApiResponse.message("Post updated successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 85 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 86 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 87 | @GetMapping("/type/{postType}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 88 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 89 | List<Post> posts = postService.getPostsByType(postType); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 90 | return ResponseEntity.ok(ApiResponse.success(posts)); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 91 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 92 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 93 | @PostMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 94 | public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) { |
| 95 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 96 | postService.likePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 97 | return ResponseEntity.ok(ApiResponse.message("Post liked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 98 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 99 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 100 | @DeleteMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 101 | public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) { |
| 102 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 103 | postService.unlikePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 104 | return ResponseEntity.ok(ApiResponse.message("Post unliked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 105 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 106 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 107 | @GetMapping("/{postId}/likes") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 108 | public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 109 | Long likeCount = postService.getPostLikeCount(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 110 | return ResponseEntity.ok(ApiResponse.success(likeCount)); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 111 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 112 | |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 113 | @GetMapping("/search") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 114 | public ResponseEntity<ApiResponse<List<Post>>> searchPosts( |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 115 | @RequestParam(required = false) String keyword, |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 116 | @RequestParam(required = false) List<Long> tags, |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 117 | @RequestParam(required = false) String author) { |
夜雨声烦 | 39b8cfe | 2025-06-08 14:10:08 +0800 | [diff] [blame^] | 118 | System.out.println(keyword + tags + author); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 119 | List<Post> result = postService.searchPosts(keyword, tags, author); |
| 120 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 121 | } |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 122 | |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 123 | @GetMapping("/history") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 124 | public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() { |
| 125 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 126 | List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 127 | return ResponseEntity.ok(ApiResponse.success(history)); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 128 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 129 | |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 130 | @GetMapping("/recommended") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 131 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 132 | @RequestParam(defaultValue = "1") int page, |
| 133 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 134 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 135 | Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 136 | return ResponseEntity.ok(ApiResponse.success(pageResult)); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 137 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 138 | |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 139 | @GetMapping("/recommended-by-tags") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 140 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags( |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 141 | @RequestParam(defaultValue = "1") int page, |
| 142 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 143 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 144 | Page<Post> result = postService.getRecommendedByTags(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 145 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 146 | } |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 147 | @Autowired |
| 148 | private IPostRatingService postRatingService; |
| 149 | |
| 150 | @PostMapping("/{postId}/rate") |
| 151 | public ResponseEntity<ApiResponse<String>> ratePost( |
| 152 | @PathVariable Long postId, |
| 153 | @RequestParam Integer rating) { |
| 154 | try { |
| 155 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 156 | |
| 157 | // 调用服务层方法(服务层已内置校验逻辑) |
| 158 | postRatingService.ratePost(userId, postId, rating); |
| 159 | |
| 160 | // 成功时返回空数据 |
| 161 | return ResponseEntity.ok(ApiResponse.success("评分成功")); |
| 162 | |
| 163 | } catch (IllegalArgumentException e) { |
| 164 | // 处理参数校验异常(如评分范围错误) |
| 165 | return ResponseEntity.badRequest() |
| 166 | .body(ApiResponse.error(400, e.getMessage())); |
| 167 | |
| 168 | } catch (RuntimeException e) { |
| 169 | // 处理数据库操作失败等运行时异常 |
| 170 | return ResponseEntity.internalServerError() |
| 171 | .body(ApiResponse.error(500, e.getMessage())); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @GetMapping("/{postId}/average-rating") |
| 176 | public ResponseEntity<ApiResponse<Double>> getAverageRating(@PathVariable Long postId) { |
| 177 | Double avg = postRatingService.getAverageRating(postId); |
| 178 | return ResponseEntity.ok(ApiResponse.success(avg)); |
| 179 | } |
| 180 | |
| 181 | @GetMapping("/{postId}/rating-users/count") |
| 182 | public ResponseEntity<ApiResponse<Long>> getRatingUserCount(@PathVariable Long postId) { |
| 183 | Long count = postRatingService.getRatingUserCount(postId); |
| 184 | return ResponseEntity.ok(ApiResponse.success(count)); |
| 185 | } |
夜雨声烦 | 0a3df4a | 2025-05-13 21:26:13 +0800 | [diff] [blame] | 186 | @PostMapping("/{postId}/report") |
| 187 | public ResponseEntity<ApiResponse<String>> reportPost( |
| 188 | @PathVariable Long postId, |
| 189 | @RequestParam String reason) { |
| 190 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 191 | try { |
| 192 | reportService.submitReport(userId, postId, reason); |
| 193 | return ResponseEntity.ok(ApiResponse.message("举报已提交")); |
| 194 | } catch (IllegalArgumentException e) { |
| 195 | return ResponseEntity.badRequest().body(ApiResponse.error(400, e.getMessage())); |
| 196 | } |
| 197 | } |
| 198 | |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame] | 199 | @DeleteMapping("/{postId}") |
| 200 | public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) { |
| 201 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 202 | Post post = postService.getById(postId); |
| 203 | if (post == null) { |
| 204 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
| 205 | } |
| 206 | if (post.getUserId() != userId) { |
| 207 | return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post.")); |
| 208 | } |
| 209 | postService.removeById(postId); |
| 210 | return ResponseEntity.ok(ApiResponse.message("Post deleted successfully.")); |
| 211 | } |
| 212 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 213 | } |