mirror of https://github.com/halo-dev/halo
Refactor sheet pageBy api
parent
227891f465
commit
56ff28cbbf
|
@ -5,6 +5,7 @@ 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.post.SheetListDTO;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.params.SheetParam;
|
||||
|
@ -38,8 +39,9 @@ public class SheetController {
|
|||
|
||||
@GetMapping
|
||||
@ApiOperation("Gets a page of sheet")
|
||||
public Page<Sheet> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
return sheetService.pageBy(pageable);
|
||||
public Page<SheetListDTO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||
Page<Sheet> sheetPage = sheetService.pageBy(pageable);
|
||||
return sheetService.convertToListDto(sheetPage);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package run.halo.app.service;
|
||||
|
||||
import run.halo.app.model.entity.SheetComment;
|
||||
import run.halo.app.service.base.CrudService;
|
||||
import run.halo.app.service.base.BaseCommentService;
|
||||
|
||||
/**
|
||||
* Sheet comment service interface.
|
||||
|
@ -9,6 +9,6 @@ import run.halo.app.service.base.CrudService;
|
|||
* @author johnniang
|
||||
* @date 19-4-24
|
||||
*/
|
||||
public interface SheetCommentService extends CrudService<SheetComment, Long> {
|
||||
public interface SheetCommentService extends BaseCommentService<SheetComment> {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import run.halo.app.model.entity.SheetComment;
|
||||
import run.halo.app.repository.PostRepository;
|
||||
import run.halo.app.repository.SheetCommentRepository;
|
||||
import run.halo.app.service.OptionService;
|
||||
import run.halo.app.service.SheetCommentService;
|
||||
import run.halo.app.service.base.AbstractCrudService;
|
||||
import run.halo.app.service.base.BaseCommentServiceImpl;
|
||||
|
||||
/**
|
||||
* Sheet comment service implementation.
|
||||
|
@ -13,12 +16,15 @@ import run.halo.app.service.base.AbstractCrudService;
|
|||
* @date 19-4-24
|
||||
*/
|
||||
@Service
|
||||
public class SheetCommentServiceImpl extends AbstractCrudService<SheetComment, Long> implements SheetCommentService {
|
||||
public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment> implements SheetCommentService {
|
||||
|
||||
private final SheetCommentRepository sheetCommentRepository;
|
||||
|
||||
public SheetCommentServiceImpl(SheetCommentRepository sheetCommentRepository) {
|
||||
super(sheetCommentRepository);
|
||||
public SheetCommentServiceImpl(SheetCommentRepository sheetCommentRepository,
|
||||
PostRepository postRepository,
|
||||
OptionService optionService,
|
||||
ApplicationEventPublisher eventPublisher) {
|
||||
super(sheetCommentRepository, postRepository, optionService, eventPublisher);
|
||||
this.sheetCommentRepository = sheetCommentRepository;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import run.halo.app.utils.MarkdownUtils;
|
|||
import run.halo.app.utils.ServiceUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -107,9 +108,13 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
|
|||
|
||||
Set<Integer> sheetIds = ServiceUtils.fetchProperty(sheets, Sheet::getId);
|
||||
|
||||
// key: sheet id, value: comment count
|
||||
Map<Integer, Long> sheetCommentCountMap = sheetCommentService.countByPostIds(sheetIds);
|
||||
|
||||
return sheetPage.map(sheet -> {
|
||||
return new SheetListDTO().convertFrom(sheet);
|
||||
SheetListDTO sheetListDTO = new SheetListDTO().convertFrom(sheet);
|
||||
sheetListDTO.setCommentCount(sheetCommentCountMap.getOrDefault(sheet.getId(), 0L));
|
||||
return sheetListDTO;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue