Skip to content

Commit 9b5eccc

Browse files
committed
added 3. Post Comments, Likes and View API
1 parent 6b4b5b6 commit 9b5eccc

File tree

9 files changed

+106
-28
lines changed

9 files changed

+106
-28
lines changed
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.cevheri.blog.repository;
22

33
import com.cevheri.blog.domain.PostComment;
4+
45
import java.util.List;
56
import java.util.Optional;
7+
68
import org.springframework.data.domain.Page;
79
import org.springframework.data.domain.Pageable;
810
import org.springframework.data.jpa.repository.*;
@@ -18,23 +20,25 @@ default Optional<PostComment> findOneWithEagerRelationships(Long id) {
1820
return this.findOneWithToOneRelationships(id);
1921
}
2022

21-
default List<PostComment> findAllWithEagerRelationships() {
22-
return this.findAllWithToOneRelationships();
23+
default List<PostComment> findAllWithEagerRelationships(Long postId) {
24+
return this.findAllWithToOneRelationships(postId);
2325
}
2426

25-
default Page<PostComment> findAllWithEagerRelationships(Pageable pageable) {
26-
return this.findAllWithToOneRelationships(pageable);
27+
default Page<PostComment> findAllWithEagerRelationships(Long postId, Pageable pageable) {
28+
return this.findAllWithToOneRelationships(postId, pageable);
2729
}
2830

2931
@Query(
30-
value = "select distinct postComment from PostComment postComment left join fetch postComment.post",
31-
countQuery = "select count(distinct postComment) from PostComment postComment"
32+
value = "select distinct postComment from PostComment postComment left join fetch postComment.post where postComment.post.id =:postId",
33+
countQuery = "select count(distinct postComment) from PostComment postComment where postComment.post.id =:postId"
3234
)
33-
Page<PostComment> findAllWithToOneRelationships(Pageable pageable);
35+
Page<PostComment> findAllWithToOneRelationships(@Param("postId") Long postId, Pageable pageable);
3436

35-
@Query("select distinct postComment from PostComment postComment left join fetch postComment.post")
36-
List<PostComment> findAllWithToOneRelationships();
37+
@Query("select distinct postComment from PostComment postComment left join fetch postComment.post where postComment.post.id =:postId")
38+
List<PostComment> findAllWithToOneRelationships(@Param("postId") Long postId);
3739

3840
@Query("select postComment from PostComment postComment left join fetch postComment.post where postComment.id =:id")
3941
Optional<PostComment> findOneWithToOneRelationships(@Param("id") Long id);
42+
43+
Page<PostComment> findAllByPost_Id(@Param("postId") Long postId, Pageable pageable);
4044
}

src/main/java/com/cevheri/blog/repository/PostLikeRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ default Page<PostLike> findAllWithEagerRelationships(Pageable pageable) {
3737

3838
@Query("select postLike from PostLike postLike left join fetch postLike.post where postLike.id =:id")
3939
Optional<PostLike> findOneWithToOneRelationships(@Param("id") Long id);
40+
41+
Integer countByPost_Id(Long id);
4042
}

src/main/java/com/cevheri/blog/service/PostCommentService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public interface PostCommentService {
3939
* @param pageable the pagination information.
4040
* @return the list of entities.
4141
*/
42+
Page<PostCommentDTO> findAll(Long postId, Pageable pageable);
4243
Page<PostCommentDTO> findAll(Pageable pageable);
4344

4445
/**
@@ -47,6 +48,8 @@ public interface PostCommentService {
4748
* @param pageable the pagination information.
4849
* @return the list of entities.
4950
*/
51+
Page<PostCommentDTO> findAllWithEagerRelationships(Long postId, Pageable pageable);
52+
5053
Page<PostCommentDTO> findAllWithEagerRelationships(Pageable pageable);
5154

5255
/**

src/main/java/com/cevheri/blog/service/dto/PostDTO.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ public class PostDTO implements Serializable {
5353

5454
private String integrationId;
5555

56+
private Integer likeCount;
57+
private Integer viewCount;
58+
59+
public Integer getLikeCount() {
60+
return likeCount;
61+
}
62+
63+
public void setLikeCount(Integer likeCount) {
64+
this.likeCount = likeCount;
65+
}
66+
67+
public Integer getViewCount() {
68+
return viewCount;
69+
}
70+
71+
public void setViewCount(Integer viewCount) {
72+
this.viewCount = viewCount;
73+
}
74+
5675
public String getIntegrationId() {
5776
return integrationId;
5877
}

src/main/java/com/cevheri/blog/service/impl/MediumServiceImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ public String sendPost(PostDTO postDTO) {
3838
PostResponse result = new PostResponse();
3939
try {
4040

41-
if(postDTO.getIntegrationId()!=null){
42-
//TODO call medium updatePost api.
43-
}
44-
41+
// if(postDTO.getIntegrationId()!=null){
42+
// //TODO call medium updatePost api.
43+
// }
4544
result = PostApi.createPost(postMapper.toPostModel(postDTO));
46-
4745
if(!result.getErrors().isEmpty()){
4846
//TODO PostCreateError. Medium response Error. Write a log!!!!
4947
}
50-
5148
integrationLogDTO.setExitCode(ExitCodeType.SUCCESS);
5249
integrationLogDTO.setResponseData(result.toString());
5350
}catch (IOException ie){

src/main/java/com/cevheri/blog/service/impl/PostCommentServiceImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.cevheri.blog.service.PostCommentService;
66
import com.cevheri.blog.service.dto.PostCommentDTO;
77
import com.cevheri.blog.service.mapper.PostCommentMapper;
8+
89
import java.util.Optional;
10+
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113
import org.springframework.data.domain.Page;
@@ -62,15 +64,28 @@ public Optional<PostCommentDTO> partialUpdate(PostCommentDTO postCommentDTO) {
6264
.map(postCommentMapper::toDto);
6365
}
6466

67+
@Override
68+
@Transactional(readOnly = true)
69+
public Page<PostCommentDTO> findAll(Long postId, Pageable pageable) {
70+
log.debug("Request to get all PostComments");
71+
return postCommentRepository.findAllByPost_Id(postId, pageable).map(postCommentMapper::toDto);
72+
}
73+
6574
@Override
6675
@Transactional(readOnly = true)
6776
public Page<PostCommentDTO> findAll(Pageable pageable) {
6877
log.debug("Request to get all PostComments");
6978
return postCommentRepository.findAll(pageable).map(postCommentMapper::toDto);
7079
}
7180

81+
@Override
82+
public Page<PostCommentDTO> findAllWithEagerRelationships(Long postId, Pageable pageable) {
83+
return postCommentRepository.findAllWithEagerRelationships(postId, pageable).map(postCommentMapper::toDto);
84+
}
85+
86+
@Override
7287
public Page<PostCommentDTO> findAllWithEagerRelationships(Pageable pageable) {
73-
return postCommentRepository.findAllWithEagerRelationships(pageable).map(postCommentMapper::toDto);
88+
return postCommentRepository.findAll(pageable).map(postCommentMapper::toDto);
7489
}
7590

7691
@Override

src/main/java/com/cevheri/blog/service/impl/PostServiceImpl.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cevheri.blog.service.impl;
22

33
import com.cevheri.blog.domain.Post;
4+
import com.cevheri.blog.repository.PostLikeRepository;
45
import com.cevheri.blog.repository.PostRepository;
56
import com.cevheri.blog.repository.PostViewRepository;
67
import com.cevheri.blog.service.PostService;
@@ -32,17 +33,20 @@ public class PostServiceImpl implements PostService {
3233

3334
private final PostRepository postRepository;
3435
private final PostViewRepository postViewRepository;
36+
private final PostLikeRepository postLikeRepository;
3537
private final PostMapper postMapper;
3638
private final ThirdPartyBlogService thirdPartyBlogService;
3739
private final PostViewService postViewService;
3840

3941
public PostServiceImpl(PostRepository postRepository,
4042
PostViewRepository postViewRepository,
43+
PostLikeRepository postLikeRepository,
4144
PostMapper postMapper,
4245
ThirdPartyBlogService thirdPartyBlogService,
4346
PostViewService postViewService) {
4447
this.postRepository = postRepository;
4548
this.postViewRepository = postViewRepository;
49+
this.postLikeRepository = postLikeRepository;
4650
this.postMapper = postMapper;
4751
this.thirdPartyBlogService = thirdPartyBlogService;
4852
this.postViewService = postViewService;
@@ -69,7 +73,11 @@ public PostDTO update(UpdatePostDTO postDTO) {
6973
log.debug("Request to save Post : {}", postDTO);
7074
Post post = postMapper.toEntity(postDTO);
7175
post = postRepository.save(post);
72-
return postMapper.toDto(post);
76+
var result = postMapper.toDto(post);
77+
if (post.getIntegrationId() != null) {
78+
thirdPartyBlogService.sendPost(result);
79+
}
80+
return result;
7381
}
7482

7583
@Override
@@ -91,7 +99,10 @@ public Optional<PostDTO> partialUpdate(PostDTO postDTO) {
9199
@Transactional(readOnly = true)
92100
public Page<PostDTO> findAll(Pageable pageable) {
93101
log.debug("Request to get all Posts");
94-
return postRepository.findAll(pageable).map(postMapper::toDto);
102+
return postRepository
103+
.findAll(pageable)
104+
.map(postMapper::toDto)
105+
;
95106
}
96107

97108
public Page<PostDTO> findAllWithEagerRelationships(Pageable pageable) {
@@ -101,7 +112,8 @@ public Page<PostDTO> findAllWithEagerRelationships(Pageable pageable) {
101112
@Override
102113
public Optional<PostDTO> findOne(Long id) {
103114
log.debug("Request to get Post : {}", id);
104-
var result = postRepository.findOneWithEagerRelationships(id).map(postMapper::toDto);
115+
var result = postRepository.findOneWithEagerRelationships(id)
116+
.map(postMapper::toDto);
105117
result.ifPresentOrElse(t -> {
106118
PostViewDTO postView = new PostViewDTO();
107119
postView.setPost(t);
@@ -111,7 +123,9 @@ public Optional<PostDTO> findOne(Long id) {
111123
throw new BadRequestAlertException("Entity not found", "post", "idnotfound");
112124
}
113125
);
114-
126+
PostDTO postDTO = result.get();
127+
postDTO.setLikeCount(postLikeRepository.countByPost_Id(postDTO.getId()));
128+
postDTO.setViewCount(postViewRepository.countByPost_Id(postDTO.getId()));
115129
return result;
116130
}
117131

src/main/java/com/cevheri/blog/service/mapper/PostMapper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ default Set<TagDTO> toDtoTagNameSet(Set<Tag> tag) {
5252
return tag.stream().map(this::toDtoTagName).collect(Collectors.toSet());
5353
}
5454

55-
default PostResponse toPostModel(PostDTO postDTO){
55+
default PostResponse toPostModel(PostDTO postDTO) {
5656
PostResponse result = new PostResponse();
5757
PostResponse.PostData model = result.getData();
58-
model.setId(null);
59-
model.setAuthorId(null);
58+
if (postDTO.getIntegrationId() != null) {
59+
model.setId(postDTO.getIntegrationId());
60+
}
6061
model.setTitle(postDTO.getTitle());
6162
model.setTags(postDTO.getTags().stream().map(TagDTO::getName).collect(Collectors.toList()));
6263
model.setCanonicalUrl("https://cevheri-blog.herokuapp.com");
6364
model.setContent(postDTO.getContent());
64-
model.setLicense(null);
65+
model.setLicense("all-rights-reserved");
6566
model.setContentFormat("html");
66-
model.setPublishStatus(null);
67+
model.setPublishStatus("public");
6768
result.setData(model);
6869
return result;
6970
}

src/main/java/com/cevheri/blog/web/rest/PostCommentResource.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,37 @@ public ResponseEntity<List<PostCommentDTO>> getAllPostComments(
153153
log.debug("REST request to get a page of PostComments");
154154
Page<PostCommentDTO> page;
155155
if (eagerload) {
156-
page = postCommentService.findAllWithEagerRelationships(pageable);
156+
page = postCommentService.findAllWithEagerRelationships(1L, pageable);
157157
} else {
158-
page = postCommentService.findAll(pageable);
158+
page = postCommentService.findAll(1L, pageable);
159+
}
160+
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
161+
return ResponseEntity.ok().headers(headers).body(page.getContent());
162+
}
163+
/**
164+
* {@code GET /post-comments/:postId } : get all the postComments By postId.
165+
*
166+
* @param postId the post ID information
167+
* @param pageable the pagination information.
168+
* @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many).
169+
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of postComments in body.
170+
*/
171+
@GetMapping("/post-comments/post/{postId}")
172+
public ResponseEntity<List<PostCommentDTO>> getAllPostCommentsByPostId(
173+
@PathVariable(value = "postId") Long postId,
174+
@org.springdoc.api.annotations.ParameterObject Pageable pageable,
175+
@RequestParam(required = false, defaultValue = "false") boolean eagerload
176+
) {
177+
log.debug("REST request to get a page of PostComments");
178+
Page<PostCommentDTO> page;
179+
if (eagerload) {
180+
page = postCommentService.findAllWithEagerRelationships(postId, pageable);
181+
} else {
182+
page = postCommentService.findAll(postId, pageable);
159183
}
160184
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
161185
return ResponseEntity.ok().headers(headers).body(page.getContent());
162186
}
163-
164187
/**
165188
* {@code GET /post-comments/:id} : get the "id" postComment.
166189
*

0 commit comments

Comments
 (0)