blob: 42330287b261dbc7cdbb0db82bae273545976cda [file] [log] [blame]
package com.example.myproject.controller;
import com.example.myproject.entity.Post;
import com.example.myproject.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/echo/forum/posts")
public class PostController {
@Autowired
private PostService postService;
//创建新帖子(已完成)
@PostMapping("/{user_id}/createPost")
public Map<String, Object> createPost(
@PathVariable("user_id") Long userId,
@RequestParam(value = "postContent") String postContent,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "imageUrl") MultipartFile[] imageFiles) {
return postService.createPost(userId, postContent, title, imageFiles);
}
//编辑帖子(已完成)
@PutMapping("/{post_id}/editPost")
public String updatePost(@PathVariable("post_id") Long postId, @RequestBody Post post) {
postService.updatePost(postId, post);
return "Post updated successfully!";
}
//删除帖子(已完成)
@DeleteMapping("/{post_id}/deletePost")
public String deletePost(@PathVariable("post_id") Long postId) {
postService.deletePost(postId);
return "Post deleted successfully!";
}
//点赞帖子(已完成)
@PostMapping("/{post_id}/like")
public String likePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
Long userId = requestBody.get("user_id");
postService.likePost(postId, userId);
return "Post liked successfully!";
}
//取消点赞(已完成)
@PostMapping("/{post_id}/unlike")
public String unlikePost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
Long userId = requestBody.get("user_id");
postService.unlikePost(postId, userId);
return "Post unliked successfully!";
}
//获取帖子列表(已完成)
@GetMapping("/getAllPosts")
public Map<String, Object> getAllPosts() {
return postService.getAllPosts(); // 调用服务层的 getAllPosts 方法
}
@GetMapping("/{postId}/getPost")
public Map<String, Object> getPostById(@PathVariable Long postId) {
return postService.getPostById(postId);
}
//收藏帖子(已完成)
@PostMapping("/{post_id}/collect")
public String collectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
Long userId = requestBody.get("user_id");
postService.collectPost(postId, userId);
return "Post collected successfully!";
}
//取消收藏(已完成)
@DeleteMapping("/{post_id}/uncollect")
public String uncollectPost(@PathVariable("post_id") Long postId, @RequestBody Map<String, Long> requestBody) {
Long userId = requestBody.get("user_id");
postService.uncollectPost(postId, userId);
return "Post uncollected successfully!";
}
// 获取用户收藏的所有帖子
@GetMapping("/{userId}/getAllcollections")
public List<Map<String, Object>> getAllCollections(@PathVariable("userId") Long userId) {
return postService.getAllCollections(userId);
}
}