Skip to content

Latest commit

 

History

History
210 lines (172 loc) · 6.92 KB

pattern.md

File metadata and controls

210 lines (172 loc) · 6.92 KB

design patern @CacheLock @ApiOperation("Initializes the blog") public BaseResponse installBlog(@RequestBody InstallParam installParam) { // Validate manually ValidationUtils.validate(installParam, CreateCheck.class);

// Check is installed
boolean isInstalled = optionService
    .getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);

if (isInstalled) {
    throw new BadRequestException("该博客已初始化,不能再次安装!");
}

// Initialize settings
initSettings(installParam);

// Create default user
User user = createUser(installParam);

// Create default category
Category category = createDefaultCategoryIfAbsent();

// Create default post
PostDetailVO post = createDefaultPostIfAbsent(category);

// Create default sheet
createDefaultSheet();

// Create default postComment
createDefaultComment(post);

// Create default menu
createDefaultMenu();

eventPublisher.publishEvent(
    new LogEvent(this, user.getId().toString(), LogType.BLOG_INITIALIZED, "博客已成功初始化")
);

return BaseResponse.ok("安装完成!");

}

private void createDefaultMenu() { long menuCount = menuService.count();

if (menuCount > 0) {
    return;
}

MenuParam menuIndex = new MenuParam();

menuIndex.setName("首页");
menuIndex.setUrl("/");
menuIndex.setPriority(1);

menuService.create(menuIndex.convertTo());

MenuParam menuArchive = new MenuParam();

menuArchive.setName("文章归档");
menuArchive.setUrl("/archives");
menuArchive.setPriority(2);
menuService.create(menuArchive.convertTo());

MenuParam menuCategory = new MenuParam();
menuCategory.setName("默认分类");
menuCategory.setUrl("/categories/default");
menuCategory.setPriority(3);
menuService.create(menuCategory.convertTo());

MenuParam menuSheet = new MenuParam();
menuSheet.setName("关于页面");
menuSheet.setUrl("/s/about");
menuSheet.setPriority(4);
menuService.create(menuSheet.convertTo());

}

@Nullable private void createDefaultComment(@Nullable PostDetailVO post) { if (post == null) { return; }

long commentCount = postCommentService.count();

if (commentCount > 0) {
    return;
}

PostComment comment = new PostComment();
comment.setAuthor("Halo");
comment.setAuthorUrl("https://halo.run");
comment.setContent(
    "欢迎使用 Halo,这是你的第一条评论,头像来自 [Gravatar](https://cn.gravatar.com),"
        + "你也可以通过注册 [Gravatar]"
        + "(https://cn.gravatar.com) 来显示自己的头像。");
comment.setEmail("[email protected]");
comment.setPostId(post.getId());
postCommentService.create(comment);

}

@Nullable private PostDetailVO createDefaultPostIfAbsent(@Nullable Category category) {

long publishedCount = postService.countByStatus(PostStatus.PUBLISHED);

if (publishedCount > 0) {
    return null;
}

PostParam postParam = new PostParam();
postParam.setSlug("hello-halo");
postParam.setTitle("Hello Halo");
postParam.setStatus(PostStatus.PUBLISHED);
postParam.setOriginalContent("## Hello Halo\n"
    + "\n"
    + "如果你看到了这一篇文章,那么证明你已经安装成功了,感谢使用 [Halo](https://halo.run) 进行创作,希望能够使用愉快。\n"
    + "\n"
    + "## 相关链接\n"
    + "\n"
    + "- 官网:[https://halo.run](https://halo.run)\n"
    + "- 文档:[https://docs.halo.run](https://docs.halo.run)\n"
    + "- 社区:[https://bbs.halo.run](https://bbs.halo.run)\n"
    + "- 主题仓库:[https://halo.run/themes.html](https://halo.run/themes.html)\n"
    + "- 开源地址:[https://github.com/halo-dev/halo](https://github.com/halo-dev/halo)\n"
    + "\n"
    + "在使用过程中,有任何问题都可以通过以上链接找寻答案,或者联系我们。\n"
    + "\n"
    + "> 这是一篇自动生成的文章,请删除这篇文章之后开始你的创作吧!\n"
    + "\n");

Set<Integer> categoryIds = new HashSet<>();
if (category != null) {
    categoryIds.add(category.getId());
    postParam.setCategoryIds(categoryIds);
}
return postService
    .createBy(postParam.convertTo(), Collections.emptySet(), categoryIds, false);

}

@Nullable private void createDefaultSheet() { long publishedCount = sheetService.countByStatus(PostStatus.PUBLISHED); if (publishedCount > 0) { return; }

SheetParam sheetParam = new SheetParam();
sheetParam.setSlug("about");
sheetParam.setTitle("关于页面");
sheetParam.setStatus(PostStatus.PUBLISHED);
sheetParam.setOriginalContent("## 关于页面\n"
    + "\n"
    + "这是一个自定义页面,你可以在后台的 `页面` -> `所有页面` -> `自定义页面` 找到它,"
    + "你可以用于新建关于页面、留言板页面等等。发挥你自己的想象力!\n"
    + "\n"
    + "> 这是一篇自动生成的页面,你可以在后台删除它。");
sheetService.createBy(sheetParam.convertTo(), false);

}

@Nullable private Category createDefaultCategoryIfAbsent() { long categoryCount = categoryService.count(); if (categoryCount > 0) { return null; }

CategoryParam category = new CategoryParam();
category.setName("默认分类");
category.setSlug("default");
category.setDescription("这是你的默认分类,如不需要,删除即可。");
ValidationUtils.validate(category);
return categoryService.create(category.convertTo());

}

private User createUser(InstallParam installParam) { // Get user return userService.getCurrentUser().map(user -> { // Update this user installParam.update(user); // Set password manually userService.setPassword(user, installParam.getPassword()); // Update user return userService.update(user); }).orElseGet(() -> { String gravatar = "//cn.gravatar.com/avatar/" + DigestUtils.md5Hex(installParam.getEmail()) + "?s=256&d=mm"; installParam.setAvatar(gravatar); return userService.createBy(installParam); }); }

private void initSettings(InstallParam installParam) { // Init default properties Map<PropertyEnum, String> properties = new HashMap<>(11); properties.put(PrimaryProperties.IS_INSTALLED, Boolean.TRUE.toString()); properties.put(BlogProperties.BLOG_LOCALE, installParam.getLocale()); properties.put(BlogProperties.BLOG_TITLE, installParam.getTitle()); properties.put(BlogProperties.BLOG_URL, StringUtils.isBlank(installParam.getUrl()) ? optionService.getBlogBaseUrl() : installParam.getUrl()); properties.put(OtherProperties.GLOBAL_ABSOLUTE_PATH_ENABLED, Boolean.FALSE.toString());

// Create properties
optionService.saveProperties(properties);

}