Create pageBy for journal service.

pull/296/head
ruibaby 2019-09-06 12:35:23 +08:00
parent 96c74369d6
commit 6564710453
4 changed files with 38 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import run.halo.app.model.entity.Journal; import run.halo.app.model.entity.Journal;
import run.halo.app.model.enums.JournalType;
import run.halo.app.service.JournalCommentService; import run.halo.app.service.JournalCommentService;
import run.halo.app.service.JournalService; import run.halo.app.service.JournalService;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
@ -70,15 +71,15 @@ public class ContentJournalController {
*/ */
@GetMapping(value = "page/{page}") @GetMapping(value = "page/{page}")
public String journals(Model model, public String journals(Model model,
@PathVariable(value = "page") Integer page, @PathVariable(value = "page") Integer page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) { @SortDefault(sort = "createTime", direction = DESC) Sort sort) {
log.debug("Requested journal page, sort info: [{}]", sort); log.debug("Requested journal page, sort info: [{}]", sort);
int pageSize = optionService.getPostPageSize(); int pageSize = optionService.getPostPageSize();
Pageable pageable = PageRequest.of(page >= 1 ? page - 1 : page, pageSize, sort); Pageable pageable = PageRequest.of(page >= 1 ? page - 1 : page, pageSize, sort);
Page<Journal> journals = journalService.listAll(pageable); Page<Journal> journals = journalService.pageBy(JournalType.PUBLIC, pageable);
int[] rainbow = PageUtil.rainbow(page, journals.getTotalPages(), 3); int[] rainbow = PageUtil.rainbow(page, journals.getTotalPages(), 3);

View File

@ -1,15 +1,29 @@
package run.halo.app.repository; package run.halo.app.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Journal; import run.halo.app.model.entity.Journal;
import run.halo.app.model.enums.JournalType;
import run.halo.app.repository.base.BaseRepository; import run.halo.app.repository.base.BaseRepository;
/** /**
* Journal repository. * Journal repository.
* *
* @author johnniang * @author johnniang
* @date 3/22/19 * @author ryanwang
* @date 2019-03-22
*/ */
public interface JournalRepository extends BaseRepository<Journal, Integer>, JpaSpecificationExecutor<Journal> { public interface JournalRepository extends BaseRepository<Journal, Integer>, JpaSpecificationExecutor<Journal> {
/**
* Finds journals by type and pageable.
*
* @param type journal type must not be null
* @param pageable page info must not be null
* @return a page of journal
*/
@NonNull
Page<Journal> findAllByType(@NonNull JournalType type, @NonNull Pageable pageable);
} }

View File

@ -7,6 +7,7 @@ import org.springframework.lang.Nullable;
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.enums.JournalType;
import run.halo.app.model.params.JournalParam; import run.halo.app.model.params.JournalParam;
import run.halo.app.model.params.JournalQuery; import run.halo.app.model.params.JournalQuery;
import run.halo.app.service.base.CrudService; import run.halo.app.service.base.CrudService;
@ -49,6 +50,16 @@ public interface JournalService extends CrudService<Journal, Integer> {
@NonNull @NonNull
Page<Journal> pageBy(@NonNull JournalQuery journalQuery, @NonNull Pageable pageable); Page<Journal> pageBy(@NonNull JournalQuery journalQuery, @NonNull Pageable pageable);
/**
* Lists by type.
*
* @param type journal type must not be null
* @param pageable page info must not be null
* @return a page of journal
*/
@NonNull
Page<Journal> pageBy(@NonNull JournalType type, @NonNull Pageable pageable);
/** /**
* Converts to journal dto. * Converts to journal dto.
* *

View File

@ -12,6 +12,7 @@ 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.enums.JournalType;
import run.halo.app.model.params.JournalParam; import run.halo.app.model.params.JournalParam;
import run.halo.app.model.params.JournalQuery; import run.halo.app.model.params.JournalQuery;
import run.halo.app.repository.JournalRepository; import run.halo.app.repository.JournalRepository;
@ -64,6 +65,13 @@ public class JournalServiceImpl extends AbstractCrudService<Journal, Integer> im
return journalRepository.findAll(buildSpecByQuery(journalQuery), pageable); return journalRepository.findAll(buildSpecByQuery(journalQuery), pageable);
} }
@Override
public Page<Journal> pageBy(JournalType type, Pageable pageable) {
Assert.notNull(type, "Journal type must not be null");
Assert.notNull(pageable, "Page info must not be null");
return journalRepository.findAllByType(type, pageable);
}
@Override @Override
public JournalDTO convertTo(Journal journal) { public JournalDTO convertTo(Journal journal) {
Assert.notNull(journal, "Journal must not be null"); Assert.notNull(journal, "Journal must not be null");