feat: 评论列表增加用户头像返回

This commit is contained in:
xiongxiaoyang 2022-05-22 10:07:50 +08:00
parent 5e01c1c5a1
commit 1b9627eddc
2 changed files with 6 additions and 2 deletions

View File

@ -35,6 +35,8 @@ public class BookCommentRespDto {
private Long commentUserId; private Long commentUserId;
private String commentUserPhoto;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime commentTime; private LocalDateTime commentTime;

View File

@ -26,6 +26,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -244,12 +245,13 @@ public class BookServiceImpl implements BookService {
// 查询评论用户信息并设置需要返回的评论用户名 // 查询评论用户信息并设置需要返回的评论用户名
List<Long> userIds = bookComments.stream().map(BookComment::getUserId).toList(); List<Long> userIds = bookComments.stream().map(BookComment::getUserId).toList();
List<UserInfo> userInfos = userDaoManager.listUsers(userIds); List<UserInfo> userInfos = userDaoManager.listUsers(userIds);
Map<Long, String> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getUsername)); Map<Long, UserInfo> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, Function.identity()));
List<BookCommentRespDto.CommentInfo> commentInfos = bookComments.stream() List<BookCommentRespDto.CommentInfo> commentInfos = bookComments.stream()
.map(v -> BookCommentRespDto.CommentInfo.builder() .map(v -> BookCommentRespDto.CommentInfo.builder()
.id(v.getId()) .id(v.getId())
.commentUserId(v.getUserId()) .commentUserId(v.getUserId())
.commentUser(userInfoMap.get(v.getUserId())) .commentUser(userInfoMap.get(v.getUserId()).getUsername())
.commentUserPhoto(userInfoMap.get(v.getUserId()).getUserPhoto())
.commentContent(v.getCommentContent()) .commentContent(v.getCommentContent())
.commentTime(v.getCreateTime()).build()).toList(); .commentTime(v.getCreateTime()).build()).toList();
bookCommentRespDto.setComments(commentInfos); bookCommentRespDto.setComments(commentInfos);