wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 3 | import com.example.g8backend.dto.PostCreateDTO; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 4 | import org.springframework.beans.factory.annotation.Autowired; |
| 5 | import org.springframework.http.ResponseEntity; |
| 6 | import org.springframework.security.core.Authentication; |
| 7 | import org.springframework.security.core.context.SecurityContextHolder; |
| 8 | import org.springframework.web.bind.annotation.*; |
| 9 | import com.example.g8backend.entity.Post; |
| 10 | import com.example.g8backend.service.IPostService; |
| 11 | |
| 12 | import java.util.List; |
| 13 | |
| 14 | @RestController |
| 15 | @RequestMapping("/post") |
| 16 | public class PostController { |
| 17 | @Autowired |
| 18 | private IPostService postService; |
| 19 | |
| 20 | @PostMapping("") |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 21 | public ResponseEntity<?> createPost(@RequestBody PostCreateDTO postCreateDTO) { |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 22 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 23 | long userId = (long) authentication.getPrincipal(); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 24 | Post post = postCreateDTO.getPost(); |
| 25 | Long[] tagIds = postCreateDTO.getTagIds(); |
| 26 | |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 27 | post.setUserId(userId); |
wuchimedes | 5a842b2 | 2025-04-21 22:01:39 +0800 | [diff] [blame] | 28 | if (tagIds.length > 0){ |
| 29 | postService.createPost(post, tagIds); |
| 30 | } else { |
| 31 | postService.createPost(post); |
| 32 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 33 | 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 | } |
22301071 | 1f457dc | 2025-04-15 17:35:55 +0800 | [diff] [blame] | 65 | |
| 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 | } |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 112 | } |