blob: 9ac733fe1e6ad7530cca8c764cfcd8f8e0eeaf66 [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.controller;
夜雨声烦e73ff922025-05-13 18:49:03 +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;
夜雨声烦e73ff922025-05-13 18:49:03 +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;
夜雨声烦e73ff922025-05-13 18:49:03 +08008import com.example.g8backend.entity.Post;
夜雨声烦f4b8b6f2025-04-24 00:58:36 +08009import com.example.g8backend.entity.PostView;
10import com.example.g8backend.mapper.PostViewMapper;
夜雨声烦e73ff922025-05-13 18:49:03 +080011import com.example.g8backend.service.IPostService;
wuchimedese5722e32025-04-13 17:38:50 +080012import org.springframework.beans.factory.annotation.Autowired;
13import org.springframework.http.ResponseEntity;
14import org.springframework.security.core.Authentication;
15import org.springframework.security.core.context.SecurityContextHolder;
16import org.springframework.web.bind.annotation.*;
夜雨声烦e73ff922025-05-13 18:49:03 +080017
wuchimedese5722e32025-04-13 17:38:50 +080018import java.util.List;
夜雨声烦e73ff922025-05-13 18:49:03 +080019
wuchimedese5722e32025-04-13 17:38:50 +080020@RestController
21@RequestMapping("/post")
22public class PostController {
夜雨声烦e73ff922025-05-13 18:49:03 +080023
wuchimedese5722e32025-04-13 17:38:50 +080024 @Autowired
25 private IPostService postService;
夜雨声烦e73ff922025-05-13 18:49:03 +080026
27 @Autowired
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080028 private PostViewMapper postViewMapper;
夜雨声烦e73ff922025-05-13 18:49:03 +080029
wuchimedese5722e32025-04-13 17:38:50 +080030 @PostMapping("")
夜雨声烦e73ff922025-05-13 18:49:03 +080031 public ResponseEntity<ApiResponse<Void>> createPost(@RequestBody PostCreateDTO postCreateDTO) {
32 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080033 Post post = postCreateDTO.getPost();
34 Long[] tagIds = postCreateDTO.getTagIds();
wuchimedese5722e32025-04-13 17:38:50 +080035 post.setUserId(userId);
夜雨声烦e73ff922025-05-13 18:49:03 +080036 if (tagIds.length > 0) {
wuchimedes5a842b22025-04-21 22:01:39 +080037 postService.createPost(post, tagIds);
38 } else {
39 postService.createPost(post);
40 }
夜雨声烦e73ff922025-05-13 18:49:03 +080041 return ResponseEntity.ok(ApiResponse.message("Post created successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080042 }
夜雨声烦e73ff922025-05-13 18:49:03 +080043
wuchimedese5722e32025-04-13 17:38:50 +080044 @GetMapping("/{postId}")
夜雨声烦e73ff922025-05-13 18:49:03 +080045 public ResponseEntity<ApiResponse<Post>> getPost(@PathVariable Long postId) {
46 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f4b8b6f2025-04-24 00:58:36 +080047 postService.recordViewHistory(userId, postId);
夜雨声烦e73ff922025-05-13 18:49:03 +080048 Post post = postService.getById(postId);
49 return ResponseEntity.ok(ApiResponse.success(post));
wuchimedese5722e32025-04-13 17:38:50 +080050 }
夜雨声烦e73ff922025-05-13 18:49:03 +080051
wuchimedese5722e32025-04-13 17:38:50 +080052 @DeleteMapping("/{postId}")
夜雨声烦e73ff922025-05-13 18:49:03 +080053 public ResponseEntity<ApiResponse<String>> deletePost(@PathVariable Long postId) {
54 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
wuchimedese5722e32025-04-13 17:38:50 +080055 Post post = postService.getById(postId);
56 if (post == null) {
夜雨声烦e73ff922025-05-13 18:49:03 +080057 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
wuchimedese5722e32025-04-13 17:38:50 +080058 }
夜雨声烦e73ff922025-05-13 18:49:03 +080059 if (post.getUserId() != userId) {
60 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to delete this post."));
wuchimedese5722e32025-04-13 17:38:50 +080061 }
62 postService.removeById(postId);
夜雨声烦e73ff922025-05-13 18:49:03 +080063 return ResponseEntity.ok(ApiResponse.message("Post deleted successfully."));
wuchimedese5722e32025-04-13 17:38:50 +080064 }
夜雨声烦f77d8132025-04-24 19:31:18 +080065
夜雨声烦e73ff922025-05-13 18:49:03 +080066 @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);
223010711f457dc2025-04-15 17:35:55 +080080 if (existingPost == null) {
夜雨声烦e73ff922025-05-13 18:49:03 +080081 return ResponseEntity.status(404).body(ApiResponse.error(404, "Post not found."));
223010711f457dc2025-04-15 17:35:55 +080082 }
83 if (existingPost.getUserId() != userId) {
夜雨声烦e73ff922025-05-13 18:49:03 +080084 return ResponseEntity.status(403).body(ApiResponse.error(403, "You are not authorized to update this post."));
223010711f457dc2025-04-15 17:35:55 +080085 }
夜雨声烦f77d8132025-04-24 19:31:18 +080086
223010711f457dc2025-04-15 17:35:55 +080087 post.setPostId(postId);
88 post.setUserId(userId);
89 postService.updateById(post);
夜雨声烦e73ff922025-05-13 18:49:03 +080090 return ResponseEntity.ok(ApiResponse.message("Post updated successfully."));
223010711f457dc2025-04-15 17:35:55 +080091 }
夜雨声烦e73ff922025-05-13 18:49:03 +080092
223010711f457dc2025-04-15 17:35:55 +080093 @GetMapping("/type/{postType}")
夜雨声烦e73ff922025-05-13 18:49:03 +080094 public ResponseEntity<ApiResponse<List<Post>>> getPostsByType(@PathVariable String postType) {
223010711f457dc2025-04-15 17:35:55 +080095 List<Post> posts = postService.getPostsByType(postType);
夜雨声烦e73ff922025-05-13 18:49:03 +080096 return ResponseEntity.ok(ApiResponse.success(posts));
223010711f457dc2025-04-15 17:35:55 +080097 }
夜雨声烦e73ff922025-05-13 18:49:03 +080098
223010711f457dc2025-04-15 17:35:55 +080099 @PostMapping("/{postId}/like")
夜雨声烦e73ff922025-05-13 18:49:03 +0800100 public ResponseEntity<ApiResponse<String>> likePost(@PathVariable Long postId) {
101 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800102 postService.likePost(userId, postId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800103 return ResponseEntity.ok(ApiResponse.message("Post liked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800104 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800105
223010711f457dc2025-04-15 17:35:55 +0800106 @DeleteMapping("/{postId}/like")
夜雨声烦e73ff922025-05-13 18:49:03 +0800107 public ResponseEntity<ApiResponse<String>> unlikePost(@PathVariable Long postId) {
108 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
223010711f457dc2025-04-15 17:35:55 +0800109 postService.unlikePost(userId, postId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800110 return ResponseEntity.ok(ApiResponse.message("Post unliked successfully."));
223010711f457dc2025-04-15 17:35:55 +0800111 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800112
223010711f457dc2025-04-15 17:35:55 +0800113 @GetMapping("/{postId}/likes")
夜雨声烦e73ff922025-05-13 18:49:03 +0800114 public ResponseEntity<ApiResponse<Long>> getPostLikeCount(@PathVariable Long postId) {
223010711f457dc2025-04-15 17:35:55 +0800115 Long likeCount = postService.getPostLikeCount(postId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800116 return ResponseEntity.ok(ApiResponse.success(likeCount));
223010711f457dc2025-04-15 17:35:55 +0800117 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800118
夜雨声烦4527a722025-04-23 17:04:25 +0800119 @GetMapping("/search")
夜雨声烦e73ff922025-05-13 18:49:03 +0800120 public ResponseEntity<ApiResponse<List<Post>>> searchPosts(
夜雨声烦4527a722025-04-23 17:04:25 +0800121 @RequestParam(required = false) String keyword,
夜雨声烦e73ff922025-05-13 18:49:03 +0800122 @RequestParam(required = false) List<Long> tags,
夜雨声烦4527a722025-04-23 17:04:25 +0800123 @RequestParam(required = false) String author) {
夜雨声烦e73ff922025-05-13 18:49:03 +0800124 List<Post> result = postService.searchPosts(keyword, tags, author);
125 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦4527a722025-04-23 17:04:25 +0800126 }
夜雨声烦ef46ac52025-04-24 20:56:47 +0800127
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800128 @GetMapping("/history")
夜雨声烦e73ff922025-05-13 18:49:03 +0800129 public ResponseEntity<ApiResponse<List<PostHistoryDTO>>> getViewHistory() {
130 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦ef46ac52025-04-24 20:56:47 +0800131 List<PostHistoryDTO> history = postService.getViewHistoryWithTitles(userId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800132 return ResponseEntity.ok(ApiResponse.success(history));
夜雨声烦f4b8b6f2025-04-24 00:58:36 +0800133 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800134
夜雨声烦368e3562025-04-24 01:49:46 +0800135 @GetMapping("/recommended")
夜雨声烦e73ff922025-05-13 18:49:03 +0800136 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedPosts(
夜雨声烦368e3562025-04-24 01:49:46 +0800137 @RequestParam(defaultValue = "1") int page,
138 @RequestParam(defaultValue = "10") int size) {
夜雨声烦e73ff922025-05-13 18:49:03 +0800139 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦368e3562025-04-24 01:49:46 +0800140 Page<Post> pageResult = postService.getRecommendedPosts(page, size, userId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800141 return ResponseEntity.ok(ApiResponse.success(pageResult));
夜雨声烦368e3562025-04-24 01:49:46 +0800142 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800143
夜雨声烦f77d8132025-04-24 19:31:18 +0800144 @GetMapping("/recommended-by-tags")
夜雨声烦e73ff922025-05-13 18:49:03 +0800145 public ResponseEntity<ApiResponse<Page<Post>>> getRecommendedByTags(
夜雨声烦f77d8132025-04-24 19:31:18 +0800146 @RequestParam(defaultValue = "1") int page,
147 @RequestParam(defaultValue = "10") int size) {
夜雨声烦e73ff922025-05-13 18:49:03 +0800148 long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
夜雨声烦f77d8132025-04-24 19:31:18 +0800149 Page<Post> result = postService.getRecommendedByTags(page, size, userId);
夜雨声烦e73ff922025-05-13 18:49:03 +0800150 return ResponseEntity.ok(ApiResponse.success(result));
夜雨声烦f77d8132025-04-24 19:31:18 +0800151 }
夜雨声烦e73ff922025-05-13 18:49:03 +0800152}