blob: 6800666a87ef88cfb3f114785e7fce08ba042443 [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) {
54 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080055 postService.recordViewHistory(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +080056 Post post = postService.getById(postId);
57 return ResponseEntity.ok(ApiResponse.success(post));
wuchimedese5722e32025-04-13 17:38:50 +080058 }
夜雨声烦f995a442025-05-13 18:43:29 +080059
wuchimedese5722e32025-04-13 17:38:50 +080060 @DeleteMapping("/{postId}")
夜雨声烦f995a442025-05-13 18:43:29 +080061 public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
62 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedese5722e32025-04-13 17:38:50 +080063 Post post = postService.getById(postId);
64 if (post == null) {
夜雨声烦f995a442025-05-13 18:43:29 +080065 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
wuchimedese5722e32025-04-13 17:38:50 +080066 }
夜雨声烦f995a442025-05-13 18:43:29 +080067 if (post.getUserId() != userId) {
68 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
wuchimedese5722e32025-04-13 17:38:50 +080069 }
70 postService.removeById(postId);
夜雨声烦f995a442025-05-13 18:43:29 +080071 return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080072 }
夜雨声烦f77d8132025-04-24 19:31:18 +080073
夜雨声烦f995a442025-05-13 18:43:29 +080074 @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);
223010711f457dc2025-04-15 17:35:55 +080088 if (existingPost == null) {
夜雨声烦f995a442025-05-13 18:43:29 +080089 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
223010711f457dc2025-04-15 17:35:55 +080090 }
91 if (existingPost.getUserId() != userId) {
夜雨声烦f995a442025-05-13 18:43:29 +080092 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to update this post."));
223010711f457dc2025-04-15 17:35:55 +080093 }
夜雨声烦f77d8132025-04-24 19:31:18 +080094
223010711f457dc2025-04-15 17:35:55 +080095 post.setPostId(postId);
96 post.setUserId(userId);
97 postService.updateById(post);
夜雨声烦f995a442025-05-13 18:43:29 +080098 return ResponseEntity.ok(ApiResponse.message("Post updated successfully."));
223010711f457dc2025-04-15 17:35:55 +080099 }
夜雨声烦f995a442025-05-13 18:43:29 +0800100
223010711f457dc2025-04-15 17:35:55 +0800101 @GetMapping("/type/{postType}")
夜雨声烦f995a442025-05-13 18:43:29 +0800102 public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) {
223010711f457dc2025-04-15 17:35:55 +0800103 List<Post> posts = postService.getPostsByType(postType);
夜雨声烦f995a442025-05-13 18:43:29 +0800104 return ResponseEntity.ok(ApiResponse.success(posts));
223010711f457dc2025-04-15 17:35:55 +0800105 }
夜雨声烦f995a442025-05-13 18:43:29 +0800106
223010711f457dc2025-04-15 17:35:55 +0800107 @PostMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +0800108 public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) {
109 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800110 postService.likePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800111 return ResponseEntity.ok(ApiResponse.message("Post liked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800112 }
夜雨声烦f995a442025-05-13 18:43:29 +0800113
223010711f457dc2025-04-15 17:35:55 +0800114 @DeleteMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +0800115 public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) {
116 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800117 postService.unlikePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800118 return ResponseEntity.ok(ApiResponse.message("Post unliked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800119 }
夜雨声烦f995a442025-05-13 18:43:29 +0800120
223010711f457dc2025-04-15 17:35:55 +0800121 @GetMapping("/{postId}/likes")
夜雨声烦f995a442025-05-13 18:43:29 +0800122 public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) {
223010711f457dc2025-04-15 17:35:55 +0800123 Long likeCount = postService.getPostLikeCount(postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800124 return ResponseEntity.ok(ApiResponse.success(likeCount));
223010711f457dc2025-04-15 17:35:55 +0800125 }
夜雨声烦f995a442025-05-13 18:43:29 +0800126
夜雨声烦4527a722025-04-23 17:04:25 +0800127 @GetMapping("/search")
夜雨声烦f995a442025-05-13 18:43:29 +0800128 public ResponseEntity<ApiResponse<List<Post>>> searchPosts(
夜雨声烦4527a722025-04-23 17:04:25 +0800129 @RequestParam(required = false) String keyword,
夜雨声烦f995a442025-05-13 18:43:29 +0800130 @RequestParam(required = false) List<Long> tags,
夜雨声烦4527a722025-04-23 17:04:25 +0800131 @RequestParam(required = false) String author) {
夜雨声烦f995a442025-05-13 18:43:29 +0800132 List<Post> result = postService.searchPosts(keyword, tags, author);
133 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦4527a722025-04-23 17:04:25 +0800134 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800135
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800136 @GetMapping("/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800137 public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() {
138 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800139 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800140 return ResponseEntity.ok(ApiResponse.success(history));
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800141 }
夜雨声烦f995a442025-05-13 18:43:29 +0800142
夜雨声烦368e3562025-04-24 01:49:46 +0800143 @GetMapping("/recommended")
夜雨声烦f995a442025-05-13 18:43:29 +0800144 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts(
夜雨声烦368e3562025-04-24 01:49:46 +0800145 @RequestParam(defaultValue = "1") int page,
146 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800147 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800148 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800149 return ResponseEntity.ok(ApiResponse.success(pageResult));
夜雨声烦368e3562025-04-24 01:49:46 +0800150 }
夜雨声烦f995a442025-05-13 18:43:29 +0800151
夜雨声烦f77d8132025-04-24 19:31:18 +0800152 @GetMapping("/recommended-by-tags")
夜雨声烦f995a442025-05-13 18:43:29 +0800153 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags(
夜雨声烦f77d8132025-04-24 19:31:18 +0800154 @RequestParam(defaultValue = "1") int page,
155 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800156 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f77d8132025-04-24 19:31:18 +0800157 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800158 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦f77d8132025-04-24 19:31:18 +0800159 }
夜雨声烦070c05a2025-05-13 20:33:50 +0800160 @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 }
夜雨声烦0a3df4a2025-05-13 21:26:13 +0800199 @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
夜雨声烦f995a442025-05-13 18:43:29 +0800212}