Complete JournalService#convertToCmtCountDto

pull/146/head
johnniang 2019-04-25 22:44:42 +08:00
parent f541d470de
commit 2219583487
4 changed files with 74 additions and 27 deletions

View File

@ -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);
}
}

View File

@ -70,13 +70,6 @@ public class JournalController {
return journalService.convertTo(createdJournal); 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") @GetMapping("{journalId:\\d+}/comments/tree_view")
@ApiOperation("Lists comments with tree view") @ApiOperation("Lists comments with tree view")
public Page<BaseCommentVO> listCommentTree(@PathVariable("journalId") Integer journalId, public Page<BaseCommentVO> listCommentTree(@PathVariable("journalId") Integer journalId,

View File

@ -46,15 +46,6 @@ public interface JournalService extends CrudService<Journal, Integer> {
@NonNull @NonNull
JournalDTO convertTo(@NonNull Journal journal); 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. * Converts to journal with comment count dto list.
* *

View File

@ -1,19 +1,25 @@
package run.halo.app.service.impl; package run.halo.app.service.impl;
import cn.hutool.core.lang.Assert;
import org.springframework.data.domain.Page; 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.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.JournalDTO;
import run.halo.app.model.dto.JournalWithCmtCountDTO; import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal; import run.halo.app.model.entity.Journal;
import run.halo.app.model.params.JournalParam; import run.halo.app.model.params.JournalParam;
import run.halo.app.repository.JournalRepository; import run.halo.app.repository.JournalRepository;
import run.halo.app.service.JournalCommentService;
import run.halo.app.service.JournalService; import run.halo.app.service.JournalService;
import run.halo.app.service.base.AbstractCrudService; import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.ServiceUtils; import run.halo.app.utils.ServiceUtils;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/** /**
* Journal service implementation. * Journal service implementation.
@ -26,9 +32,13 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
private final JournalRepository journalRepository; private final JournalRepository journalRepository;
public JournalServiceImpl(JournalRepository journalRepository) { private final JournalCommentService journalCommentService;
public JournalServiceImpl(JournalRepository journalRepository,
JournalCommentService journalCommentService) {
super(journalRepository); super(journalRepository);
this.journalRepository = journalRepository; this.journalRepository = journalRepository;
this.journalCommentService = journalCommentService;
} }
@Override @Override
@ -50,20 +60,37 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
return new JournalDTO().convertFrom(journal); return new JournalDTO().convertFrom(journal);
} }
@Override
public JournalWithCmtCountDTO convertToCmtCountDto(Journal journal) {
return null;
}
@Override @Override
public List<JournalWithCmtCountDTO> convertToCmtCountDto(List<Journal> journals) { 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 @Override
public Page<JournalWithCmtCountDTO> convertToCmtCountDto(Page<Journal> journalPage) { 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());
} }