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; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 11 | import com.example.g8backend.service.IPostService; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 12 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | import org.springframework.http.ResponseEntity; |
| 14 | import org.springframework.security.core.Authentication; |
| 15 | import org.springframework.security.core.context.SecurityContextHolder; |
| 16 | import org.springframework.web.bind.annotation.*; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 17 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 18 | import java.util.List; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 19 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 20 | @RestController |
| 21 | @RequestMapping("/post") |
| 22 | public class PostController { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 23 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 24 | @Autowired |
| 25 | private IPostService postService; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 26 | |
| 27 | @Autowired |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 28 | private PostViewMapper postViewMapper; |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 29 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 30 | @PostMapping("") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 31 | public ResponseEntity<ApiResponse<Void>> createPost(@RequestBody PostCreateDTO postCreateDTO) { |
| 32 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 33 | Post post = postCreateDTO.getPost(); |
| 34 | Long[] tagIds = postCreateDTO.getTagIds(); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 35 | post.setUserId(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 36 | if (tagIds.length > 0) { |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 37 | postService.createPost(post, tagIds); |
| 38 | } else { |
| 39 | postService.createPost(post); |
| 40 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 41 | return ResponseEntity.ok(ApiResponse.message("Post created successfully.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 42 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 43 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 44 | @GetMapping("/{postId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 45 | public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) { |
| 46 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 47 | postService.recordViewHistory(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 48 | Post post = postService.getById(postId); |
| 49 | return ResponseEntity.ok(ApiResponse.success(post)); |
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 | @DeleteMapping("/{postId}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 53 | public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) { |
| 54 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 55 | Post post = postService.getById(postId); |
| 56 | if (post == null) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 57 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 58 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 59 | if (post.getUserId() != userId) { |
| 60 | 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] | 61 | } |
| 62 | postService.removeById(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 63 | return ResponseEntity.ok(ApiResponse.message("Post deleted successfully.")); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 64 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 65 | |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 66 | @GetMapping("/getAll") |
| 67 | public ResponseEntity<ApiResponse<List<Post>>> getAllPosts() { |
| 68 | return ResponseEntity.ok(ApiResponse.success(postService.list())); |
| 69 | } |
| 70 | |
| 71 | @GetMapping("/getByUserId/{userId}") |
| 72 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByUserId(@PathVariable Long userId) { |
| 73 | return ResponseEntity.ok(ApiResponse.success(postService.getPostsByUserId(userId))); |
| 74 | } |
| 75 | |
| 76 | @PutMapping("/{postId}") |
| 77 | public ResponseEntity<ApiResponse<String>> updatePost(@PathVariable Long postId, @RequestBody Post post) { |
| 78 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 79 | Post existingPost = postService.getById(postId); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 80 | if (existingPost == null) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 81 | return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 82 | } |
| 83 | if (existingPost.getUserId() != userId) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 84 | 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] | 85 | } |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 86 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 87 | post.setPostId(postId); |
| 88 | post.setUserId(userId); |
| 89 | postService.updateById(post); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 90 | return ResponseEntity.ok(ApiResponse.message("Post updated successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 91 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 92 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 93 | @GetMapping("/type/{postType}") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 94 | public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 95 | List<Post> posts = postService.getPostsByType(postType); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 96 | return ResponseEntity.ok(ApiResponse.success(posts)); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 97 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 98 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 99 | @PostMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 100 | public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) { |
| 101 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 102 | postService.likePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 103 | return ResponseEntity.ok(ApiResponse.message("Post liked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 104 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 105 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 106 | @DeleteMapping("/{postId}/like") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 107 | public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) { |
| 108 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 109 | postService.unlikePost(userId, postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 110 | return ResponseEntity.ok(ApiResponse.message("Post unliked successfully.")); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 111 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 112 | |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 113 | @GetMapping("/{postId}/likes") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 114 | public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) { |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 115 | Long likeCount = postService.getPostLikeCount(postId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 116 | return ResponseEntity.ok(ApiResponse.success(likeCount)); |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 117 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 118 | |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 119 | @GetMapping("/search") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 120 | public ResponseEntity<ApiResponse<List<Post>>> searchPosts( |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 121 | @RequestParam(required = false) String keyword, |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 122 | @RequestParam(required = false) List<Long> tags, |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 123 | @RequestParam(required = false) String author) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 124 | List<Post> result = postService.searchPosts(keyword, tags, author); |
| 125 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame] | 126 | } |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 127 | |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 128 | @GetMapping("/history") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 129 | public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() { |
| 130 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | ef46ac5 | 2025-04-24 20:56:47 +0800 | [diff] [blame] | 131 | List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 132 | return ResponseEntity.ok(ApiResponse.success(history)); |
夜雨声烦 | f4b8b6f | 2025-04-24 00:58:36 +0800 | [diff] [blame] | 133 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 134 | |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 135 | @GetMapping("/recommended") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 136 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts( |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 137 | @RequestParam(defaultValue = "1") int page, |
| 138 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 139 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 140 | Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 141 | return ResponseEntity.ok(ApiResponse.success(pageResult)); |
夜雨声烦 | 368e356 | 2025-04-24 01:49:46 +0800 | [diff] [blame] | 142 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 143 | |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 144 | @GetMapping("/recommended-by-tags") |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 145 | public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags( |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 146 | @RequestParam(defaultValue = "1") int page, |
| 147 | @RequestParam(defaultValue = "10") int size) { |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 148 | long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 149 | Page<Post> result = postService.getRecommendedByTags(page, size, userId); |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 150 | return ResponseEntity.ok(ApiResponse.success(result)); |
夜雨声烦 | f77d813 | 2025-04-24 19:31:18 +0800 | [diff] [blame] | 151 | } |
夜雨声烦 | f995a44 | 2025-05-13 18:43:29 +0800 | [diff] [blame^] | 152 | } |