mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-20 05:50:53 +08:00
feat: add dedicated mappers
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.*;
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.dto.CommentDto;
|
||||
import com.openisle.dto.PostDetailDto;
|
||||
import com.openisle.dto.PostSummaryDto;
|
||||
import com.openisle.dto.ReactionDto;
|
||||
import com.openisle.model.CommentSort;
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.service.CommentService;
|
||||
import com.openisle.service.ReactionService;
|
||||
import com.openisle.service.SubscriptionService;
|
||||
@@ -12,9 +17,7 @@ import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Mapper responsible for converting domain models into DTOs.
|
||||
*/
|
||||
/** Mapper responsible for converting posts into DTOs. */
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PostMapper {
|
||||
@@ -22,6 +25,11 @@ public class PostMapper {
|
||||
private final CommentService commentService;
|
||||
private final ReactionService reactionService;
|
||||
private final SubscriptionService subscriptionService;
|
||||
private final CommentMapper commentMapper;
|
||||
private final ReactionMapper reactionMapper;
|
||||
private final UserMapper userMapper;
|
||||
private final TagMapper tagMapper;
|
||||
private final CategoryMapper categoryMapper;
|
||||
|
||||
public PostSummaryDto toSummaryDto(Post post) {
|
||||
PostSummaryDto dto = new PostSummaryDto();
|
||||
@@ -34,7 +42,7 @@ public class PostMapper {
|
||||
applyCommon(post, dto);
|
||||
List<CommentDto> comments = commentService.getCommentsForPost(post.getId(), CommentSort.OLDEST)
|
||||
.stream()
|
||||
.map(this::toCommentDtoWithReplies)
|
||||
.map(commentMapper::toDtoWithReplies)
|
||||
.collect(Collectors.toList());
|
||||
dto.setComments(comments);
|
||||
dto.setSubscribed(viewer != null && subscriptionService.isPostSubscribed(viewer, post.getId()));
|
||||
@@ -46,96 +54,25 @@ public class PostMapper {
|
||||
dto.setTitle(post.getTitle());
|
||||
dto.setContent(post.getContent());
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setAuthor(toAuthorDto(post.getAuthor()));
|
||||
dto.setCategory(toCategoryDto(post.getCategory()));
|
||||
dto.setTags(post.getTags().stream().map(this::toTagDto).collect(Collectors.toList()));
|
||||
dto.setAuthor(userMapper.toAuthorDto(post.getAuthor()));
|
||||
dto.setCategory(categoryMapper.toDto(post.getCategory()));
|
||||
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
|
||||
dto.setViews(post.getViews());
|
||||
dto.setStatus(post.getStatus());
|
||||
dto.setPinnedAt(post.getPinnedAt());
|
||||
|
||||
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
|
||||
.stream()
|
||||
.map(this::toReactionDto)
|
||||
.map(reactionMapper::toDto)
|
||||
.collect(Collectors.toList());
|
||||
dto.setReactions(reactions);
|
||||
|
||||
List<User> participants = commentService.getParticipants(post.getId(), 5);
|
||||
dto.setParticipants(participants.stream().map(this::toAuthorDto).collect(Collectors.toList()));
|
||||
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
||||
|
||||
LocalDateTime last = commentService.getLastCommentTime(post.getId());
|
||||
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
|
||||
dto.setReward(0);
|
||||
dto.setSubscribed(false);
|
||||
}
|
||||
|
||||
private CommentDto toCommentDtoWithReplies(Comment comment) {
|
||||
CommentDto dto = toCommentDto(comment);
|
||||
List<CommentDto> replies = commentService.getReplies(comment.getId()).stream()
|
||||
.map(this::toCommentDtoWithReplies)
|
||||
.collect(Collectors.toList());
|
||||
dto.setReplies(replies);
|
||||
|
||||
List<ReactionDto> reactions = reactionService.getReactionsForComment(comment.getId())
|
||||
.stream()
|
||||
.map(this::toReactionDto)
|
||||
.collect(Collectors.toList());
|
||||
dto.setReactions(reactions);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private CommentDto toCommentDto(Comment comment) {
|
||||
CommentDto dto = new CommentDto();
|
||||
dto.setId(comment.getId());
|
||||
dto.setContent(comment.getContent());
|
||||
dto.setCreatedAt(comment.getCreatedAt());
|
||||
dto.setAuthor(toAuthorDto(comment.getAuthor()));
|
||||
dto.setReward(0);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ReactionDto toReactionDto(Reaction reaction) {
|
||||
ReactionDto dto = new ReactionDto();
|
||||
dto.setId(reaction.getId());
|
||||
dto.setType(reaction.getType());
|
||||
dto.setUser(reaction.getUser().getUsername());
|
||||
if (reaction.getPost() != null) {
|
||||
dto.setPostId(reaction.getPost().getId());
|
||||
}
|
||||
if (reaction.getComment() != null) {
|
||||
dto.setCommentId(reaction.getComment().getId());
|
||||
}
|
||||
dto.setReward(0);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private CategoryDto toCategoryDto(Category category) {
|
||||
CategoryDto dto = new CategoryDto();
|
||||
dto.setId(category.getId());
|
||||
dto.setName(category.getName());
|
||||
dto.setDescription(category.getDescription());
|
||||
dto.setIcon(category.getIcon());
|
||||
dto.setSmallIcon(category.getSmallIcon());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private TagDto toTagDto(Tag tag) {
|
||||
TagDto dto = new TagDto();
|
||||
dto.setId(tag.getId());
|
||||
dto.setName(tag.getName());
|
||||
dto.setDescription(tag.getDescription());
|
||||
dto.setIcon(tag.getIcon());
|
||||
dto.setSmallIcon(tag.getSmallIcon());
|
||||
dto.setCreatedAt(tag.getCreatedAt());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private AuthorDto toAuthorDto(User user) {
|
||||
AuthorDto dto = new AuthorDto();
|
||||
dto.setId(user.getId());
|
||||
dto.setUsername(user.getUsername());
|
||||
dto.setAvatar(user.getAvatar());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user