Search in sources :

Example 21 with Pageable

use of org.springframework.data.domain.Pageable in project halo by ruibaby.

the class IndexController method index.

/**
 * 首页分页
 *
 * @param model model
 * @param page page
 * @param size size
 * @return freemarker
 */
@GetMapping(value = "page/{page}")
public String index(Model model, @PathVariable(value = "page") Integer page) {
    Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    // 默认显示10条
    Integer size = 10;
    // 尝试加载设置选项,用于设置显示条数
    if (!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))) {
        size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
    }
    // 所有文章数据,分页
    Pageable pageable = new PageRequest(page - 1, size, sort);
    Page<Post> posts = postService.findPostByStatus(0, pageable);
    model.addAttribute("posts", posts);
    // 文章总数
    model.addAttribute("postsCount", postService.findAllPosts().size());
    model.addAttribute("is_home", true);
    // 用户信息
    User user = userService.findUser();
    model.addAttribute("user", user);
    // 所有分类目录
    List<Category> categories = categoryService.findAllCategories();
    model.addAttribute("categories", categories);
    // 菜单列表
    List<Menu> menus = menuService.findAllMenus();
    model.addAttribute("menus", menus);
    // 归档数据,包含[year,month,count,List<Post>]
    List<Archive> archives = postService.findPostGroupByPostDate();
    model.addAttribute("archives", archives);
    // 设置选项
    model.addAttribute("options", HaloConst.OPTIONS);
    return this.render("index");
}
Also used : Archive(cc.ryanc.halo.model.dto.Archive) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort)

Example 22 with Pageable

use of org.springframework.data.domain.Pageable in project halo by ruibaby.

the class IndexController method feed.

/**
 * 获取文章rss
 *
 * @return rss
 */
@GetMapping(value = { "feed", "feed.xml", "atom.xml" }, produces = { MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_RSS_XML_VALUE })
@ResponseBody
public String feed() {
    String rssPosts = HaloConst.OPTIONS.get("rss_posts");
    if (StringUtils.isBlank(rssPosts)) {
        rssPosts = "20";
    }
    // 获取文章列表并根据时间排序
    Sort sort = new Sort(Sort.Direction.DESC, "postDate");
    Pageable pageable = new PageRequest(0, Integer.parseInt(rssPosts), sort);
    Page<Post> postsPage = postService.findPostByStatus(0, pageable);
    List<Post> posts = postsPage.getContent();
    return postService.buildRss(posts);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort)

Example 23 with Pageable

use of org.springframework.data.domain.Pageable in project halo by ruibaby.

the class IndexController method archives.

/**
 * 文章归档,根据年月
 *
 * @param model model
 * @param year year
 * @param month month
 * @return string
 */
@GetMapping(value = "/archives/{year}/{month}")
public String archives(Model model, @PathVariable(value = "year") String year, @PathVariable(value = "month") String month) {
    log.info(year);
    log.info(month);
    // 根据年月查出的文章数据,分页
    Sort sort = new Sort(Sort.Direction.DESC, "post_date");
    Pageable pageable = new PageRequest(0, 5, sort);
    Page<Post> posts = postService.findPostByYearAndMonth(year, month, pageable);
    model.addAttribute("posts", posts);
    // 文章总数
    model.addAttribute("postsCount", postService.findAllPosts().size());
    // 用户信息
    User user = userService.findUser();
    model.addAttribute("user", user);
    // 分类目录
    List<Category> categories = categoryService.findAllCategories();
    model.addAttribute("categories", categories);
    // 菜单列表
    List<Menu> menus = menuService.findAllMenus();
    model.addAttribute("menus", menus);
    // 归档数据,包含[year,month,count,List<Post>]
    List<Archive> archives = postService.findPostGroupByPostDate();
    model.addAttribute("archives", archives);
    // 是否是归档页,用于判断输出链接
    model.addAttribute("isArchives", "true");
    // 设置选项
    model.addAttribute("options", HaloConst.OPTIONS);
    return this.render("archives");
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Archive(cc.ryanc.halo.model.dto.Archive) Sort(org.springframework.data.domain.Sort)

Example 24 with Pageable

use of org.springframework.data.domain.Pageable in project halo by ruibaby.

the class AttachmentController method attachments.

/**
 * 获取upload的所有图片资源并渲染页面
 *
 * @param model model
 * @return String
 */
@GetMapping
public String attachments(Model model, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "18") Integer size) {
    Sort sort = new Sort(Sort.Direction.DESC, "attachId");
    Pageable pageable = new PageRequest(page, size, sort);
    Page<Attachment> attachments = attachmentService.findAllAttachments(pageable);
    model.addAttribute("attachments", attachments);
    // 设置选项
    model.addAttribute("options", HaloConst.OPTIONS);
    return "admin/admin_attachment";
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Attachment(cc.ryanc.halo.model.domain.Attachment)

Example 25 with Pageable

use of org.springframework.data.domain.Pageable in project halo by ruibaby.

the class PostController method searchPost.

/**
 * 模糊查询文章
 *
 * @param model Model
 * @param keyword keyword
 * @param page page
 * @param size size
 * @return freemarker
 */
@PostMapping(value = "/search")
public String searchPost(Model model, @RequestParam(value = "keyword") String keyword, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
    try {
        // 排序规则
        Sort sort = new Sort(Sort.Direction.DESC, "postId");
        Pageable pageable = new PageRequest(page, size, sort);
        model.addAttribute("posts", postService.searchPosts(keyword, pageable));
    } catch (Exception e) {
        log.error("未知错误:" + e.getMessage());
    }
    return "admin/admin_post";
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort)

Aggregations

Pageable (org.springframework.data.domain.Pageable)185 PageRequest (org.springframework.data.domain.PageRequest)88 Sort (org.springframework.data.domain.Sort)81 Test (org.junit.Test)39 PageImpl (org.springframework.data.domain.PageImpl)28 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)17 List (java.util.List)15 Page (org.springframework.data.domain.Page)15 Autowired (org.springframework.beans.factory.annotation.Autowired)14 GetMapping (org.springframework.web.bind.annotation.GetMapping)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Test (org.junit.jupiter.api.Test)11 UUID (java.util.UUID)10 ApiOperation (io.swagger.annotations.ApiOperation)9 Calendar (java.util.Calendar)9 java.util (java.util)8 Lists (com.google.common.collect.Lists)7 Map (java.util.Map)7 Transactional (org.springframework.transaction.annotation.Transactional)6