feat: getting all posts and user's posts.
This commit is contained in:
parent
e146b18f1d
commit
32cec6da12
@ -14,6 +14,9 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/posts")
|
||||
@CrossOrigin(origins = "*")
|
||||
@ -43,8 +46,7 @@ public class PostController {
|
||||
}
|
||||
|
||||
try {
|
||||
Post post = postService.createPost(request.getBody());
|
||||
PostResponseDTO response = new PostResponseDTO(post);
|
||||
PostResponseDTO response = postService.createPostWithResponse(request.getBody());
|
||||
return ResponseEntity.ok(ApiResponse.success(response));
|
||||
} catch (RuntimeException e) {
|
||||
String message;
|
||||
@ -58,4 +60,38 @@ public class PostController {
|
||||
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@Operation(summary = "Get all recent posts", description = "Get all posts from the last 7 days, ordered by creation date (latest first)")
|
||||
public ResponseEntity<ApiResponse<List<PostResponseDTO>>> getAllRecentPosts() {
|
||||
try {
|
||||
List<PostResponseDTO> response = postService.getAllRecentPosts();
|
||||
return ResponseEntity.ok(ApiResponse.success(response));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||
}
|
||||
}
|
||||
|
||||
@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"));
|
||||
}
|
||||
|
||||
try {
|
||||
List<PostResponseDTO> response = postService.getUserPosts(id);
|
||||
return ResponseEntity.ok(ApiResponse.success(response));
|
||||
} catch (RuntimeException e) {
|
||||
String message;
|
||||
if (e.getMessage().contains("User not found")) {
|
||||
message = "User not found";
|
||||
} else {
|
||||
message = "Something went wrong.. We're sorry but try again later";
|
||||
}
|
||||
return ResponseEntity.badRequest().body(ApiResponse.error(message));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body(ApiResponse.error("Something went wrong.. We're sorry but try again later"));
|
||||
}
|
||||
}
|
||||
}
|
||||
32
backend/src/main/java/online/wesal/wesal/dto/CreatorDTO.java
Normal file
32
backend/src/main/java/online/wesal/wesal/dto/CreatorDTO.java
Normal file
@ -0,0 +1,32 @@
|
||||
package online.wesal.wesal.dto;
|
||||
|
||||
import online.wesal.wesal.entity.User;
|
||||
|
||||
public class CreatorDTO {
|
||||
|
||||
private String id;
|
||||
private String displayName;
|
||||
|
||||
public CreatorDTO() {}
|
||||
|
||||
public CreatorDTO(User user) {
|
||||
this.id = String.valueOf(user.getId());
|
||||
this.displayName = user.getDisplayName();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public void setDisplayName(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,13 @@
|
||||
package online.wesal.wesal.dto;
|
||||
|
||||
import online.wesal.wesal.entity.Post;
|
||||
import online.wesal.wesal.entity.User;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class PostResponseDTO {
|
||||
|
||||
private String id;
|
||||
private String creatorId;
|
||||
private CreatorDTO creator;
|
||||
private String body;
|
||||
private String likes;
|
||||
private String comments;
|
||||
@ -14,9 +15,9 @@ public class PostResponseDTO {
|
||||
|
||||
public PostResponseDTO() {}
|
||||
|
||||
public PostResponseDTO(Post post) {
|
||||
public PostResponseDTO(Post post, User creator) {
|
||||
this.id = String.valueOf(post.getId());
|
||||
this.creatorId = String.valueOf(post.getCreatorId());
|
||||
this.creator = new CreatorDTO(creator);
|
||||
this.body = post.getBody();
|
||||
this.likes = String.valueOf(post.getLikes());
|
||||
this.comments = String.valueOf(post.getComments());
|
||||
@ -31,12 +32,12 @@ public class PostResponseDTO {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCreatorId() {
|
||||
return creatorId;
|
||||
public CreatorDTO getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public void setCreatorId(String creatorId) {
|
||||
this.creatorId = creatorId;
|
||||
public void setCreator(CreatorDTO creator) {
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
|
||||
@ -2,11 +2,19 @@ package online.wesal.wesal.repository;
|
||||
|
||||
import online.wesal.wesal.entity.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
List<Post> findByCreatorIdOrderByCreationDateDesc(Long creatorId);
|
||||
|
||||
@Query("SELECT p FROM Post p WHERE p.creationDate >= :sevenDaysAgo ORDER BY p.creationDate DESC")
|
||||
List<Post> findAllPostsWithinLast7Days(@Param("sevenDaysAgo") LocalDateTime sevenDaysAgo);
|
||||
|
||||
List<Post> findAllByOrderByCreationDateDesc();
|
||||
}
|
||||
@ -1,17 +1,27 @@
|
||||
package online.wesal.wesal.service;
|
||||
|
||||
import online.wesal.wesal.dto.PostResponseDTO;
|
||||
import online.wesal.wesal.entity.Post;
|
||||
import online.wesal.wesal.entity.User;
|
||||
import online.wesal.wesal.repository.PostRepository;
|
||||
import online.wesal.wesal.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class PostService {
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@ -20,4 +30,38 @@ public class PostService {
|
||||
Post post = new Post(currentUser.getId(), body);
|
||||
return postRepository.save(post);
|
||||
}
|
||||
|
||||
public List<PostResponseDTO> getAllRecentPosts() {
|
||||
LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
|
||||
List<Post> posts = postRepository.findAllPostsWithinLast7Days(sevenDaysAgo);
|
||||
|
||||
List<Long> creatorIds = posts.stream()
|
||||
.map(Post::getCreatorId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<Long, User> creators = userRepository.findAllById(creatorIds).stream()
|
||||
.collect(Collectors.toMap(User::getId, user -> user));
|
||||
|
||||
return posts.stream()
|
||||
.map(post -> new PostResponseDTO(post, creators.get(post.getCreatorId())))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<PostResponseDTO> getUserPosts(Long userId) {
|
||||
List<Post> posts = postRepository.findByCreatorIdOrderByCreationDateDesc(userId);
|
||||
User creator = userRepository.findById(userId)
|
||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
||||
|
||||
return posts.stream()
|
||||
.map(post -> new PostResponseDTO(post, creator))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public PostResponseDTO createPostWithResponse(String body) {
|
||||
User currentUser = userService.getCurrentUser();
|
||||
Post post = new Post(currentUser.getId(), body);
|
||||
post = postRepository.save(post);
|
||||
return new PostResponseDTO(post, currentUser);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user