mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-05 09:27:34 +08:00
fix: test commit
This commit is contained in:
@@ -1,122 +1,161 @@
|
|||||||
package com.openisle.mapper;
|
package com.openisle.mapper;
|
||||||
|
|
||||||
|
import com.openisle.dto.AuthorDto;
|
||||||
import com.openisle.dto.CommentDto;
|
import com.openisle.dto.CommentDto;
|
||||||
|
import com.openisle.dto.LotteryDto;
|
||||||
|
import com.openisle.dto.PollDto;
|
||||||
import com.openisle.dto.PostDetailDto;
|
import com.openisle.dto.PostDetailDto;
|
||||||
import com.openisle.dto.PostSummaryDto;
|
import com.openisle.dto.PostSummaryDto;
|
||||||
import com.openisle.dto.ReactionDto;
|
import com.openisle.dto.ReactionDto;
|
||||||
import com.openisle.dto.LotteryDto;
|
|
||||||
import com.openisle.dto.PollDto;
|
|
||||||
import com.openisle.dto.AuthorDto;
|
|
||||||
import com.openisle.model.CommentSort;
|
import com.openisle.model.CommentSort;
|
||||||
import com.openisle.model.Post;
|
|
||||||
import com.openisle.model.LotteryPost;
|
import com.openisle.model.LotteryPost;
|
||||||
import com.openisle.model.PollPost;
|
import com.openisle.model.PollPost;
|
||||||
import com.openisle.model.User;
|
|
||||||
import com.openisle.model.PollVote;
|
import com.openisle.model.PollVote;
|
||||||
|
import com.openisle.model.Post;
|
||||||
|
import com.openisle.model.User;
|
||||||
|
import com.openisle.repository.PollVoteRepository;
|
||||||
import com.openisle.service.CommentService;
|
import com.openisle.service.CommentService;
|
||||||
import com.openisle.service.ReactionService;
|
import com.openisle.service.ReactionService;
|
||||||
import com.openisle.service.SubscriptionService;
|
import com.openisle.service.SubscriptionService;
|
||||||
import com.openisle.repository.PollVoteRepository;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/** Mapper responsible for converting posts into DTOs. */
|
/** Mapper responsible for converting posts into DTOs. */
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PostMapper {
|
public class PostMapper {
|
||||||
|
|
||||||
private final CommentService commentService;
|
private final CommentService commentService;
|
||||||
private final ReactionService reactionService;
|
private final ReactionService reactionService;
|
||||||
private final SubscriptionService subscriptionService;
|
private final SubscriptionService subscriptionService;
|
||||||
private final CommentMapper commentMapper;
|
private final CommentMapper commentMapper;
|
||||||
private final ReactionMapper reactionMapper;
|
private final ReactionMapper reactionMapper;
|
||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
private final TagMapper tagMapper;
|
private final TagMapper tagMapper;
|
||||||
private final CategoryMapper categoryMapper;
|
private final CategoryMapper categoryMapper;
|
||||||
private final PollVoteRepository pollVoteRepository;
|
private final PollVoteRepository pollVoteRepository;
|
||||||
|
|
||||||
public PostSummaryDto toSummaryDto(Post post) {
|
public PostSummaryDto toSummaryDto(Post post) {
|
||||||
PostSummaryDto dto = new PostSummaryDto();
|
PostSummaryDto dto = new PostSummaryDto();
|
||||||
applyCommon(post, dto);
|
applyCommon(post, dto);
|
||||||
return dto;
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostDetailDto toDetailDto(Post post, String viewer) {
|
||||||
|
PostDetailDto dto = new PostDetailDto();
|
||||||
|
applyCommon(post, dto);
|
||||||
|
List<CommentDto> comments = commentService
|
||||||
|
.getCommentsForPost(post.getId(), CommentSort.OLDEST)
|
||||||
|
.stream()
|
||||||
|
.map(commentMapper::toDtoWithReplies)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
dto.setComments(comments);
|
||||||
|
dto.setSubscribed(
|
||||||
|
viewer != null &&
|
||||||
|
subscriptionService.isPostSubscribed(viewer, post.getId())
|
||||||
|
);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void applyCommon(Post post, PostSummaryDto dto) {
|
||||||
|
dto.setId(post.getId());
|
||||||
|
dto.setTitle(post.getTitle());
|
||||||
|
dto.setContent(post.getContent());
|
||||||
|
|
||||||
|
dto.setCreatedAt(post.getCreatedAt());
|
||||||
|
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());
|
||||||
|
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
|
||||||
|
dto.setClosed(post.isClosed());
|
||||||
|
|
||||||
|
List<ReactionDto> reactions = reactionService
|
||||||
|
.getReactionsForPost(post.getId())
|
||||||
|
.stream()
|
||||||
|
.map(reactionMapper::toDto)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
dto.setReactions(reactions);
|
||||||
|
|
||||||
|
List<User> participants = commentService.getParticipants(post.getId(), 5);
|
||||||
|
dto.setParticipants(
|
||||||
|
participants
|
||||||
|
.stream()
|
||||||
|
.map(userMapper::toAuthorDto)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
|
|
||||||
|
LocalDateTime last = post.getLastReplyAt();
|
||||||
|
if (last == null) {
|
||||||
|
commentService.updatePostCommentStats(post);
|
||||||
|
}
|
||||||
|
dto.setCommentCount(post.getCommentCount());
|
||||||
|
dto.setLastReplyAt(post.getLastReplyAt());
|
||||||
|
dto.setReward(0);
|
||||||
|
dto.setSubscribed(false);
|
||||||
|
dto.setType(post.getType());
|
||||||
|
|
||||||
|
if (post instanceof LotteryPost lp) {
|
||||||
|
LotteryDto l = new LotteryDto();
|
||||||
|
l.setPrizeDescription(lp.getPrizeDescription());
|
||||||
|
l.setPrizeIcon(lp.getPrizeIcon());
|
||||||
|
l.setPrizeCount(lp.getPrizeCount());
|
||||||
|
l.setPointCost(lp.getPointCost());
|
||||||
|
l.setStartTime(lp.getStartTime());
|
||||||
|
l.setEndTime(lp.getEndTime());
|
||||||
|
l.setParticipants(
|
||||||
|
lp
|
||||||
|
.getParticipants()
|
||||||
|
.stream()
|
||||||
|
.map(userMapper::toAuthorDto)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
|
l.setWinners(
|
||||||
|
lp
|
||||||
|
.getWinners()
|
||||||
|
.stream()
|
||||||
|
.map(userMapper::toAuthorDto)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
|
dto.setLottery(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PostDetailDto toDetailDto(Post post, String viewer) {
|
if (post instanceof PollPost pp) {
|
||||||
PostDetailDto dto = new PostDetailDto();
|
PollDto p = new PollDto();
|
||||||
applyCommon(post, dto);
|
p.setOptions(pp.getOptions());
|
||||||
List<CommentDto> comments = commentService.getCommentsForPost(post.getId(), CommentSort.OLDEST)
|
p.setVotes(pp.getVotes());
|
||||||
.stream()
|
p.setEndTime(pp.getEndTime());
|
||||||
.map(commentMapper::toDtoWithReplies)
|
p.setParticipants(
|
||||||
.collect(Collectors.toList());
|
pp
|
||||||
dto.setComments(comments);
|
.getParticipants()
|
||||||
dto.setSubscribed(viewer != null && subscriptionService.isPostSubscribed(viewer, post.getId()));
|
.stream()
|
||||||
return dto;
|
.map(userMapper::toAuthorDto)
|
||||||
}
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
private void applyCommon(Post post, PostSummaryDto dto) {
|
Map<Integer, List<AuthorDto>> optionParticipants = pollVoteRepository
|
||||||
dto.setId(post.getId());
|
.findByPostId(pp.getId())
|
||||||
dto.setTitle(post.getTitle());
|
.stream()
|
||||||
dto.setContent(post.getContent());
|
.collect(
|
||||||
dto.setCreatedAt(post.getCreatedAt());
|
Collectors.groupingBy(
|
||||||
dto.setAuthor(userMapper.toAuthorDto(post.getAuthor()));
|
PollVote::getOptionIndex,
|
||||||
dto.setCategory(categoryMapper.toDto(post.getCategory()));
|
Collectors.mapping(
|
||||||
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
|
v -> userMapper.toAuthorDto(v.getUser()),
|
||||||
dto.setViews(post.getViews());
|
Collectors.toList()
|
||||||
dto.setStatus(post.getStatus());
|
)
|
||||||
dto.setPinnedAt(post.getPinnedAt());
|
)
|
||||||
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
|
);
|
||||||
dto.setClosed(post.isClosed());
|
p.setOptionParticipants(optionParticipants);
|
||||||
|
p.setMultiple(Boolean.TRUE.equals(pp.getMultiple()));
|
||||||
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
|
dto.setPoll(p);
|
||||||
.stream()
|
|
||||||
.map(reactionMapper::toDto)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
dto.setReactions(reactions);
|
|
||||||
|
|
||||||
List<User> participants = commentService.getParticipants(post.getId(), 5);
|
|
||||||
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
|
||||||
|
|
||||||
LocalDateTime last = post.getLastReplyAt();
|
|
||||||
if (last == null) {
|
|
||||||
commentService.updatePostCommentStats(post);
|
|
||||||
}
|
|
||||||
dto.setCommentCount(post.getCommentCount());
|
|
||||||
dto.setLastReplyAt(post.getLastReplyAt());
|
|
||||||
dto.setReward(0);
|
|
||||||
dto.setSubscribed(false);
|
|
||||||
dto.setType(post.getType());
|
|
||||||
|
|
||||||
if (post instanceof LotteryPost lp) {
|
|
||||||
LotteryDto l = new LotteryDto();
|
|
||||||
l.setPrizeDescription(lp.getPrizeDescription());
|
|
||||||
l.setPrizeIcon(lp.getPrizeIcon());
|
|
||||||
l.setPrizeCount(lp.getPrizeCount());
|
|
||||||
l.setPointCost(lp.getPointCost());
|
|
||||||
l.setStartTime(lp.getStartTime());
|
|
||||||
l.setEndTime(lp.getEndTime());
|
|
||||||
l.setParticipants(lp.getParticipants().stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
|
||||||
l.setWinners(lp.getWinners().stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
|
||||||
dto.setLottery(l);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (post instanceof PollPost pp) {
|
|
||||||
PollDto p = new PollDto();
|
|
||||||
p.setOptions(pp.getOptions());
|
|
||||||
p.setVotes(pp.getVotes());
|
|
||||||
p.setEndTime(pp.getEndTime());
|
|
||||||
p.setParticipants(pp.getParticipants().stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
|
||||||
Map<Integer, List<AuthorDto>> optionParticipants = pollVoteRepository.findByPostId(pp.getId()).stream()
|
|
||||||
.collect(Collectors.groupingBy(PollVote::getOptionIndex,
|
|
||||||
Collectors.mapping(v -> userMapper.toAuthorDto(v.getUser()), Collectors.toList())));
|
|
||||||
p.setOptionParticipants(optionParticipants);
|
|
||||||
p.setMultiple(Boolean.TRUE.equals(pp.getMultiple()));
|
|
||||||
dto.setPoll(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
--header-border-color: lightgray;
|
--header-border-color: lightgray;
|
||||||
--header-text-color: black;
|
--header-text-color: black;
|
||||||
--blur-1: blur(1px);
|
--blur-1: blur(1px);
|
||||||
|
|
||||||
--blur-2: blur(2px);
|
--blur-2: blur(2px);
|
||||||
--blur-4: blur(4px);
|
--blur-4: blur(4px);
|
||||||
--blur-5: blur(5px);
|
--blur-5: blur(5px);
|
||||||
|
|||||||
Reference in New Issue
Block a user