Skip to content

Commit

Permalink
feature/like-count
Browse files Browse the repository at this point in the history
Like Count has been added along in Blog Response and Blog List Response
  • Loading branch information
rohit-zip committed Sep 2, 2024
1 parent c0851b3 commit 70edc48
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ public class BlogResponse {
private String seoTitle;
private String canonicalUrl;
private String chapterName;
private Long likes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.bloggios.blog.constants.DataErrorCodes;
import com.bloggios.blog.dao.implementation.esimplementation.ChapterDocumentDao;
import com.bloggios.blog.dao.implementation.esimplementation.LikeDocumentDao;
import com.bloggios.blog.document.BlogDocument;
import com.bloggios.blog.document.ChapterDocument;
import com.bloggios.blog.exception.payloads.BadRequestException;
import com.bloggios.blog.feign.implementation.ProfileInternalResponseFeignCall;
import com.bloggios.blog.payload.response.BlogResponse;
import com.bloggios.blog.payload.response.ProfileInternalResponse;
import com.bloggios.blog.transformer.Transform;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand All @@ -26,23 +28,21 @@
*/

@Component
@RequiredArgsConstructor
public class BlogDocumentToBlogResponseTransformer implements Transform<BlogResponse, BlogDocument> {

private final ModelMapper modelMapper;
private final ProfileInternalResponseFeignCall profileInternalResponseFeignCall;
private final ChapterDocumentDao chapterDocumentDao;

public BlogDocumentToBlogResponseTransformer(ModelMapper modelMapper, ProfileInternalResponseFeignCall profileInternalResponseFeignCall, ChapterDocumentDao chapterDocumentDao) {
this.modelMapper = modelMapper;
this.profileInternalResponseFeignCall = profileInternalResponseFeignCall;
this.chapterDocumentDao = chapterDocumentDao;
}
private final LikeDocumentDao likeDocumentDao;

@Override
public BlogResponse transform(BlogDocument blogDocument) {
BlogResponse blogResponse = modelMapper.map(blogDocument, BlogResponse.class);
CompletableFuture<ProfileInternalResponse> profileInternalResponseCompletableFuture = CompletableFuture.supplyAsync(() -> profileInternalResponseFeignCall.callFeign(blogDocument.getUserId())
.orElseThrow(() -> new BadRequestException(DataErrorCodes.PROFILE_DATA_NOT_FOUND_FEIGN)));
CompletableFuture<Long> likeCountCompletableFuture = CompletableFuture.supplyAsync(() -> likeDocumentDao.countLikeDocumentByDestinationId(blogDocument.getBlogId()));
CompletableFuture.allOf(profileInternalResponseCompletableFuture, likeCountCompletableFuture);
ChapterDocument chapterDocument = null;
if (StringUtils.hasText(blogDocument.getChapterId())) {
chapterDocument = chapterDocumentDao.findById(blogDocument.getChapterId())
Expand All @@ -55,6 +55,7 @@ public BlogResponse transform(BlogDocument blogDocument) {
blogResponse.setProfileImage(profileInternalResponse.getProfileImage());
blogResponse.setBadge(profileInternalResponse.isBadge());
blogResponse.setChapterName(Objects.nonNull(chapterDocument) ? chapterDocument.getChapterName() : null);
blogResponse.setLikes(likeCountCompletableFuture.join() != null ? likeCountCompletableFuture.join() : 0);
return blogResponse;
}
}

0 comments on commit 70edc48

Please sign in to comment.