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