Refactor PostService

pull/98/head
johnniang 2019-02-20 11:16:17 +08:00
parent f2c6ab43f0
commit 6687db022c
12 changed files with 44 additions and 70 deletions

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
@ -22,23 +23,7 @@ import java.util.Optional;
* @author : RYAN0UP
* @date : 2017/11/14
*/
public interface PostService {
/**
*
*
* @param post Post
* @return Post
*/
Post save(Post post);
/**
*
*
* @param postId postId
* @return Post
*/
Post remove(Long postId);
public interface PostService extends CrudService<Post, Long> {
/**
*
@ -104,13 +89,6 @@ public interface PostService {
*/
List<Post> findPostByStatus(Integer status, String postType);
/**
*
*
* @param postId postId
* @return Post
*/
Optional<Post> findByPostId(Long postId);
/**
*

View File

@ -8,6 +8,7 @@ import cc.ryanc.halo.utils.ServiceUtils;
import cn.hutool.core.util.StrUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
@ -41,7 +42,7 @@ public class OptionsServiceImpl extends AbstractCrudService<Options, String> imp
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public void saveOptions(Map<String, String> options) {
if (null != options && !options.isEmpty()) {
if (!CollectionUtils.isEmpty(options)) {
options.forEach(this::saveOption);
}
}

View File

@ -11,9 +11,9 @@ import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
@ -41,20 +41,27 @@ import static cc.ryanc.halo.model.dto.HaloConst.POSTS_VIEWS;
* @date : 2017/11/14
*/
@Service
public class PostServiceImpl implements PostService {
public class PostServiceImpl extends AbstractCrudService<Post, Long> implements PostService {
private static final String POSTS_CACHE_NAME = "posts";
private static final String COMMENTS_CACHE_NAME = "comments";
@Autowired
private PostRepository postRepository;
private final PostRepository postRepository;
@Autowired
private CategoryService categoryService;
private final CategoryService categoryService;
private final TagService tagService;
public PostServiceImpl(PostRepository postRepository,
CategoryService categoryService,
TagService tagService) {
super(postRepository);
this.postRepository = postRepository;
this.categoryService = categoryService;
this.tagService = tagService;
}
@Autowired
private TagService tagService;
/**
*
@ -64,7 +71,7 @@ public class PostServiceImpl implements PostService {
*/
@Override
@CacheEvict(value = {POSTS_CACHE_NAME, COMMENTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public Post save(Post post) {
public Post create(Post post) {
int postSummary = 50;
if (StrUtil.isNotEmpty(OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()))) {
postSummary = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.POST_SUMMARY.getProp()));
@ -76,7 +83,7 @@ public class PostServiceImpl implements PostService {
} else {
post.setPostSummary(summaryText);
}
return postRepository.save(post);
return super.create(post);
}
/**
@ -87,8 +94,8 @@ public class PostServiceImpl implements PostService {
*/
@Override
@CacheEvict(value = {POSTS_CACHE_NAME, COMMENTS_CACHE_NAME}, allEntries = true, beforeInvocation = true)
public Post remove(Long postId) {
final Optional<Post> post = this.findByPostId(postId);
public Post removeById(Long postId) {
final Optional<Post> post = fetchById(postId);
postRepository.delete(post.get());
return post.get();
}
@ -103,7 +110,7 @@ public class PostServiceImpl implements PostService {
@Override
@CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true)
public Post updatePostStatus(Long postId, Integer status) {
final Optional<Post> post = this.findByPostId(postId);
final Optional<Post> post = fetchById(postId);
post.get().setPostStatus(status);
return postRepository.save(post.get());
}
@ -199,17 +206,6 @@ public class PostServiceImpl implements PostService {
return postRepository.findPostsByPostStatusAndPostType(status, postType);
}
/**
*
*
* @param postId postId
* @return Optional
*/
@Override
public Optional<Post> findByPostId(Long postId) {
return postRepository.findById(postId);
}
/**
*
*

View File

@ -19,13 +19,12 @@ public class PostSyncTask {
*/
public void postSync() {
final PostService postService = SpringUtil.getBean(PostService.class);
Post post = null;
int count = 0;
for (Long key : POSTS_VIEWS.keySet()) {
post = postService.findByPostId(key).orElse(null);
Post post = postService.getNullableById(key);
if (null != post) {
post.setPostViews(post.getPostViews() + POSTS_VIEWS.get(key));
postService.save(post);
postService.create(post);
count++;
}
}

View File

@ -341,7 +341,7 @@ public class AdminController extends BaseController {
if (null == post.getPostUpdate()) {
post.setPostUpdate(new Date());
}
postService.save(post);
postService.create(post);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode());
}
}

View File

@ -152,7 +152,7 @@ public class CommentController extends BaseController {
HttpServletRequest request,
HttpSession session) {
try {
final Post post = postService.findByPostId(postId).orElse(new Post());
final Post post = postService.fetchById(postId).orElse(new Post());
//博主信息
final User user = (User) session.getAttribute(USER_SESSION_KEY);

View File

@ -243,7 +243,7 @@ public class PageController {
post.setUser(user);
post.setPostType(PostTypeEnum.POST_TYPE_PAGE.getDesc());
if (null != post.getPostId()) {
final Post oldPost = postService.findByPostId(post.getPostId()).get();
final Post oldPost = postService.fetchById(post.getPostId()).get();
if (null == post.getPostDate()) {
post.setPostDate(DateUtil.date());
}
@ -255,7 +255,7 @@ public class PageController {
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
}
postService.save(post);
postService.create(post);
logsService.save(LogsRecord.PUSH_PAGE, post.getPostTitle(), request);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
} catch (Exception e) {
@ -273,7 +273,7 @@ public class PageController {
*/
@GetMapping(value = "/edit")
public String editPage(@RequestParam("pageId") Long pageId, Model model) {
final Optional<Post> post = postService.findByPostId(pageId);
final Optional<Post> post = postService.fetchById(pageId);
final List<String> customTpls = HaloUtils.getCustomTpl(OPTIONS.get(BlogPropertiesEnum.THEME.getProp()));
model.addAttribute("post", post.orElse(new Post()));
model.addAttribute("customTpls", customTpls);

View File

@ -118,7 +118,7 @@ public class PostController extends BaseController {
*/
@GetMapping(value = "/edit")
public String editPost(@RequestParam("postId") Long postId, Model model) {
final Optional<Post> post = postService.findByPostId(postId);
final Optional<Post> post = postService.fetchById(postId);
model.addAttribute("post", post.orElse(new Post()));
return "admin/admin_post_edit";
}
@ -152,7 +152,7 @@ public class PostController extends BaseController {
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
}
postService.save(post);
postService.create(post);
logsService.save(LogsRecord.PUSH_POST, post.getPostTitle(), request);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.save-success"));
} catch (Exception e) {
@ -176,7 +176,7 @@ public class PostController extends BaseController {
@RequestParam("cateList") List<String> cateList,
@RequestParam("tagList") String tagList) {
//old data
final Post oldPost = postService.findByPostId(post.getPostId()).orElse(new Post());
final Post oldPost = postService.fetchById(post.getPostId()).orElse(new Post());
post.setPostViews(oldPost.getPostViews());
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
post.setUser(oldPost.getUser());
@ -191,7 +191,7 @@ public class PostController extends BaseController {
if (StrUtil.equals(post.getPostThumbnail(), BlogPropertiesEnum.DEFAULT_THUMBNAIL.getProp())) {
post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
}
post = postService.save(post);
post = postService.create(post);
if (null != post) {
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.common.update-success"));
} else {
@ -243,8 +243,8 @@ public class PostController extends BaseController {
@GetMapping(value = "/remove")
public String removePost(@RequestParam("postId") Long postId, @RequestParam("postType") String postType) {
try {
final Optional<Post> post = postService.findByPostId(postId);
postService.remove(postId);
final Optional<Post> post = postService.fetchById(postId);
postService.removeById(postId);
logsService.save(LogsRecord.REMOVE_POST, post.get().getPostTitle(), request);
} catch (Exception e) {
log.error("Delete article failed: {}", e.getMessage());

View File

@ -66,7 +66,7 @@ public class ApiCommentController {
}
try {
Comment lastComment = null;
final Post post = postService.findByPostId(postId).orElse(new Post());
final Post post = postService.fetchById(postId).orElse(new Post());
comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase());
comment.setPost(post);
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));

View File

@ -87,7 +87,7 @@ public class ApiMetaWeBlog {
} else if ("metaWeblog.newPost".equals(methodName)) {
Post post = parsetPost(methodCall);
post.setUser(user);
post = postService.save(post);
post = postService.create(post);
final StrBuilder strBuilder = new StrBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>");
strBuilder.append("<params><param><value><string>");
strBuilder.append(post.getPostId());
@ -101,7 +101,7 @@ public class ApiMetaWeBlog {
final Long postId = Long.parseLong(params.getJSONObject(0).getJSONObject("value").optString("string"));
post.setPostId(postId);
post.setUser(user);
postService.save(post);
postService.create(post);
final StrBuilder strBuilder = new StrBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>");
strBuilder.append("<params><param><value><string>");
strBuilder.append(postId);
@ -109,7 +109,7 @@ public class ApiMetaWeBlog {
responseContent = strBuilder.toString();
} else if ("blogger.deletePost".equals(methodName)) {
final Long postId = Long.parseLong(params.getJSONObject(0).getJSONObject("value").optString("string"));
postService.remove(postId);
postService.removeById(postId);
final StrBuilder strBuilder = new StrBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>");
strBuilder.append("<params><param><value><boolean>");
strBuilder.append(true);
@ -180,7 +180,7 @@ public class ApiMetaWeBlog {
*/
private String buildPost(final Long postId) {
final StrBuilder strBuilder = new StrBuilder();
final Post post = postService.findByPostId(postId).orElse(new Post());
final Post post = postService.fetchById(postId).orElse(new Post());
strBuilder.append("<struct>");
strBuilder.append("<member><name>dateCreated</name>");
strBuilder.append("<value><dateTime.iso8601>");

View File

@ -140,7 +140,7 @@ public class InstallController {
post.setCategories(categories);
post.setAllowComment(AllowCommentEnum.ALLOW.getCode());
post.setPostThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
postService.save(post);
postService.create(post);
//第一个评论
final Comment comment = new Comment();

View File

@ -80,7 +80,7 @@ public class FrontCommentController {
}
try {
Comment lastComment = null;
post = postService.findByPostId(post.getPostId()).orElse(new Post());
post = postService.fetchById(post.getPostId()).orElse(new Post());
comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase());
comment.setPost(post);
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));