From c548c7e2a3cda596d24a0091573ccb55005e666d Mon Sep 17 00:00:00 2001 From: sBubshait Date: Wed, 23 Jul 2025 14:07:20 +0300 Subject: [PATCH] feat: Get the signed in user posts from endpoint --- .../wesal/controller/PostController.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/online/wesal/wesal/controller/PostController.java b/backend/src/main/java/online/wesal/wesal/controller/PostController.java index 2d34d88..812cb9f 100644 --- a/backend/src/main/java/online/wesal/wesal/controller/PostController.java +++ b/backend/src/main/java/online/wesal/wesal/controller/PostController.java @@ -8,6 +8,7 @@ import online.wesal.wesal.dto.PostCreateRequestDTO; import online.wesal.wesal.dto.PostResponseDTO; import online.wesal.wesal.entity.Post; import online.wesal.wesal.service.PostService; +import online.wesal.wesal.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -26,6 +27,9 @@ public class PostController { @Autowired private PostService postService; + @Autowired + private UserService userService; + @PostMapping(value = "/create", consumes = "application/json", produces = "application/json") @Operation(summary = "Create post", description = "Create a new post") public ResponseEntity> createPost( @@ -73,14 +77,25 @@ public class PostController { } @GetMapping("/user") - @Operation(summary = "Get user posts", description = "Get all posts by a specific user, ordered by creation date (latest first)") - public ResponseEntity>> getUserPosts(@RequestParam Long id) { - if (id == null || id <= 0) { - return ResponseEntity.badRequest().body(ApiResponse.error("Valid user ID is required")); - } + @Operation(summary = "Get user posts", description = "Get all posts by a specific user, ordered by creation date (latest first). If no id provided, returns authenticated user's posts.") + public ResponseEntity>> getUserPosts( + @RequestParam(required = false) Long id, + Authentication authentication) { try { - List response = postService.getUserPosts(id); + Long targetUserId; + if (id == null) { + // Use authenticated user's ID when no id is provided + String userEmail = authentication.getName(); + targetUserId = userService.getCurrentUser().getId(); + } else { + if (id <= 0) { + return ResponseEntity.badRequest().body(ApiResponse.error("Valid user ID is required")); + } + targetUserId = id; + } + + List response = postService.getUserPosts(targetUserId); return ResponseEntity.ok(ApiResponse.success(response)); } catch (RuntimeException e) { String message;