feat: Get the signed in user posts from endpoint

This commit is contained in:
sBubshait 2025-07-23 14:07:20 +03:00
parent 05fb7b19aa
commit c548c7e2a3

View File

@ -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<ApiResponse<PostResponseDTO>> 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<ApiResponse<List<PostResponseDTO>>> 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<ApiResponse<List<PostResponseDTO>>> getUserPosts(
@RequestParam(required = false) Long id,
Authentication authentication) {
try {
List<PostResponseDTO> 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<PostResponseDTO> response = postService.getUserPosts(targetUserId);
return ResponseEntity.ok(ApiResponse.success(response));
} catch (RuntimeException e) {
String message;