posts manage

pull/137/head
ruibaby 2019-03-14 16:59:09 +08:00
parent 64a6dd9d2f
commit 79ac0b96fd
4 changed files with 124 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
@ -9,7 +11,28 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* Post repository.
*
* @author johnniang
* @author RYAN0UP
*/
public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecificationExecutor<Post> {
/**
* Find posts by status and type
*
* @param status status
* @param type type
* @param pageable pageable
*
* @return Page<Post>
*/
Page<Post> queryAllByStatusAndType(int status, Integer type, Pageable pageable);
/**
* Count posts by status and type
*
* @param status status
* @param type type
*
* @return posts count
*/
Long countAllByStatusAndType(int status, Integer type);
}

View File

@ -4,12 +4,14 @@ import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
/**
* Post service.
*
* @author johnniang
* @author RYAN0UP
*/
public interface PostService extends CrudService<Post, Integer> {
@ -17,9 +19,30 @@ public interface PostService extends CrudService<Post, Integer> {
* Lists latest posts.
*
* @param top top number must not be less than 0
*
* @return latest posts
*/
@NonNull
Page<PostSimpleOutputDTO> listLatest(int top);
/**
* List by status and type
*
* @param status status
* @param type type
* @param pageable pageable
*
* @return Page<PostSimpleOutputDTO>
*/
@NonNull
Page<PostSimpleOutputDTO> listByStatus(int status, Integer type, Pageable pageable);
/**
* Count posts by status and type
*
* @param status status
* @param type type
* @return posts count
*/
Long countByStatus(int status,Integer type);
}

View File

@ -7,6 +7,7 @@ import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
@ -15,6 +16,7 @@ import org.springframework.util.Assert;
* Post service implementation.
*
* @author johnniang
* @author RYAN0UP
*/
@Service
public class PostServiceImpl extends AbstractCrudService<Post, Integer> implements PostService {
@ -36,4 +38,32 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
/**
* List by status and type
*
* @param status status
* @param type type
* @param pageable pageable
*
* @return Page<PostSimpleOutputDTO>
*/
@Override
public Page<PostSimpleOutputDTO> listByStatus(int status, Integer type, Pageable pageable) {
Page<Post> posts = postRepository.queryAllByStatusAndType(status, type, pageable);
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
/**
* Count posts by status and type
*
* @param status status
* @param type type
*
* @return posts count
*/
@Override
public Long countByStatus(int status, Integer type) {
return postRepository.countAllByStatusAndType(status,type);
}
}

View File

@ -1,7 +1,21 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.service.PostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Posts controller
@ -12,4 +26,38 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/admin/posts")
public class PostController {
private PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
/**
* posts manage
*
* @param model model
* @param status post status
* @param page current page
* @param sort sort
*
* @return template path: admin/admin_post.ftl
*/
@GetMapping
public String posts(Model model,
@RequestParam(value = "status", defaultValue = "0") Integer status,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@SortDefault.SortDefaults({
@SortDefault(sort = "postPriority", direction = DESC),
@SortDefault(sort = "postDate", direction = DESC)
}) Sort sort) {
final Pageable pageable = PageRequest.of(page, 10, sort);
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST.getValue(), pageable);
model.addAttribute("posts", posts);
model.addAttribute("publishCount", postService.countByStatus(PostStatus.PUBLISHED.getValue(), PostType.POST.getValue()));
model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT.getValue(), PostType.POST.getValue()));
model.addAttribute("trashCount", postService.countByStatus(PostStatus.RECYCLE.getValue(), PostType.POST.getValue()));
model.addAttribute("status", status);
return "admin/admin_post";
}
}