blob: 42330287b261dbc7cdbb0db82bae273545976cda [file] [log] [blame]
223011381c359102025-06-03 15:19:59 +08001package com.example.myproject.controller;
2
3import com.example.myproject.entity.Post;
4import com.example.myproject.service.PostService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.web.bind.annotation.*;
7import org.springframework.web.multipart.MultipartFile;
8
9import java.util.List;
10import java.util.Map;
11import java.util.Optional;
12
13@RestController
14@RequestMapping("/echo/forum/posts")
15public class PostController {
16
17 @Autowired
18 private PostService postService;
19
20 //创建新帖子(已完成)
21 @PostMapping("/{user_id}/createPost")
22 public Map<String, Object> createPost(
23 @PathVariable("user_id") Long userId,
24 @RequestParam(value = "postContent") String postContent,
25 @RequestParam(value = "title", required = false) String title,
26 @RequestParam(value = "imageUrl") MultipartFile[] imageFiles) {
27 return postService.createPost(userId, postContent, title, imageFiles);
28 }
29
30 //编辑帖子(已完成)
31 @PutMapping("/{post_id}/editPost")
32 public String updatePost(@PathVariable("post_id") Long postId, @RequestBody Post post) {
33 postService.updatePost(postId, post);
34 return "Post updated successfully!";
35 }
36
37 //删除帖子(已完成)
38 @DeleteMapping("/{post_id}/deletePost")
39 public String deletePost(@PathVariable("post_id") Long postId) {
40 postService.deletePost(postId);
41 return "Post deleted successfully!";
42 }
43
44 //点赞帖子(已完成)
45 @PostMapping("/{post_id}/like")
46 public String likePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
47
48 Long userId = requestBody.get("user_id");
49 postService.likePost(postId, userId);
50
51 return "Post liked successfully!";
52 }
53
54 //取消点赞(已完成)
55 @PostMapping("/{post_id}/unlike")
56 public String unlikePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
57 Long userId = requestBody.get("user_id");
58 postService.unlikePost(postId, userId);
59
60 return "Post unliked successfully!";
61 }
62
63 //获取帖子列表(已完成)
64 @GetMapping("/getAllPosts")
65 public Map<String, Object> getAllPosts() {
66 return postService.getAllPosts(); // 调用服务层的 getAllPosts 方法
67 }
68
69 @GetMapping("/{postId}/getPost")
70 public Map<String, Object> getPostById(@PathVariable Long postId) {
71 return postService.getPostById(postId);
72 }
73
74
75 //收藏帖子(已完成)
76 @PostMapping("/{post_id}/collect")
77 public String collectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
78 Long userId = requestBody.get("user_id");
79 postService.collectPost(postId, userId);
80
81 return "Post collected successfully!";
82 }
83
84
85 //取消收藏(已完成)
86 @DeleteMapping("/{post_id}/uncollect")
87 public String uncollectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
88 Long userId = requestBody.get("user_id");
89 postService.uncollectPost(postId, userId);
90 return "Post uncollected successfully!";
91 }
92
93 // 获取用户收藏的所有帖子
94 @GetMapping("/{userId}/getAllcollections")
95 public List<Map<String, Object>> getAllCollections(@PathVariable("userId") Long userId) {
96 return postService.getAllCollections(userId);
97 }
98
99
100}