release:专栏文章添加评论数量统计

release-2024-08-08
wangqing 4 months ago
parent cd6ac9a0ac
commit 820855dc2c

@ -3,6 +3,7 @@ package com.luoo.music.controller;
import api.PageResult;
import api.Result;
import com.luoo.music.dto.response.EssayRespDTO;
import com.luoo.music.pojo.Essay;
import com.luoo.music.service.EssayService;
import io.swagger.annotations.Api;
@ -11,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* author: wangqing
@ -34,7 +38,10 @@ public class EssayController {
}else{
pageData = essayService.getByCategory(category, page, size);
}
return Result.success(new PageResult<Essay>(pageData.getTotalElements(),pageData.getContent()));
List<EssayRespDTO> essayRespDTOS = pageData.stream().map(essay -> {
EssayRespDTO essayRespDTO = essayService.getJournalRespDTOWithComment(essay);return essayRespDTO;}).collect(Collectors.toList());
return Result.success(new PageResult<EssayRespDTO>(pageData.getTotalElements(),essayRespDTOS));
}
@ApiOperation("根据ID获取专栏文章")

@ -0,0 +1,44 @@
package com.luoo.music.dto.response;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Data
public class EssayRespDTO {
private String id;
// 标题
private String title;
// 简介
private String summary;
// 阅读数
private Integer readCount;
// 内容
private String content;
// 作者
private String authorId;
private String authorName;
private String authorAvatar;
// 分类
private Integer category;
// 封面路径
private String cover;
@CreatedDate
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@LastModifiedDate
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
private Integer totalCommentReplyCount;
}

@ -1,11 +1,21 @@
package com.luoo.music.service;
import com.luoo.music.client.UserClient;
import com.luoo.music.dao.CommentDao;
import com.luoo.music.dao.EssayDao;
import com.luoo.music.dao.TagDao;
import com.luoo.music.dto.mapper.CommentMapper;
import com.luoo.music.dto.response.CommentDTO;
import com.luoo.music.dto.response.EssayRespDTO;
import com.luoo.music.pojo.Essay;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
@ -19,6 +29,22 @@ public class EssayService {
@Autowired
private IdWorker idWorker;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private CacheChannel cacheChannel;
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private CommentDao commentDao;
@Autowired
private UserClient userClient;
public Page<Essay> search(int page, int size) {
// TODO: implement search logic
@ -28,6 +54,7 @@ public class EssayService {
public void add(Essay essay) {
essay.setId(idWorker.nextId()+"");
essay.setReadCount(0);
essayDao.save(essay);
}
@ -48,4 +75,14 @@ public class EssayService {
Pageable pageable = PageRequest.of(page-1, size);
return essayDao.findAllByCategoryOrderByCreateTimeDesc(category, pageable);
}
public EssayRespDTO getJournalRespDTOWithComment(Essay essay) {
CommentDTO commentDTO = CommentMapper.getCommentDTO(essay.getId(), mongoTemplate, commentDao,
redisTemplate,userClient,cacheChannel);
EssayRespDTO essayRespDTO = new EssayRespDTO();
BeanUtils.copyProperties(essay, essayRespDTO);
essayRespDTO.setTotalCommentReplyCount(commentDTO.getTotalCommentReplyInt());
return essayRespDTO;
}
}

Loading…
Cancel
Save