Skip to content

Commit

Permalink
feature: Additional tasks - add a GET endpoint to get a post by id
Browse files Browse the repository at this point in the history
task:  8697gdchv
  • Loading branch information
KinTrae committed Jan 16, 2025
1 parent 3809b22 commit 7428082
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public ResponseEntity<Page<PostDto>> getPosts(@AuthenticationPrincipal UserDetai
public ResponseEntity<Page<PostDto>> getPostsForUser(@PathVariable("login") String login, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
Page<PostDto> posts = postService.getPostsForUser(login, userDetails.getUsername(), pageNo, pageSize);
return ResponseEntity.ok(posts);
}

@GetMapping("/{postId}")
public ResponseEntity<PostDto> getPostById(@PathVariable("postId") String postId, @AuthenticationPrincipal UserDetails userDetails) {
PostDto postDto = postService.getPostById(userDetails.getUsername(), postId);
return ResponseEntity.ok(postDto);
}

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public interface PostRepository extends JpaRepository<Post, String> {
JOIN u.posts p
LEFT JOIN Profile profile ON profile.user.id = u.id
LEFT JOIN ProfilePicture pp ON pp.profile.id = profile.id
LEFT JOIN u.postsPrivacy postsPrivacy
WHERE postsPrivacy.code = 'PUBLIC'
OR (postsPrivacy.code = 'FRIENDS_ONLY' AND u.id IN (u.id,
WHERE u.postsPrivacy.code = 'PUBLIC'
OR (u.postsPrivacy.code = 'FRIENDS_ONLY' AND u.id IN (u.id,
(SELECT r.receiver.id
FROM User sender
JOIN sender.userRelationsSender r
Expand Down Expand Up @@ -92,6 +91,36 @@ public interface PostRepository extends JpaRepository<Post, String> {
""")
Page<PostDto> findByUserLoginIfPublicOrFriend(@Param("login") String login, @Param("requestedBy") String requestedBy, Pageable pageable);

@Query("""
SELECT new meowhub.backend.posts.dtos.PostDto (
p.id,
p.contentHtml,
new meowhub.backend.users.dtos.BasicUserInfoDto (
u.id,
u.name,
u.surname,
u.login,
pp.ociUrl
),
(SELECT COUNT(c.id) FROM Comment c WHERE c.post.id = p.id),
p.createdAt
)
FROM Post p
JOIN User u ON p.user.id = u.id
LEFT JOIN ProfilePicture pp ON pp.profile.user.id = u.id AND pp.isCurrentProfilePicture = true
WHERE p.id = :postId
AND (u.postsPrivacy.code = 'PUBLIC'
OR (u.postsPrivacy.code = 'FRIENDS_ONLY' AND (
SELECT COUNT(*)
FROM UserRelation ur
WHERE ((ur.receiver.login = u.login AND ur.sender.login = :requestedBy)
OR (ur.sender.login = u.login AND ur.receiver.login = :requestedBy))
AND ur.relationType.code = 'FRIENDS'
) > 0
))
""")
PostDto getPostByIdIfPublicOrFriends(@Param("requestedBy") String requestedBy, @Param("postId") String postId);

@Query("""
SELECT new meowhub.backend.posts.dtos.PostDto (
p.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface PostService {

Page<PostDto> getPostsForUser(String login, String requestedBy, int pageNo, int pageSize);

PostDto getPostById(String requestedBy, String postId);

PostDto createPost(String login, String content, List<MultipartFile> pictures);

PostDto updatePost(String login, String postId, String content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class PostServiceImpl implements PostService {
private final PictureUtils pictureUtils;
private final PostPictureRepository postPictureRepository;

@Override
public PostDto getPostById(String requestedBy, String postId) {
return postRepository.getPostByIdIfPublicOrFriends(requestedBy, postId);
}

@Override
public Page<PostDto> getPosts(String requestedBy, int pageNo, int pageSize) {
Pageable pageable = PageRequest.of(pageNo, pageSize);
Expand Down

0 comments on commit 7428082

Please sign in to comment.