mirror of https://github.com/halo-dev/halo
Complete JournalService#convertToCmtCountDto
parent
f541d470de
commit
2219583487
|
@ -0,0 +1,36 @@
|
|||
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 run.halo.app.model.dto.BaseCommentDTO;
|
||||
import run.halo.app.model.entity.JournalComment;
|
||||
import run.halo.app.model.params.JournalCommentParam;
|
||||
import run.halo.app.service.JournalCommentService;
|
||||
|
||||
/**
|
||||
* Journal comment controller.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 19-4-25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/journals/comments")
|
||||
public class JournalCommentController {
|
||||
|
||||
private final JournalCommentService journalCommentService;
|
||||
|
||||
public JournalCommentController(JournalCommentService journalCommentService) {
|
||||
this.journalCommentService = journalCommentService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation("Creates a journal comment")
|
||||
public BaseCommentDTO createCommentBy(@RequestBody JournalCommentParam journalCommentParam) {
|
||||
JournalComment journalComment = journalCommentService.createBy(journalCommentParam);
|
||||
return journalCommentService.convertTo(journalComment);
|
||||
}
|
||||
|
||||
}
|
|
@ -70,13 +70,6 @@ public class JournalController {
|
|||
return journalService.convertTo(createdJournal);
|
||||
}
|
||||
|
||||
@PostMapping("comments")
|
||||
@ApiOperation("Create a journal comment")
|
||||
public BaseCommentDTO createCommentBy(@RequestBody JournalCommentParam journalCommentParam) {
|
||||
JournalComment journalComment = journalCommentService.createBy(journalCommentParam);
|
||||
return journalCommentService.convertTo(journalComment);
|
||||
}
|
||||
|
||||
@GetMapping("{journalId:\\d+}/comments/tree_view")
|
||||
@ApiOperation("Lists comments with tree view")
|
||||
public Page<BaseCommentVO> listCommentTree(@PathVariable("journalId") Integer journalId,
|
||||
|
|
|
@ -46,15 +46,6 @@ public interface JournalService extends CrudService<Journal, Integer> {
|
|||
@NonNull
|
||||
JournalDTO convertTo(@NonNull Journal journal);
|
||||
|
||||
/**
|
||||
* Converts to journal with comment count dto.
|
||||
*
|
||||
* @param journal journal must not be null
|
||||
* @return journal with comment count dto
|
||||
*/
|
||||
@NonNull
|
||||
JournalWithCmtCountDTO convertToCmtCountDto(@NonNull Journal journal);
|
||||
|
||||
/**
|
||||
* Converts to journal with comment count dto list.
|
||||
*
|
||||
|
|
|
@ -1,19 +1,25 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.model.dto.JournalDTO;
|
||||
import run.halo.app.model.dto.JournalWithCmtCountDTO;
|
||||
import run.halo.app.model.entity.Journal;
|
||||
import run.halo.app.model.params.JournalParam;
|
||||
import run.halo.app.repository.JournalRepository;
|
||||
import run.halo.app.service.JournalCommentService;
|
||||
import run.halo.app.service.JournalService;
|
||||
import run.halo.app.service.base.AbstractCrudService;
|
||||
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 service implementation.
|
||||
|
@ -26,9 +32,13 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
|
|||
|
||||
private final JournalRepository journalRepository;
|
||||
|
||||
public JournalServiceImpl(JournalRepository journalRepository) {
|
||||
private final JournalCommentService journalCommentService;
|
||||
|
||||
public JournalServiceImpl(JournalRepository journalRepository,
|
||||
JournalCommentService journalCommentService) {
|
||||
super(journalRepository);
|
||||
this.journalRepository = journalRepository;
|
||||
this.journalCommentService = journalCommentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,20 +60,37 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
|
|||
return new JournalDTO().convertFrom(journal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JournalWithCmtCountDTO convertToCmtCountDto(Journal journal) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JournalWithCmtCountDTO> convertToCmtCountDto(List<Journal> journals) {
|
||||
return null;
|
||||
if (CollectionUtils.isEmpty(journals)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// Get journal ids
|
||||
Set<Integer> journalIds = ServiceUtils.fetchProperty(journals, Journal::getId);
|
||||
|
||||
// Get comment count map
|
||||
Map<Integer, Long> journalCommentCountMap = journalCommentService.countByPostIds(journalIds);
|
||||
|
||||
return journals.stream()
|
||||
.map(journal -> {
|
||||
JournalWithCmtCountDTO journalWithCmtCountDTO = new JournalWithCmtCountDTO().convertFrom(journal);
|
||||
// Set comment count
|
||||
journalWithCmtCountDTO.setCommentCount(journalCommentCountMap.getOrDefault(journal.getId(), 0L));
|
||||
return journalWithCmtCountDTO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<JournalWithCmtCountDTO> convertToCmtCountDto(Page<Journal> journalPage) {
|
||||
return null;
|
||||
Assert.notNull(journalPage, "Journal page must not be null");
|
||||
|
||||
// Convert
|
||||
List<JournalWithCmtCountDTO> journalWithCmtCountDTOS = convertToCmtCountDto(journalPage.getContent());
|
||||
|
||||
// Build and return
|
||||
return new PageImpl<>(journalWithCmtCountDTOS, journalPage.getPageable(), journalPage.getTotalElements());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue