blob: 1f7e886439d9625446c4a2553b7e46c3241dc1f1 [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) {
夜雨声烦39b8cfe2025-06-08 14:10:08 +0800118 System.out.println(keyword + tags + author);
夜雨声烦f995a442025-05-13 18:43:29 +0800119 List<Post> result = postService.searchPosts(keyword, tags, author);
120 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦4527a722025-04-23 17:04:25 +0800121 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800122
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800123 @GetMapping("/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800124 public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() {
125 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800126 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800127 return ResponseEntity.ok(ApiResponse.success(history));
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800128 }
夜雨声烦f995a442025-05-13 18:43:29 +0800129
夜雨声烦368e3562025-04-24 01:49:46 +0800130 @GetMapping("/recommended")
夜雨声烦f995a442025-05-13 18:43:29 +0800131 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts(
夜雨声烦368e3562025-04-24 01:49:46 +0800132 @RequestParam(defaultValue = "1") int page,
133 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800134 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800135 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800136 return ResponseEntity.ok(ApiResponse.success(pageResult));
夜雨声烦368e3562025-04-24 01:49:46 +0800137 }
夜雨声烦f995a442025-05-13 18:43:29 +0800138
夜雨声烦f77d8132025-04-24 19:31:18 +0800139 @GetMapping("/recommended-by-tags")
夜雨声烦f995a442025-05-13 18:43:29 +0800140 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags(
夜雨声烦f77d8132025-04-24 19:31:18 +0800141 @RequestParam(defaultValue = "1") int page,
142 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800143 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f77d8132025-04-24 19:31:18 +0800144 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800145 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦f77d8132025-04-24 19:31:18 +0800146 }
夜雨声烦070c05a2025-05-13 20:33:50 +0800147 @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 }
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800186 @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
夜雨声烦7affa472025-05-20 19:27:16 +0800199 @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
夜雨声烦f995a442025-05-13 18:43:29 +0800213}