blob: 41be09aa017660e182ce98fed03f0249c4244ccd [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.controller;
夜雨声烦f995a442025-05-13 18:43:29 +08002
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08003import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
夜雨声烦368e3562025-04-24 01:49:46 +08004import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
夜雨声烦f995a442025-05-13 18:43:29 +08005import com.example.g8backend.dto.ApiResponse;
wuchimedes5a842b22025-04-21 22:01:39 +08006import com.example.g8backend.dto.PostCreateDTO;
夜雨声烦ef46ac52025-04-24 20:56:47 +08007import com.example.g8backend.dto.PostHistoryDTO;
夜雨声烦f995a442025-05-13 18:43:29 +08008import com.example.g8backend.entity.Post;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08009import com.example.g8backend.entity.PostView;
10import com.example.g8backend.mapper.PostViewMapper;
夜雨声烦070c05a2025-05-13 20:33:50 +080011import com.example.g8backend.service.IPostRatingService;
夜雨声烦f995a442025-05-13 18:43:29 +080012import com.example.g8backend.service.IPostService;
wuchimedese5722e32025-04-13 17:38:50 +080013import org.springframework.beans.factory.annotation.Autowired;
14import org.springframework.http.ResponseEntity;
15import org.springframework.security.core.Authentication;
16import org.springframework.security.core.context.SecurityContextHolder;
夜雨声烦070c05a2025-05-13 20:33:50 +080017import org.springframework.validation.annotation.Validated;
wuchimedese5722e32025-04-13 17:38:50 +080018import org.springframework.web.bind.annotation.*;
夜雨声烦0a3df4a2025-05-13 21:26:13 +080019import com.example.g8backend.entity.Report;
20import com.example.g8backend.service.IReportService;
夜雨声烦f995a442025-05-13 18:43:29 +080021
wuchimedese5722e32025-04-13 17:38:50 +080022import java.util.List;
夜雨声烦f995a442025-05-13 18:43:29 +080023
夜雨声烦0a3df4a2025-05-13 21:26:13 +080024
wuchimedese5722e32025-04-13 17:38:50 +080025@RestController
26@RequestMapping("/post")
夜雨声烦070c05a2025-05-13 20:33:50 +080027@Validated
wuchimedese5722e32025-04-13 17:38:50 +080028public class PostController {
夜雨声烦f995a442025-05-13 18:43:29 +080029
wuchimedese5722e32025-04-13 17:38:50 +080030 @Autowired
31 private IPostService postService;
夜雨声烦f995a442025-05-13 18:43:29 +080032
33 @Autowired
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080034 private PostViewMapper postViewMapper;
夜雨声烦0a3df4a2025-05-13 21:26:13 +080035 @Autowired
36 private IReportService reportService;
夜雨声烦f995a442025-05-13 18:43:29 +080037
wuchimedese5722e32025-04-13 17:38:50 +080038 @PostMapping("")
夜雨声烦f995a442025-05-13 18:43:29 +080039 public ResponseEntity<ApiResponse<Void>> createPost(@RequestBody PostCreateDTO postCreateDTO) {
40 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080041 Post post = postCreateDTO.getPost();
42 Long[] tagIds = postCreateDTO.getTagIds();
wuchimedese5722e32025-04-13 17:38:50 +080043 post.setUserId(userId);
夜雨声烦f995a442025-05-13 18:43:29 +080044 if (tagIds.length > 0) {
wuchimedes5a842b22025-04-21 22:01:39 +080045 postService.createPost(post, tagIds);
46 } else {
47 postService.createPost(post);
48 }
夜雨声烦f995a442025-05-13 18:43:29 +080049 return ResponseEntity.ok(ApiResponse.message("Post created successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080050 }
夜雨声烦f995a442025-05-13 18:43:29 +080051
wuchimedese5722e32025-04-13 17:38:50 +080052 @GetMapping("/{postId}")
夜雨声烦f995a442025-05-13 18:43:29 +080053 public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) {
夜雨声烦7affa472025-05-20 19:27:16 +080054 Post post = postService.getById(postId);
夜雨声烦f995a442025-05-13 18:43:29 +080055 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080056 postService.recordViewHistory(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +080057 return ResponseEntity.ok(ApiResponse.success(post));
wuchimedese5722e32025-04-13 17:38:50 +080058 }
夜雨声烦f995a442025-05-13 18:43:29 +080059
夜雨声烦f995a442025-05-13 18:43:29 +080060 @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);
223010711f457dc2025-04-15 17:35:55 +080074 if (existingPost == null) {
夜雨声烦f995a442025-05-13 18:43:29 +080075 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
223010711f457dc2025-04-15 17:35:55 +080076 }
77 if (existingPost.getUserId() != userId) {
夜雨声烦f995a442025-05-13 18:43:29 +080078 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to update this post."));
223010711f457dc2025-04-15 17:35:55 +080079 }
夜雨声烦f77d8132025-04-24 19:31:18 +080080
223010711f457dc2025-04-15 17:35:55 +080081 post.setPostId(postId);
82 post.setUserId(userId);
83 postService.updateById(post);
夜雨声烦f995a442025-05-13 18:43:29 +080084 return ResponseEntity.ok(ApiResponse.message("Post updated successfully."));
223010711f457dc2025-04-15 17:35:55 +080085 }
夜雨声烦f995a442025-05-13 18:43:29 +080086
223010711f457dc2025-04-15 17:35:55 +080087 @GetMapping("/type/{postType}")
夜雨声烦f995a442025-05-13 18:43:29 +080088 public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) {
223010711f457dc2025-04-15 17:35:55 +080089 List<Post> posts = postService.getPostsByType(postType);
夜雨声烦f995a442025-05-13 18:43:29 +080090 return ResponseEntity.ok(ApiResponse.success(posts));
223010711f457dc2025-04-15 17:35:55 +080091 }
夜雨声烦f995a442025-05-13 18:43:29 +080092
223010711f457dc2025-04-15 17:35:55 +080093 @PostMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +080094 public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) {
95 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +080096 postService.likePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +080097 return ResponseEntity.ok(ApiResponse.message("Post liked successfully."));
223010711f457dc2025-04-15 17:35:55 +080098 }
夜雨声烦f995a442025-05-13 18:43:29 +080099
223010711f457dc2025-04-15 17:35:55 +0800100 @DeleteMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +0800101 public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) {
102 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800103 postService.unlikePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800104 return ResponseEntity.ok(ApiResponse.message("Post unliked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800105 }
夜雨声烦f995a442025-05-13 18:43:29 +0800106
223010711f457dc2025-04-15 17:35:55 +0800107 @GetMapping("/{postId}/likes")
夜雨声烦f995a442025-05-13 18:43:29 +0800108 public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) {
223010711f457dc2025-04-15 17:35:55 +0800109 Long likeCount = postService.getPostLikeCount(postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800110 return ResponseEntity.ok(ApiResponse.success(likeCount));
223010711f457dc2025-04-15 17:35:55 +0800111 }
夜雨声烦f995a442025-05-13 18:43:29 +0800112
夜雨声烦4527a722025-04-23 17:04:25 +0800113 @GetMapping("/search")
夜雨声烦f995a442025-05-13 18:43:29 +0800114 public ResponseEntity<ApiResponse<List<Post>>> searchPosts(
夜雨声烦4527a722025-04-23 17:04:25 +0800115 @RequestParam(required = false) String keyword,
夜雨声烦f995a442025-05-13 18:43:29 +0800116 @RequestParam(required = false) List<Long> tags,
夜雨声烦4527a722025-04-23 17:04:25 +0800117 @RequestParam(required = false) String author) {
夜雨声烦f995a442025-05-13 18:43:29 +0800118 List<Post> result = postService.searchPosts(keyword, tags, author);
119 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦4527a722025-04-23 17:04:25 +0800120 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800121
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800122 @GetMapping("/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800123 public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() {
124 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800125 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800126 return ResponseEntity.ok(ApiResponse.success(history));
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800127 }
夜雨声烦f995a442025-05-13 18:43:29 +0800128
夜雨声烦368e3562025-04-24 01:49:46 +0800129 @GetMapping("/recommended")
夜雨声烦f995a442025-05-13 18:43:29 +0800130 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts(
夜雨声烦368e3562025-04-24 01:49:46 +0800131 @RequestParam(defaultValue = "1") int page,
132 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800133 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800134 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800135 return ResponseEntity.ok(ApiResponse.success(pageResult));
夜雨声烦368e3562025-04-24 01:49:46 +0800136 }
夜雨声烦f995a442025-05-13 18:43:29 +0800137
夜雨声烦f77d8132025-04-24 19:31:18 +0800138 @GetMapping("/recommended-by-tags")
夜雨声烦f995a442025-05-13 18:43:29 +0800139 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags(
夜雨声烦f77d8132025-04-24 19:31:18 +0800140 @RequestParam(defaultValue = "1") int page,
141 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800142 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f77d8132025-04-24 19:31:18 +0800143 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800144 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦f77d8132025-04-24 19:31:18 +0800145 }
夜雨声烦070c05a2025-05-13 20:33:50 +0800146 @Autowired
147 private IPostRatingService postRatingService;
148
149 @PostMapping("/{postId}/rate")
150 public ResponseEntity<ApiResponse<String>> ratePost(
151 @PathVariable Long postId,
152 @RequestParam Integer rating) {
153 try {
154 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
155
156 // 调用服务层方法(服务层已内置校验逻辑)
157 postRatingService.ratePost(userId, postId, rating);
158
159 // 成功时返回空数据
160 return ResponseEntity.ok(ApiResponse.success("评分成功"));
161
162 } catch (IllegalArgumentException e) {
163 // 处理参数校验异常(如评分范围错误)
164 return ResponseEntity.badRequest()
165 .body(ApiResponse.error(400, e.getMessage()));
166
167 } catch (RuntimeException e) {
168 // 处理数据库操作失败等运行时异常
169 return ResponseEntity.internalServerError()
170 .body(ApiResponse.error(500, e.getMessage()));
171 }
172 }
173
174 @GetMapping("/{postId}/average-rating")
175 public ResponseEntity<ApiResponse<Double>> getAverageRating(@PathVariable Long postId) {
176 Double avg = postRatingService.getAverageRating(postId);
177 return ResponseEntity.ok(ApiResponse.success(avg));
178 }
179
180 @GetMapping("/{postId}/rating-users/count")
181 public ResponseEntity<ApiResponse<Long>> getRatingUserCount(@PathVariable Long postId) {
182 Long count = postRatingService.getRatingUserCount(postId);
183 return ResponseEntity.ok(ApiResponse.success(count));
184 }
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800185 @PostMapping("/{postId}/report")
186 public ResponseEntity<ApiResponse<String>> reportPost(
187 @PathVariable Long postId,
188 @RequestParam String reason) {
189 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
190 try {
191 reportService.submitReport(userId, postId, reason);
192 return ResponseEntity.ok(ApiResponse.message("举报已提交"));
193 } catch (IllegalArgumentException e) {
194 return ResponseEntity.badRequest().body(ApiResponse.error(400, e.getMessage()));
195 }
196 }
197
夜雨声烦7affa472025-05-20 19:27:16 +0800198 @DeleteMapping("/{postId}")
199 public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
200 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
201 Post post = postService.getById(postId);
202 if (post == null) {
203 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
204 }
205 if (post.getUserId() != userId) {
206 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
207 }
208 postService.removeById(postId);
209 return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
210 }
211
夜雨声烦f995a442025-05-13 18:43:29 +0800212}