complete_postservice_test_code
Change-Id: If176a0ac12d71e7695635761e7199c03926e4c85
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
index 7b7b7c7..3557af2 100644
--- a/src/main/java/com/example/g8backend/controller/PostController.java
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -54,4 +54,51 @@
public List<Post> getPostsByUserId(@PathVariable("userId") Long userId) {
return postService.getPostsByUserId(userId);
}
+
+ @PutMapping("/{postId}")
+ public ResponseEntity<?> updatePost(@PathVariable("postId") Long postId, @RequestBody Post post) {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ long userId = (long) authentication.getPrincipal();
+ Post existingPost = postService.getById(postId);
+
+ if (existingPost == null) {
+ return ResponseEntity.status(500).body("Post not found.");
+ }
+ if (existingPost.getUserId() != userId) {
+ return ResponseEntity.status(403).body("You are not authorized to update this post.");
+ }
+
+ post.setPostId(postId);
+ post.setUserId(userId);
+ postService.updateById(post);
+ return ResponseEntity.ok().body("Post updated successfully.");
+ }
+
+ @GetMapping("/type/{postType}")
+ public ResponseEntity<?> getPostsByType(@PathVariable String postType) {
+ List<Post> posts = postService.getPostsByType(postType);
+ return ResponseEntity.ok().body(posts);
+ }
+
+ @PostMapping("/{postId}/like")
+ public ResponseEntity<?> likePost(@PathVariable Long postId) {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ long userId = (long) authentication.getPrincipal();
+ postService.likePost(userId, postId);
+ return ResponseEntity.ok().body("Post liked successfully.");
+ }
+
+ @DeleteMapping("/{postId}/like")
+ public ResponseEntity<?> unlikePost(@PathVariable Long postId) {
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ long userId = (long) authentication.getPrincipal();
+ postService.unlikePost(userId, postId);
+ return ResponseEntity.ok().body("Post unliked successfully.");
+ }
+
+ @GetMapping("/{postId}/likes")
+ public ResponseEntity<?> getPostLikeCount(@PathVariable Long postId) {
+ Long likeCount = postService.getPostLikeCount(postId);
+ return ResponseEntity.ok().body(likeCount);
+ }
}