blob: e5e4eab7092c11e341caec5d6729bbcd2fcda66e [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.*;
夜雨声烦f995a442025-05-13 18:43:29 +080019
wuchimedese5722e32025-04-13 17:38:50 +080020import java.util.List;
夜雨声烦f995a442025-05-13 18:43:29 +080021
wuchimedese5722e32025-04-13 17:38:50 +080022@RestController
23@RequestMapping("/post")
夜雨声烦070c05a2025-05-13 20:33:50 +080024@Validated
wuchimedese5722e32025-04-13 17:38:50 +080025public class PostController {
夜雨声烦f995a442025-05-13 18:43:29 +080026
wuchimedese5722e32025-04-13 17:38:50 +080027 @Autowired
28 private IPostService postService;
夜雨声烦f995a442025-05-13 18:43:29 +080029
30 @Autowired
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080031 private PostViewMapper postViewMapper;
夜雨声烦f995a442025-05-13 18:43:29 +080032
wuchimedese5722e32025-04-13 17:38:50 +080033 @PostMapping("")
夜雨声烦f995a442025-05-13 18:43:29 +080034 public ResponseEntity<ApiResponse<Void>> createPost(@RequestBody PostCreateDTO postCreateDTO) {
35 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080036 Post post = postCreateDTO.getPost();
37 Long[] tagIds = postCreateDTO.getTagIds();
wuchimedese5722e32025-04-13 17:38:50 +080038 post.setUserId(userId);
夜雨声烦f995a442025-05-13 18:43:29 +080039 if (tagIds.length > 0) {
wuchimedes5a842b22025-04-21 22:01:39 +080040 postService.createPost(post, tagIds);
41 } else {
42 postService.createPost(post);
43 }
夜雨声烦f995a442025-05-13 18:43:29 +080044 return ResponseEntity.ok(ApiResponse.message("Post created successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080045 }
夜雨声烦f995a442025-05-13 18:43:29 +080046
wuchimedese5722e32025-04-13 17:38:50 +080047 @GetMapping("/{postId}")
夜雨声烦f995a442025-05-13 18:43:29 +080048 public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) {
49 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080050 postService.recordViewHistory(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +080051 Post post = postService.getById(postId);
52 return ResponseEntity.ok(ApiResponse.success(post));
wuchimedese5722e32025-04-13 17:38:50 +080053 }
夜雨声烦f995a442025-05-13 18:43:29 +080054
wuchimedese5722e32025-04-13 17:38:50 +080055 @DeleteMapping("/{postId}")
夜雨声烦f995a442025-05-13 18:43:29 +080056 public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
57 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedese5722e32025-04-13 17:38:50 +080058 Post post = postService.getById(postId);
59 if (post == null) {
夜雨声烦f995a442025-05-13 18:43:29 +080060 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
wuchimedese5722e32025-04-13 17:38:50 +080061 }
夜雨声烦f995a442025-05-13 18:43:29 +080062 if (post.getUserId() != userId) {
63 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
wuchimedese5722e32025-04-13 17:38:50 +080064 }
65 postService.removeById(postId);
夜雨声烦f995a442025-05-13 18:43:29 +080066 return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080067 }
夜雨声烦f77d8132025-04-24 19:31:18 +080068
夜雨声烦f995a442025-05-13 18:43:29 +080069 @GetMapping("/getAll")
70 public ResponseEntity<ApiResponse<List<Post>>> getAllPosts() {
71 return ResponseEntity.ok(ApiResponse.success(postService.list()));
72 }
73
74 @GetMapping("/getByUserId/{userId}")
75 public ResponseEntity<ApiResponse<List<Post>>> getPostsByUserId(@PathVariable Long userId) {
76 return ResponseEntity.ok(ApiResponse.success(postService.getPostsByUserId(userId)));
77 }
78
79 @PutMapping("/{postId}")
80 public ResponseEntity<ApiResponse<String>> updatePost(@PathVariable Long postId, @RequestBody Post post) {
81 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
82 Post existingPost = postService.getById(postId);
223010711f457dc2025-04-15 17:35:55 +080083 if (existingPost == null) {
夜雨声烦f995a442025-05-13 18:43:29 +080084 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
223010711f457dc2025-04-15 17:35:55 +080085 }
86 if (existingPost.getUserId() != userId) {
夜雨声烦f995a442025-05-13 18:43:29 +080087 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to update this post."));
223010711f457dc2025-04-15 17:35:55 +080088 }
夜雨声烦f77d8132025-04-24 19:31:18 +080089
223010711f457dc2025-04-15 17:35:55 +080090 post.setPostId(postId);
91 post.setUserId(userId);
92 postService.updateById(post);
夜雨声烦f995a442025-05-13 18:43:29 +080093 return ResponseEntity.ok(ApiResponse.message("Post updated successfully."));
223010711f457dc2025-04-15 17:35:55 +080094 }
夜雨声烦f995a442025-05-13 18:43:29 +080095
223010711f457dc2025-04-15 17:35:55 +080096 @GetMapping("/type/{postType}")
夜雨声烦f995a442025-05-13 18:43:29 +080097 public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) {
223010711f457dc2025-04-15 17:35:55 +080098 List<Post> posts = postService.getPostsByType(postType);
夜雨声烦f995a442025-05-13 18:43:29 +080099 return ResponseEntity.ok(ApiResponse.success(posts));
223010711f457dc2025-04-15 17:35:55 +0800100 }
夜雨声烦f995a442025-05-13 18:43:29 +0800101
223010711f457dc2025-04-15 17:35:55 +0800102 @PostMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +0800103 public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) {
104 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800105 postService.likePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800106 return ResponseEntity.ok(ApiResponse.message("Post liked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800107 }
夜雨声烦f995a442025-05-13 18:43:29 +0800108
223010711f457dc2025-04-15 17:35:55 +0800109 @DeleteMapping("/{postId}/like")
夜雨声烦f995a442025-05-13 18:43:29 +0800110 public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) {
111 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800112 postService.unlikePost(userId, postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800113 return ResponseEntity.ok(ApiResponse.message("Post unliked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800114 }
夜雨声烦f995a442025-05-13 18:43:29 +0800115
223010711f457dc2025-04-15 17:35:55 +0800116 @GetMapping("/{postId}/likes")
夜雨声烦f995a442025-05-13 18:43:29 +0800117 public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) {
223010711f457dc2025-04-15 17:35:55 +0800118 Long likeCount = postService.getPostLikeCount(postId);
夜雨声烦f995a442025-05-13 18:43:29 +0800119 return ResponseEntity.ok(ApiResponse.success(likeCount));
223010711f457dc2025-04-15 17:35:55 +0800120 }
夜雨声烦f995a442025-05-13 18:43:29 +0800121
夜雨声烦4527a722025-04-23 17:04:25 +0800122 @GetMapping("/search")
夜雨声烦f995a442025-05-13 18:43:29 +0800123 public ResponseEntity<ApiResponse<List<Post>>> searchPosts(
夜雨声烦4527a722025-04-23 17:04:25 +0800124 @RequestParam(required = false) String keyword,
夜雨声烦f995a442025-05-13 18:43:29 +0800125 @RequestParam(required = false) List<Long> tags,
夜雨声烦4527a722025-04-23 17:04:25 +0800126 @RequestParam(required = false) String author) {
夜雨声烦f995a442025-05-13 18:43:29 +0800127 List<Post> result = postService.searchPosts(keyword, tags, author);
128 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦4527a722025-04-23 17:04:25 +0800129 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800130
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800131 @GetMapping("/history")
夜雨声烦f995a442025-05-13 18:43:29 +0800132 public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() {
133 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800134 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800135 return ResponseEntity.ok(ApiResponse.success(history));
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800136 }
夜雨声烦f995a442025-05-13 18:43:29 +0800137
夜雨声烦368e3562025-04-24 01:49:46 +0800138 @GetMapping("/recommended")
夜雨声烦f995a442025-05-13 18:43:29 +0800139 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts(
夜雨声烦368e3562025-04-24 01:49:46 +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();
夜雨声烦368e3562025-04-24 01:49:46 +0800143 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800144 return ResponseEntity.ok(ApiResponse.success(pageResult));
夜雨声烦368e3562025-04-24 01:49:46 +0800145 }
夜雨声烦f995a442025-05-13 18:43:29 +0800146
夜雨声烦f77d8132025-04-24 19:31:18 +0800147 @GetMapping("/recommended-by-tags")
夜雨声烦f995a442025-05-13 18:43:29 +0800148 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags(
夜雨声烦f77d8132025-04-24 19:31:18 +0800149 @RequestParam(defaultValue = "1") int page,
150 @RequestParam(defaultValue = "10") int size) {
夜雨声烦f995a442025-05-13 18:43:29 +0800151 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f77d8132025-04-24 19:31:18 +0800152 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
夜雨声烦f995a442025-05-13 18:43:29 +0800153 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦f77d8132025-04-24 19:31:18 +0800154 }
夜雨声烦070c05a2025-05-13 20:33:50 +0800155 @Autowired
156 private IPostRatingService postRatingService;
157
158 @PostMapping("/{postId}/rate")
159 public ResponseEntity<ApiResponse<String>> ratePost(
160 @PathVariable Long postId,
161 @RequestParam Integer rating) {
162 try {
163 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
164
165 // 调用服务层方法(服务层已内置校验逻辑)
166 postRatingService.ratePost(userId, postId, rating);
167
168 // 成功时返回空数据
169 return ResponseEntity.ok(ApiResponse.success("评分成功"));
170
171 } catch (IllegalArgumentException e) {
172 // 处理参数校验异常(如评分范围错误)
173 return ResponseEntity.badRequest()
174 .body(ApiResponse.error(400, e.getMessage()));
175
176 } catch (RuntimeException e) {
177 // 处理数据库操作失败等运行时异常
178 return ResponseEntity.internalServerError()
179 .body(ApiResponse.error(500, e.getMessage()));
180 }
181 }
182
183 @GetMapping("/{postId}/average-rating")
184 public ResponseEntity<ApiResponse<Double>> getAverageRating(@PathVariable Long postId) {
185 Double avg = postRatingService.getAverageRating(postId);
186 return ResponseEntity.ok(ApiResponse.success(avg));
187 }
188
189 @GetMapping("/{postId}/rating-users/count")
190 public ResponseEntity<ApiResponse<Long>> getRatingUserCount(@PathVariable Long postId) {
191 Long count = postRatingService.getRatingUserCount(postId);
192 return ResponseEntity.ok(ApiResponse.success(count));
193 }
夜雨声烦f995a442025-05-13 18:43:29 +0800194}