blob: f3735392a8267f7f4edcbab87bb13b963abae478 [file] [log] [blame]
wuchimedese5722e32025-04-13 17:38:50 +08001package com.example.g8backend.controller;
2
wuchimedes5a842b22025-04-21 22:01:39 +08003import com.example.g8backend.dto.PostCreateDTO;
wuchimedese5722e32025-04-13 17:38:50 +08004import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.http.ResponseEntity;
6import org.springframework.security.core.Authentication;
7import org.springframework.security.core.context.SecurityContextHolder;
8import org.springframework.web.bind.annotation.*;
9import com.example.g8backend.entity.Post;
10import com.example.g8backend.service.IPostService;
11
12import java.util.List;
13
14@RestController
15@RequestMapping("/post")
16public class PostController {
17 @Autowired
18 private IPostService postService;
19
20 @PostMapping("")
wuchimedes5a842b22025-04-21 22:01:39 +080021 public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) {
wuchimedese5722e32025-04-13 17:38:50 +080022 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
23 long userId = (long) authentication.getPrincipal();
wuchimedes5a842b22025-04-21 22:01:39 +080024 Post post = postCreateDTO.getPost();
25 Long[] tagIds = postCreateDTO.getTagIds();
26
wuchimedese5722e32025-04-13 17:38:50 +080027 post.setUserId(userId);
wuchimedes5a842b22025-04-21 22:01:39 +080028 if (tagIds.length > 0){
29 postService.createPost(post, tagIds);
30 } else {
31 postService.createPost(post);
32 }
wuchimedese5722e32025-04-13 17:38:50 +080033 return ResponseEntity.ok().build();
34 }
35
36 @GetMapping("/{postId}")
37 public Post getPost(@PathVariable("postId") Long postId) {
38 return postService.getById(postId);
39 }
40
41 @DeleteMapping("/{postId}")
42 public ResponseEntity<?> deletePost(@PathVariable("postId") Long postId) {
43 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
44 long userId = (long) authentication.getPrincipal();
45 Post post = postService.getById(postId);
46 if (post == null) {
47 return ResponseEntity.status(500).body("Post not found.");
48 }
49 if (post.getUserId()!= userId) {
50 return ResponseEntity.status(403).body("You are not authorized to delete this post.");
51 }
52 postService.removeById(postId);
53 return ResponseEntity.ok().body("Post deleted successfully.");
54 }
55
56 @GetMapping("/getAll")
57 public List<Post> getAllPosts() {
58 return postService.list();
59 }
60
61 @GetMapping("/getByUserId/{userId}")
62 public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
63 return postService.getPostsByUserId(userId);
64 }
223010711f457dc2025-04-15 17:35:55 +080065
66 @PutMapping("/{postId}")
67 public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
68 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
69 long userId = (long) authentication.getPrincipal();
70 Post existingPost = postService.getById(postId);
71
72 if (existingPost == null) {
73 return ResponseEntity.status(500).body("Post not found.");
74 }
75 if (existingPost.getUserId() != userId) {
76 return ResponseEntity.status(403).body("You are not authorized to update this post.");
77 }
78
79 post.setPostId(postId);
80 post.setUserId(userId);
81 postService.updateById(post);
82 return ResponseEntity.ok().body("Post updated successfully.");
83 }
84
85 @GetMapping("/type/{postType}")
86 public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
87 List<Post> posts = postService.getPostsByType(postType);
88 return ResponseEntity.ok().body(posts);
89 }
90
91 @PostMapping("/{postId}/like")
92 public ResponseEntity<?> likePost(@PathVariable Long postId) {
93 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
94 long userId = (long) authentication.getPrincipal();
95 postService.likePost(userId, postId);
96 return ResponseEntity.ok().body("Post liked successfully.");
97 }
98
99 @DeleteMapping("/{postId}/like")
100 public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
101 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
102 long userId = (long) authentication.getPrincipal();
103 postService.unlikePost(userId, postId);
104 return ResponseEntity.ok().body("Post unliked successfully.");
105 }
106
107 @GetMapping("/{postId}/likes")
108 public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
109 Long likeCount = postService.getPostLikeCount(postId);
110 return ResponseEntity.ok().body(likeCount);
111 }
wuchimedese5722e32025-04-13 17:38:50 +0800112}