提交 d7aebebf 编写于 作者: J johnniang

Complete journal comment list api

上级 22195834
package run.halo.app.controller.admin.api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.params.CommentQuery;
import run.halo.app.model.params.JournalCommentParam;
import run.halo.app.model.vo.JournalCommentWithJournalVO;
import run.halo.app.service.JournalCommentService;
import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Journal comment controller.
*
......@@ -26,6 +32,20 @@ public class JournalCommentController {
this.journalCommentService = journalCommentService;
}
@GetMapping
@ApiOperation("Lists journal comments")
public Page<JournalCommentWithJournalVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
CommentQuery commentQuery) {
Page<JournalComment> journalCommentPage = journalCommentService.pageBy(commentQuery, pageable);
return journalCommentService.convertToWithJournalVo(journalCommentPage);
}
@GetMapping("latest")
public List<JournalCommentWithJournalVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
return journalCommentService.convertToWithJournalVo(journalCommentService.pageLatest(top).getContent());
}
@PostMapping
@ApiOperation("Creates a journal comment")
public BaseCommentDTO createCommentBy(@RequestBody JournalCommentParam journalCommentParam) {
......
......@@ -34,7 +34,7 @@ public class PostCommentController {
}
@GetMapping
@ApiOperation("Lists comments")
@ApiOperation("Lists post comments")
public Page<PostCommentWithPostVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
CommentQuery commentQuery) {
Page<PostComment> commentPage = postCommentService.pageBy(commentQuery, pageable);
......
package run.halo.app.model.vo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.dto.JournalDTO;
/**
* Journal comment with journal vo.
*
* @author johnniang
* @date 19-4-25
*/
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class JournalCommentWithJournalVO extends BaseCommentDTO {
private JournalDTO journal;
}
package run.halo.app.service;
import org.springframework.data.domain.Page;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.vo.JournalCommentWithJournalVO;
import run.halo.app.service.base.BaseCommentService;
import java.util.List;
/**
* Journal comment service interface.
*
......@@ -11,4 +17,9 @@ import run.halo.app.service.base.BaseCommentService;
*/
public interface JournalCommentService extends BaseCommentService<JournalComment> {
@NonNull
List<JournalCommentWithJournalVO> convertToWithJournalVo(@Nullable List<JournalComment> journalComments);
@NonNull
Page<JournalCommentWithJournalVO> convertToWithJournalVo(@NonNull Page<JournalComment> journalCommentPage);
}
package run.halo.app.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.JournalDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.vo.JournalCommentWithJournalVO;
import run.halo.app.repository.JournalCommentRepository;
import run.halo.app.repository.JournalRepository;
import run.halo.app.service.JournalCommentService;
import run.halo.app.service.OptionService;
import run.halo.app.utils.ServiceUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Journal comment service implementation.
......@@ -36,4 +50,39 @@ public class JournalCommentServiceImpl extends BaseCommentServiceImpl<JournalCom
throw new NotFoundException("The journal with id " + journalId + " was not found");
}
}
@Override
public List<JournalCommentWithJournalVO> convertToWithJournalVo(List<JournalComment> journalComments) {
if (CollectionUtil.isEmpty(journalComments)) {
return Collections.emptyList();
}
Set<Integer> journalIds = ServiceUtils.fetchProperty(journalComments, JournalComment::getPostId);
// Get all journals
List<Journal> journals = journalRepository.findAllById(journalIds);
Map<Integer, Journal> journalMap = ServiceUtils.convertToMap(journals, Journal::getId);
return journalComments.stream()
.filter(journalComment -> journalMap.containsKey(journalComment.getPostId()))
.map(journalComment -> {
JournalCommentWithJournalVO journalCmtWithJournalVo = new JournalCommentWithJournalVO().convertFrom(journalComment);
journalCmtWithJournalVo.setJournal(new JournalDTO().convertFrom(journalMap.get(journalComment.getPostId())));
return journalCmtWithJournalVo;
})
.collect(Collectors.toList());
}
@Override
public Page<JournalCommentWithJournalVO> convertToWithJournalVo(Page<JournalComment> journalCommentPage) {
Assert.notNull(journalCommentPage, "Journal comment page must not be null");
// Convert the list
List<JournalCommentWithJournalVO> journalCmtWithJournalVOS = convertToWithJournalVo(journalCommentPage.getContent());
// Build and return
return new PageImpl<>(journalCmtWithJournalVOS, journalCommentPage.getPageable(), journalCommentPage.getTotalElements());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册