Code optimization

pull/137/head
ruibaby 2019-03-14 17:17:42 +08:00
parent 60472912fa
commit c9c68eef54
4 changed files with 17 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package cc.ryanc.halo.repository; package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.repository.base.BaseRepository; import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -24,7 +26,7 @@ public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecif
* *
* @return Page<Post> * @return Page<Post>
*/ */
Page<Post> queryAllByStatusAndType(int status, Integer type, Pageable pageable); Page<Post> queryAllByStatusAndType(PostStatus status, PostType type, Pageable pageable);
/** /**
* Count posts by status and type * Count posts by status and type
@ -34,5 +36,5 @@ public interface PostRepository extends BaseRepository<Post, Integer>, JpaSpecif
* *
* @return posts count * @return posts count
*/ */
Long countAllByStatusAndType(int status, Integer type); Long countAllByStatusAndType(PostStatus status, PostType type);
} }

View File

@ -2,6 +2,8 @@ package cc.ryanc.halo.service;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO; import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.service.base.CrudService; import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -35,7 +37,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return Page<PostSimpleOutputDTO> * @return Page<PostSimpleOutputDTO>
*/ */
@NonNull @NonNull
Page<PostSimpleOutputDTO> listByStatus(int status, Integer type, Pageable pageable); Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable);
/** /**
* Count posts by status and type * Count posts by status and type
@ -44,5 +46,5 @@ public interface PostService extends CrudService<Post, Integer> {
* @param type type * @param type type
* @return posts count * @return posts count
*/ */
Long countByStatus(int status,Integer type); Long countByStatus(PostStatus status, PostType type);
} }

View File

@ -2,6 +2,8 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO; import cc.ryanc.halo.model.dto.post.PostSimpleOutputDTO;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.repository.PostRepository; import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.base.AbstractCrudService; import cc.ryanc.halo.service.base.AbstractCrudService;
@ -49,7 +51,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return Page<PostSimpleOutputDTO> * @return Page<PostSimpleOutputDTO>
*/ */
@Override @Override
public Page<PostSimpleOutputDTO> listByStatus(int status, Integer type, Pageable pageable) { public Page<PostSimpleOutputDTO> listByStatus(PostStatus status, PostType type, Pageable pageable) {
Page<Post> posts = postRepository.queryAllByStatusAndType(status, type, pageable); Page<Post> posts = postRepository.queryAllByStatusAndType(status, type, pageable);
return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post)); return posts.map(post -> new PostSimpleOutputDTO().convertFrom(post));
} }
@ -63,7 +65,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
* @return posts count * @return posts count
*/ */
@Override @Override
public Long countByStatus(int status, Integer type) { public Long countByStatus(PostStatus status, PostType type) {
return postRepository.countAllByStatusAndType(status,type); return postRepository.countAllByStatusAndType(status,type);
} }
} }

View File

@ -45,18 +45,18 @@ public class PostController {
*/ */
@GetMapping @GetMapping
public String posts(Model model, public String posts(Model model,
@RequestParam(value = "status", defaultValue = "0") Integer status, @RequestParam(value = "status", defaultValue = "0") PostStatus status,
@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "page", defaultValue = "0") Integer page,
@SortDefault.SortDefaults({ @SortDefault.SortDefaults({
@SortDefault(sort = "postPriority", direction = DESC), @SortDefault(sort = "postPriority", direction = DESC),
@SortDefault(sort = "postDate", direction = DESC) @SortDefault(sort = "postDate", direction = DESC)
}) Sort sort) { }) Sort sort) {
final Pageable pageable = PageRequest.of(page, 10, sort); final Pageable pageable = PageRequest.of(page, 10, sort);
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST.getValue(), pageable); final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable);
model.addAttribute("posts", posts); model.addAttribute("posts", posts);
model.addAttribute("publishCount", postService.countByStatus(PostStatus.PUBLISHED.getValue(), PostType.POST.getValue())); model.addAttribute("publishCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST));
model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT.getValue(), PostType.POST.getValue())); model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST));
model.addAttribute("trashCount", postService.countByStatus(PostStatus.RECYCLE.getValue(), PostType.POST.getValue())); model.addAttribute("trashCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST));
model.addAttribute("status", status); model.addAttribute("status", status);
return "admin/admin_post"; return "admin/admin_post";
} }