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.domain.Pageable;
|
||||||
import org.springframework.data.web.PageableDefault;
|
import org.springframework.data.web.PageableDefault;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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.entity.Sheet;
|
||||||
import run.halo.app.model.enums.PostStatus;
|
import run.halo.app.model.enums.PostStatus;
|
||||||
import run.halo.app.model.params.SheetParam;
|
import run.halo.app.model.params.SheetParam;
|
||||||
|
@ -38,8 +39,9 @@ public class SheetController {
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ApiOperation("Gets a page of sheet")
|
@ApiOperation("Gets a page of sheet")
|
||||||
public Page<Sheet> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
public Page<SheetListDTO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
|
||||||
return sheetService.pageBy(pageable);
|
Page<Sheet> sheetPage = sheetService.pageBy(pageable);
|
||||||
|
return sheetService.convertToListDto(sheetPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package run.halo.app.service;
|
package run.halo.app.service;
|
||||||
|
|
||||||
import run.halo.app.model.entity.SheetComment;
|
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.
|
* Sheet comment service interface.
|
||||||
|
@ -9,6 +9,6 @@ import run.halo.app.service.base.CrudService;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 19-4-24
|
* @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;
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import run.halo.app.model.entity.SheetComment;
|
import run.halo.app.model.entity.SheetComment;
|
||||||
|
import run.halo.app.repository.PostRepository;
|
||||||
import run.halo.app.repository.SheetCommentRepository;
|
import run.halo.app.repository.SheetCommentRepository;
|
||||||
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.service.SheetCommentService;
|
import run.halo.app.service.SheetCommentService;
|
||||||
import run.halo.app.service.base.AbstractCrudService;
|
import run.halo.app.service.base.BaseCommentServiceImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sheet comment service implementation.
|
* Sheet comment service implementation.
|
||||||
|
@ -13,12 +16,15 @@ import run.halo.app.service.base.AbstractCrudService;
|
||||||
* @date 19-4-24
|
* @date 19-4-24
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class SheetCommentServiceImpl extends AbstractCrudService<SheetComment, Long> implements SheetCommentService {
|
public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment> implements SheetCommentService {
|
||||||
|
|
||||||
private final SheetCommentRepository sheetCommentRepository;
|
private final SheetCommentRepository sheetCommentRepository;
|
||||||
|
|
||||||
public SheetCommentServiceImpl(SheetCommentRepository sheetCommentRepository) {
|
public SheetCommentServiceImpl(SheetCommentRepository sheetCommentRepository,
|
||||||
super(sheetCommentRepository);
|
PostRepository postRepository,
|
||||||
|
OptionService optionService,
|
||||||
|
ApplicationEventPublisher eventPublisher) {
|
||||||
|
super(sheetCommentRepository, postRepository, optionService, eventPublisher);
|
||||||
this.sheetCommentRepository = sheetCommentRepository;
|
this.sheetCommentRepository = sheetCommentRepository;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import run.halo.app.utils.MarkdownUtils;
|
||||||
import run.halo.app.utils.ServiceUtils;
|
import run.halo.app.utils.ServiceUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -107,9 +108,13 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
|
||||||
|
|
||||||
Set<Integer> sheetIds = ServiceUtils.fetchProperty(sheets, Sheet::getId);
|
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 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