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) { |
| 54 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 55 | postService.recordViewHistory(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 56 | Post post = postService.getById(postId); |
| 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 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 60 | @DeleteMapping("/{postId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 61 | public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) { |
| 62 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 63 | Post post = postService.getById(postId); |
| 64 | if (post == null) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 65 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 66 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 67 | if (post.getUserId() != userId) { |
| 68 | return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 69 | } |
| 70 | postService.removeById(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 71 | return ResponseEntity.ok(ApiResponse.message("Post deleted successfully.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 72 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 73 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 74 | @GetMapping("/getAll") |
| 75 | public ResponseEntity<ApiResponse<List<Post>>> getAllPosts() { |
| 76 | return ResponseEntity.ok(ApiResponse.success(postService.list())); |
| 77 | } |
| 78 | |
| 79 | @GetMapping("/getByUserId/{userId}") |
| 80 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByUserId(@PathVariable Long userId) { |
| 81 | return ResponseEntity.ok(ApiResponse.success(postService.getPostsByUserId(userId))); |
| 82 | } |
| 83 | |
| 84 | @PutMapping("/{postId}") |
| 85 | public ResponseEntity<ApiResponse<String>> updatePost(@PathVariable Long postId, @RequestBody Post post) { |
| 86 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 87 | Post existingPost = postService.getById(postId); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 88 | if (existingPost == null) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 89 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 90 | } |
| 91 | if (existingPost.getUserId() != userId) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 92 | 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] | 93 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 94 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 95 | post.setPostId(postId); |
| 96 | post.setUserId(userId); |
| 97 | postService.updateById(post); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 98 | return ResponseEntity.ok(ApiResponse.message("Post updated successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 99 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 100 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 101 | @GetMapping("/type/{postType}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 102 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 103 | List<Post> posts = postService.getPostsByType(postType); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 104 | return ResponseEntity.ok(ApiResponse.success(posts)); |
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 | @PostMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 108 | public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) { |
| 109 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 110 | postService.likePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 111 | return ResponseEntity.ok(ApiResponse.message("Post liked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 112 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 113 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 114 | @DeleteMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 115 | public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) { |
| 116 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 117 | postService.unlikePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 118 | return ResponseEntity.ok(ApiResponse.message("Post unliked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 119 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 120 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 121 | @GetMapping("/{postId}/likes") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 122 | public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 123 | Long likeCount = postService.getPostLikeCount(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 124 | return ResponseEntity.ok(ApiResponse.success(likeCount)); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 125 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 126 | |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 127 | @GetMapping("/search") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 128 | public ResponseEntity<ApiResponse<List<Post>>> searchPosts( |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 129 | @RequestParam(required = false) String keyword, |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 130 | @RequestParam(required = false) List<Long> tags, |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 131 | @RequestParam(required = false) String author) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 132 | List<Post> result = postService.searchPosts(keyword, tags, author); |
| 133 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 134 | } |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 135 | |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 136 | @GetMapping("/history") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 137 | public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() { |
| 138 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 139 | List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 140 | return ResponseEntity.ok(ApiResponse.success(history)); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 141 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 142 | |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 143 | @GetMapping("/recommended") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 144 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 145 | @RequestParam(defaultValue = "1") int page, |
| 146 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 147 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 148 | Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 149 | return ResponseEntity.ok(ApiResponse.success(pageResult)); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 150 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 151 | |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 152 | @GetMapping("/recommended-by-tags") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 153 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags( |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 154 | @RequestParam(defaultValue = "1") int page, |
| 155 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 156 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 157 | Page<Post> result = postService.getRecommendedByTags(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 158 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 159 | } |
夜雨声烦 | 070c05a | 2025-05-13 20:33:50 +0800 | [diff] [blame] | 160 | @Autowired |
| 161 | private IPostRatingService postRatingService; |
| 162 | |
| 163 | @PostMapping("/{postId}/rate") |
| 164 | public ResponseEntity<ApiResponse<String>> ratePost( |
| 165 | @PathVariable Long postId, |
| 166 | @RequestParam Integer rating) { |
| 167 | try { |
| 168 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 169 | |
| 170 | // 调用服务层方法(服务层已内置校验逻辑) |
| 171 | postRatingService.ratePost(userId, postId, rating); |
| 172 | |
| 173 | // 成功时返回空数据 |
| 174 | return ResponseEntity.ok(ApiResponse.success("评分成功")); |
| 175 | |
| 176 | } catch (IllegalArgumentException e) { |
| 177 | // 处理参数校验异常(如评分范围错误) |
| 178 | return ResponseEntity.badRequest() |
| 179 | .body(ApiResponse.error(400, e.getMessage())); |
| 180 | |
| 181 | } catch (RuntimeException e) { |
| 182 | // 处理数据库操作失败等运行时异常 |
| 183 | return ResponseEntity.internalServerError() |
| 184 | .body(ApiResponse.error(500, e.getMessage())); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | @GetMapping("/{postId}/average-rating") |
| 189 | public ResponseEntity<ApiResponse<Double>> getAverageRating(@PathVariable Long postId) { |
| 190 | Double avg = postRatingService.getAverageRating(postId); |
| 191 | return ResponseEntity.ok(ApiResponse.success(avg)); |
| 192 | } |
| 193 | |
| 194 | @GetMapping("/{postId}/rating-users/count") |
| 195 | public ResponseEntity<ApiResponse<Long>> getRatingUserCount(@PathVariable Long postId) { |
| 196 | Long count = postRatingService.getRatingUserCount(postId); |
| 197 | return ResponseEntity.ok(ApiResponse.success(count)); |
| 198 | } |
夜雨声烦 | 0a3df4a | 2025-05-13 21:26:13 +0800 | [diff] [blame] | 199 | @PostMapping("/{postId}/report") |
| 200 | public ResponseEntity<ApiResponse<String>> reportPost( |
| 201 | @PathVariable Long postId, |
| 202 | @RequestParam String reason) { |
| 203 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 204 | try { |
| 205 | reportService.submitReport(userId, postId, reason); |
| 206 | return ResponseEntity.ok(ApiResponse.message("举报已提交")); |
| 207 | } catch (IllegalArgumentException e) { |
| 208 | return ResponseEntity.badRequest().body(ApiResponse.error(400, e.getMessage())); |
| 209 | } |
| 210 | } |
| 211 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame] | 212 | } |