Refactor related page operations

pull/137/head
johnniang 2019-03-14 21:58:07 +08:00
parent e895725a9c
commit 06e18f3e8c
5 changed files with 27 additions and 33 deletions

View File

@ -7,6 +7,7 @@ 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;
import org.springframework.lang.NonNull;
/**
@ -18,23 +19,22 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecificationExecutor<Post> {
/**
* Find posts by status and type
* Finds posts by status and type.
*
* @param status status
* @param type type
* @param pageable pageable
*
* @return Page<Post>
*/
Page<Post> queryAllByStatusAndType(PostStatus status, PostType type, Pageable pageable);
@NonNull
Page<Post> findAllByStatusAndType(@NonNull PostStatus status, @NonNull PostType type, @NonNull Pageable pageable);
/**
* Count posts by status and type
* Counts posts by status and type.
*
* @param status status
* @param type type
*
* @return posts count
*/
Long countAllByStatusAndType(PostStatus status, PostType type);
long countByStatusAndType(@NonNull PostStatus status, @NonNull PostType type);
}

View File

@ -21,6 +21,7 @@ public class AdminAuthenticationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// TODO Handle admin authentication
filterChain.doFilter(request, response);
}
public void setFailureHandler(AuthenticationFailureHandler failureHandler) {

View File

@ -37,7 +37,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return Page<PostSimpleOutputDTO>
*/
@NonNull
Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable);
Page<PostSimpleOutputDTO> pageByStatusAndType(PostStatus status, PostType type, Pageable pageable);
/**
* Count posts by status and type
@ -46,5 +46,5 @@ public interface PostService extends CrudService<Post, Integer> {
* @param type type
* @return posts count
*/
Long countByStatus(PostStatus status, PostType type);
Long countByStatusAndType(PostStatus status, PostType type);
}

View File

@ -51,8 +51,8 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return Page<PostSimpleOutputDTO>
*/
@Override
public Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable) {
Page<Post> posts = postRepository.queryAllByStatusAndType(status, type, pageable);
public Page<PostSimpleOutputDTO> pageByStatusAndType(PostStatus status, PostType type, Pageable pageable) {
Page<Post> posts = postRepository.findAllByStatusAndType(status, type, pageable);
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
@ -65,7 +65,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return posts count
*/
@Override
public Long countByStatus(PostStatus status, PostType type) {
return postRepository.countAllByStatusAndType(status,type);
public Long countByStatusAndType(PostStatus status, PostType type) {
return postRepository.countByStatusAndType(status,type);
}
}

View File

@ -5,10 +5,8 @@ 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.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -36,28 +34,23 @@ public class PostController {
/**
* posts manage
*
* @param model model
* @param status post status
* @param page current page
* @param sort sort
*
* @param model model
* @param status post status
* @param pageable page info
* @return template path: admin/admin_post.ftl
*/
@GetMapping
public String posts(Model model,
@RequestParam(value = "status", defaultValue = "published") PostStatus status,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@SortDefault.SortDefaults({
@SortDefault(sort = "topPriority", direction = DESC),
@SortDefault(sort = "createTime", direction = DESC)
}) Sort sort) {
final Pageable pageable = PageRequest.of(page, 10, sort);
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable);
model.addAttribute("posts", posts);
model.addAttribute("publishedCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST));
model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST));
model.addAttribute("recycleCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
public String listPosts(Model model,
@PageableDefault(sort = {"topPriority", "createTime"}, direction = DESC) Pageable pageable,
@RequestParam(value = "status", defaultValue = "published") PostStatus status) {
final Page<PostSimpleOutputDTO> postPage = postService.pageByStatusAndType(status, PostType.POST, pageable);
model.addAttribute("posts", postPage.getContent());
model.addAttribute("publishedCount", postService.countByStatusAndType(PostStatus.PUBLISHED, PostType.POST));
model.addAttribute("draftCount", postService.countByStatusAndType(PostStatus.DRAFT, PostType.POST));
model.addAttribute("recycleCount", postService.countByStatusAndType(PostStatus.RECYCLE, PostType.POST));
model.addAttribute("status", status);
return "admin/admin_post";
}
}