mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 18:51:14 +08:00
feat: add dedicated mappers
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.CategoryDto;
|
||||
import com.openisle.model.Category;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Mapper for category entities. */
|
||||
@Component
|
||||
public class CategoryMapper {
|
||||
|
||||
public CategoryDto toDto(Category c) {
|
||||
return toDto(c, null);
|
||||
}
|
||||
|
||||
public CategoryDto toDto(Category c, Long count) {
|
||||
CategoryDto dto = new CategoryDto();
|
||||
dto.setId(c.getId());
|
||||
dto.setName(c.getName());
|
||||
dto.setDescription(c.getDescription());
|
||||
dto.setIcon(c.getIcon());
|
||||
dto.setSmallIcon(c.getSmallIcon());
|
||||
dto.setCount(count);
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
42
backend/src/main/java/com/openisle/mapper/CommentMapper.java
Normal file
42
backend/src/main/java/com/openisle/mapper/CommentMapper.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.CommentDto;
|
||||
import com.openisle.model.Comment;
|
||||
import com.openisle.service.CommentService;
|
||||
import com.openisle.service.ReactionService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Mapper for comments including replies and reactions. */
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CommentMapper {
|
||||
|
||||
private final CommentService commentService;
|
||||
private final ReactionService reactionService;
|
||||
private final ReactionMapper reactionMapper;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
public CommentDto toDto(Comment comment) {
|
||||
CommentDto dto = new CommentDto();
|
||||
dto.setId(comment.getId());
|
||||
dto.setContent(comment.getContent());
|
||||
dto.setCreatedAt(comment.getCreatedAt());
|
||||
dto.setAuthor(userMapper.toAuthorDto(comment.getAuthor()));
|
||||
dto.setReward(0);
|
||||
return dto;
|
||||
}
|
||||
|
||||
public CommentDto toDtoWithReplies(Comment comment) {
|
||||
CommentDto dto = toDto(comment);
|
||||
dto.setReplies(commentService.getReplies(comment.getId()).stream()
|
||||
.map(this::toDtoWithReplies)
|
||||
.collect(Collectors.toList()));
|
||||
dto.setReactions(reactionService.getReactionsForComment(comment.getId()).stream()
|
||||
.map(reactionMapper::toDto)
|
||||
.collect(Collectors.toList()));
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
24
backend/src/main/java/com/openisle/mapper/DraftMapper.java
Normal file
24
backend/src/main/java/com/openisle/mapper/DraftMapper.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.DraftDto;
|
||||
import com.openisle.model.Draft;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** Mapper for draft entities. */
|
||||
@Component
|
||||
public class DraftMapper {
|
||||
|
||||
public DraftDto toDto(Draft draft) {
|
||||
DraftDto dto = new DraftDto();
|
||||
dto.setId(draft.getId());
|
||||
dto.setTitle(draft.getTitle());
|
||||
dto.setContent(draft.getContent());
|
||||
if (draft.getCategory() != null) {
|
||||
dto.setCategoryId(draft.getCategory().getId());
|
||||
}
|
||||
dto.setTagIds(draft.getTags().stream().map(tag -> tag.getId()).collect(Collectors.toList()));
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.NotificationDto;
|
||||
import com.openisle.dto.PostSummaryDto;
|
||||
import com.openisle.model.Comment;
|
||||
import com.openisle.model.Notification;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Mapper for notifications. */
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class NotificationMapper {
|
||||
|
||||
private final CommentMapper commentMapper;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
public NotificationDto toDto(Notification n) {
|
||||
NotificationDto dto = new NotificationDto();
|
||||
dto.setId(n.getId());
|
||||
dto.setType(n.getType());
|
||||
if (n.getPost() != null) {
|
||||
PostSummaryDto postDto = new PostSummaryDto();
|
||||
postDto.setId(n.getPost().getId());
|
||||
postDto.setTitle(n.getPost().getTitle());
|
||||
dto.setPost(postDto);
|
||||
}
|
||||
if (n.getComment() != null) {
|
||||
dto.setComment(commentMapper.toDto(n.getComment()));
|
||||
Comment parent = n.getComment().getParent();
|
||||
if (parent != null) {
|
||||
dto.setParentComment(commentMapper.toDto(parent));
|
||||
}
|
||||
}
|
||||
if (n.getFromUser() != null) {
|
||||
dto.setFromUser(userMapper.toAuthorDto(n.getFromUser()));
|
||||
}
|
||||
if (n.getReactionType() != null) {
|
||||
dto.setReactionType(n.getReactionType());
|
||||
}
|
||||
dto.setApproved(n.getApproved());
|
||||
dto.setContent(n.getContent());
|
||||
dto.setRead(n.isRead());
|
||||
dto.setCreatedAt(n.getCreatedAt());
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.ReactionDto;
|
||||
import com.openisle.model.Reaction;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Mapper for reactions. */
|
||||
@Component
|
||||
public class ReactionMapper {
|
||||
|
||||
public ReactionDto toDto(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;
|
||||
}
|
||||
}
|
||||
26
backend/src/main/java/com/openisle/mapper/TagMapper.java
Normal file
26
backend/src/main/java/com/openisle/mapper/TagMapper.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.TagDto;
|
||||
import com.openisle.model.Tag;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Mapper for tag entities. */
|
||||
@Component
|
||||
public class TagMapper {
|
||||
|
||||
public TagDto toDto(Tag tag) {
|
||||
return toDto(tag, null);
|
||||
}
|
||||
|
||||
public TagDto toDto(Tag tag, Long count) {
|
||||
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());
|
||||
dto.setCount(count);
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
104
backend/src/main/java/com/openisle/mapper/UserMapper.java
Normal file
104
backend/src/main/java/com/openisle/mapper/UserMapper.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package com.openisle.mapper;
|
||||
|
||||
import com.openisle.dto.*;
|
||||
import com.openisle.model.Comment;
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.service.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Mapper for user related DTOs. */
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class UserMapper {
|
||||
|
||||
private final SubscriptionService subscriptionService;
|
||||
private final PostService postService;
|
||||
private final CommentService commentService;
|
||||
private final ReactionService reactionService;
|
||||
private final UserVisitService userVisitService;
|
||||
private final PostReadService postReadService;
|
||||
private final LevelService levelService;
|
||||
|
||||
@Value("${app.snippet-length:50}")
|
||||
private int snippetLength;
|
||||
|
||||
public AuthorDto toAuthorDto(User user) {
|
||||
AuthorDto dto = new AuthorDto();
|
||||
dto.setId(user.getId());
|
||||
dto.setUsername(user.getUsername());
|
||||
dto.setAvatar(user.getAvatar());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public UserDto toDto(User user, Authentication viewer) {
|
||||
UserDto dto = new UserDto();
|
||||
dto.setId(user.getId());
|
||||
dto.setUsername(user.getUsername());
|
||||
dto.setEmail(user.getEmail());
|
||||
dto.setAvatar(user.getAvatar());
|
||||
dto.setRole(user.getRole().name());
|
||||
dto.setIntroduction(user.getIntroduction());
|
||||
dto.setFollowers(subscriptionService.countSubscribers(user.getUsername()));
|
||||
dto.setFollowing(subscriptionService.countSubscribed(user.getUsername()));
|
||||
dto.setCreatedAt(user.getCreatedAt());
|
||||
dto.setLastPostTime(postService.getLastPostTime(user.getUsername()));
|
||||
dto.setLastCommentTime(commentService.getLastCommentTimeOfUserByUserId(user.getId()));
|
||||
dto.setTotalViews(postService.getTotalViews(user.getUsername()));
|
||||
dto.setVisitedDays(userVisitService.countVisits(user.getUsername()));
|
||||
dto.setReadPosts(postReadService.countReads(user.getUsername()));
|
||||
dto.setLikesSent(reactionService.countLikesSent(user.getUsername()));
|
||||
dto.setLikesReceived(reactionService.countLikesReceived(user.getUsername()));
|
||||
dto.setExperience(user.getExperience());
|
||||
dto.setCurrentLevel(levelService.getLevel(user.getExperience()));
|
||||
dto.setNextLevelExp(levelService.nextLevelExp(user.getExperience()));
|
||||
if (viewer != null) {
|
||||
dto.setSubscribed(subscriptionService.isSubscribed(viewer.getName(), user.getUsername()));
|
||||
} else {
|
||||
dto.setSubscribed(false);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
public UserDto toDto(User user) {
|
||||
return toDto(user, null);
|
||||
}
|
||||
|
||||
public PostMetaDto toMetaDto(Post post) {
|
||||
PostMetaDto dto = new PostMetaDto();
|
||||
dto.setId(post.getId());
|
||||
dto.setTitle(post.getTitle());
|
||||
String content = post.getContent();
|
||||
if (content == null) {
|
||||
content = "";
|
||||
}
|
||||
if (snippetLength >= 0) {
|
||||
dto.setSnippet(content.length() > snippetLength ? content.substring(0, snippetLength) : content);
|
||||
} else {
|
||||
dto.setSnippet(content);
|
||||
}
|
||||
dto.setCreatedAt(post.getCreatedAt());
|
||||
dto.setCategory(post.getCategory().getName());
|
||||
dto.setViews(post.getViews());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public CommentInfoDto toCommentInfoDto(Comment comment) {
|
||||
CommentInfoDto dto = new CommentInfoDto();
|
||||
dto.setId(comment.getId());
|
||||
dto.setContent(comment.getContent());
|
||||
dto.setCreatedAt(comment.getCreatedAt());
|
||||
dto.setPost(toMetaDto(comment.getPost()));
|
||||
if (comment.getParent() != null) {
|
||||
ParentCommentDto pc = new ParentCommentDto();
|
||||
pc.setId(comment.getParent().getId());
|
||||
pc.setAuthor(comment.getParent().getAuthor().getUsername());
|
||||
pc.setContent(comment.getParent().getContent());
|
||||
dto.setParentComment(pc);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user