新增及编辑文章功能

master
barney 2 years ago
parent 4794869375
commit 7bf5c46464
  1. 2
      .gitignore
  2. 15
      src/main/java/cc/bnblogs/controller/AdminController.java
  3. 3
      src/main/java/cc/bnblogs/controller/ArticleController.java
  4. 14
      src/main/java/cc/bnblogs/pojo/Article.java
  5. 36
      src/main/java/cc/bnblogs/service/ArticleService.java
  6. 48
      src/main/java/cc/bnblogs/utils/UpdateUtil.java
  7. 2
      src/main/resources/static/plugin/editormd/editormd.js
  8. BIN
      src/main/resources/static/plugin/editormd/images/emoji/+1.png
  9. BIN
      src/main/resources/static/plugin/editormd/images/emoji/-1.png
  10. BIN
      src/main/resources/static/plugin/editormd/images/emoji/100.png
  11. BIN
      src/main/resources/static/plugin/editormd/images/emoji/109.png
  12. BIN
      src/main/resources/static/plugin/editormd/images/emoji/1234.png
  13. BIN
      src/main/resources/static/plugin/editormd/images/emoji/8ball.png
  14. BIN
      src/main/resources/static/plugin/editormd/images/emoji/a.png
  15. BIN
      src/main/resources/static/plugin/editormd/images/emoji/ab.png
  16. BIN
      src/main/resources/static/plugin/editormd/images/emoji/abc.png
  17. BIN
      src/main/resources/static/plugin/editormd/images/emoji/abcd.png
  18. BIN
      src/main/resources/static/plugin/editormd/images/emoji/accept.png
  19. BIN
      src/main/resources/static/plugin/editormd/images/emoji/aerial_tramway.png
  20. BIN
      src/main/resources/static/plugin/editormd/images/emoji/airplane.png
  21. BIN
      src/main/resources/static/plugin/editormd/images/emoji/alarm_clock.png
  22. BIN
      src/main/resources/static/plugin/editormd/images/emoji/alien.png
  23. BIN
      src/main/resources/static/plugin/editormd/images/emoji/ambulance.png
  24. BIN
      src/main/resources/static/plugin/editormd/images/emoji/anchor.png
  25. BIN
      src/main/resources/static/plugin/editormd/images/emoji/angel.png
  26. BIN
      src/main/resources/static/plugin/editormd/images/emoji/anger.png
  27. BIN
      src/main/resources/static/plugin/editormd/images/emoji/angry.png
  28. BIN
      src/main/resources/static/plugin/editormd/images/emoji/anguished.png
  29. BIN
      src/main/resources/static/plugin/editormd/images/emoji/ant.png
  30. BIN
      src/main/resources/static/plugin/editormd/images/emoji/apple.png
  31. BIN
      src/main/resources/static/plugin/editormd/images/emoji/aquarius.png
  32. BIN
      src/main/resources/static/plugin/editormd/images/emoji/aries.png
  33. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_backward.png
  34. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_double_down.png
  35. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_double_up.png
  36. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_down.png
  37. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_down_small.png
  38. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_forward.png
  39. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_heading_down.png
  40. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_heading_up.png
  41. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_left.png
  42. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_lower_left.png
  43. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_lower_right.png
  44. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_right.png
  45. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_right_hook.png
  46. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_up.png
  47. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_up_down.png
  48. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_up_small.png
  49. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_upper_left.png
  50. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrow_upper_right.png
  51. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrows_clockwise.png
  52. BIN
      src/main/resources/static/plugin/editormd/images/emoji/arrows_counterclockwise.png
  53. BIN
      src/main/resources/static/plugin/editormd/images/emoji/art.png
  54. BIN
      src/main/resources/static/plugin/editormd/images/emoji/articulated_lorry.png
  55. BIN
      src/main/resources/static/plugin/editormd/images/emoji/astonished.png
  56. BIN
      src/main/resources/static/plugin/editormd/images/emoji/atm.png
  57. BIN
      src/main/resources/static/plugin/editormd/images/emoji/b.png
  58. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baby.png
  59. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baby_bottle.png
  60. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baby_chick.png
  61. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baby_symbol.png
  62. BIN
      src/main/resources/static/plugin/editormd/images/emoji/back.png
  63. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baggage_claim.png
  64. BIN
      src/main/resources/static/plugin/editormd/images/emoji/balloon.png
  65. BIN
      src/main/resources/static/plugin/editormd/images/emoji/ballot_box_with_check.png
  66. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bamboo.png
  67. BIN
      src/main/resources/static/plugin/editormd/images/emoji/banana.png
  68. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bangbang.png
  69. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bank.png
  70. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bar_chart.png
  71. BIN
      src/main/resources/static/plugin/editormd/images/emoji/barber.png
  72. BIN
      src/main/resources/static/plugin/editormd/images/emoji/baseball.png
  73. BIN
      src/main/resources/static/plugin/editormd/images/emoji/basketball.png
  74. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bath.png
  75. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bathtub.png
  76. BIN
      src/main/resources/static/plugin/editormd/images/emoji/battery.png
  77. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bear.png
  78. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bee.png
  79. BIN
      src/main/resources/static/plugin/editormd/images/emoji/beer.png
  80. BIN
      src/main/resources/static/plugin/editormd/images/emoji/beers.png
  81. BIN
      src/main/resources/static/plugin/editormd/images/emoji/beetle.png
  82. BIN
      src/main/resources/static/plugin/editormd/images/emoji/beginner.png
  83. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bell.png
  84. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bento.png
  85. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bicyclist.png
  86. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bike.png
  87. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bikini.png
  88. BIN
      src/main/resources/static/plugin/editormd/images/emoji/bird.png
  89. BIN
      src/main/resources/static/plugin/editormd/images/emoji/birthday.png
  90. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_circle.png
  91. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_joker.png
  92. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_medium_small_square.png
  93. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_medium_square.png
  94. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_nib.png
  95. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_small_square.png
  96. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_square.png
  97. BIN
      src/main/resources/static/plugin/editormd/images/emoji/black_square_button.png
  98. BIN
      src/main/resources/static/plugin/editormd/images/emoji/blossom.png
  99. BIN
      src/main/resources/static/plugin/editormd/images/emoji/blowfish.png
  100. BIN
      src/main/resources/static/plugin/editormd/images/emoji/blue_book.png
  101. Some files were not shown because too many files have changed in this diff Show More

2
.gitignore vendored

@ -1,5 +1,7 @@
.idea/
target/
# 忽略上传图片的文件夹
upload/
# Compiled class file
*.class

@ -1,13 +1,18 @@
package cc.bnblogs.controller;
import cc.bnblogs.pojo.Article;
import cc.bnblogs.pojo.Category;
import cc.bnblogs.service.ArticleService;
import cc.bnblogs.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.Objects;
/**
* @author zfp@bnblogs.cc
@ -17,9 +22,11 @@ import java.util.List;
@RequestMapping("/admin")
public class AdminController {
private final CategoryService categoryService;
private final ArticleService articleService;
public AdminController(CategoryService categoryService) {
public AdminController(CategoryService categoryService, ArticleService articleService) {
this.categoryService = categoryService;
this.articleService = articleService;
}
@GetMapping("/")
@ -54,11 +61,15 @@ public class AdminController {
return "admin/comment";
}
// 打开编辑文章界面可以带一个id参数
@GetMapping("/write.html")
public String write(Model model) {
public String write(@RequestParam(required = false)Integer id, Model model) {
// 获取分类列表
List<Category> categories = categoryService.list();
// 默认开启评论
Article article = Objects.isNull(id) ? Article.builder().allowComment(1).build() : articleService.detail(id);
model.addAttribute("categories",categories);
model.addAttribute("article",article);
return "admin/write";
}
}

@ -51,8 +51,9 @@ public class ArticleController {
* @param article 修改的文章
* @return 修改结果
*/
// @RequestBody: 可以接收json类型的数据
@PostMapping("/")
public Result<String> save(Article article) {
public Result<String> save(@RequestBody Article article) {
articleService.save(article);
return Result.success();
}

@ -54,4 +54,18 @@ public class Article {
// 分类和文章是一对多关系
@ManyToOne
private Category category;
// 文章
public static final Integer TYPE_ARTICLE = 1;
// 页面
public static final Integer TYPE_PAGE = 2;
// 文章已发布
public static final Integer STATUS_PUBLISH = 1;
// 草稿
public static final Integer STATUS_DRAFT = 2;
// 允许评论
public static final Integer COMMENT_ENABLE = 1;
// 不允许评论
public static final Integer COMMENT_DISABLE = 2;
}

@ -1,10 +1,15 @@
package cc.bnblogs.service;
import cc.bnblogs.mapper.ArticleMapper;
import cc.bnblogs.mapper.TagMapper;
import cc.bnblogs.pojo.Article;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author zfp@bnblogs.cc
@ -12,11 +17,13 @@ import java.util.List;
*/
@Service
public class ArticleService {
private final TagMapper tagMapper;
private final ArticleMapper articleMapper;
public ArticleService(ArticleMapper articleMapper) {
public ArticleService(ArticleMapper articleMapper, TagMapper tagMapper) {
this.articleMapper = articleMapper;
this.tagMapper = tagMapper;
}
/**
@ -48,7 +55,34 @@ public class ArticleService {
* 保存文章
* @param article 待保存的文章对象
*/
@Transactional
public void save(Article article) {
// 修改更新时间
article.setUpdated(new Date());
// 设置空字段的默认值
// 设置文章类型(默认是文章)
if (Objects.isNull(article.getType())) {
article.setType(Article.TYPE_ARTICLE);
}
// 设置文章状态(默认是发布状态)
if (Objects.isNull(article.getStatus())) {
article.setStatus(Article.STATUS_PUBLISH);
}
// 设置是否可以评论(默认禁止评论)
if (Objects.isNull(article.getAllowComment())) {
article.setAllowComment(Article.COMMENT_DISABLE);
}
// 如果文章是新建的,设置创建时间;设置访问量为0
if (Objects.isNull(article.getCreated())) {
article.setCreated(new Date());
article.setViews(0);
}
// 将数据库中不存在的标签插入数据库中
article.setTags(article.getTags().stream().peek(x -> {
if (Objects.isNull(x.getId())) {
x.setId(tagMapper.save(x).getId());
}
}).collect(Collectors.toList()));
articleMapper.save(article);
}

@ -0,0 +1,48 @@
package cc.bnblogs.utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* 更新工具类(忽略为null的字段)
*/
public class UpdateUtil {
/**
* 所有为空值的属性都不copy
* @param source
* @param target
*/
public static void copyNullProperties(Object source, Object target) {
BeanUtils.copyProperties(source, target, getNullField(source));
}
/**
* 获取属性中为空的字段
*
* @param target
* @return
*/
private static String[] getNullField(Object target) {
BeanWrapper beanWrapper = new BeanWrapperImpl(target);
PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
Set<String> notNullFieldSet = new HashSet<>();
if (propertyDescriptors.length > 0) {
for (PropertyDescriptor p : propertyDescriptors) {
String name = p.getName();
Object value = beanWrapper.getPropertyValue(name);
if (Objects.isNull(value)) {
notNullFieldSet.add(name);
}
}
}
String[] notNullField = new String[notNullFieldSet.size()];
return notNullFieldSet.toArray(notNullField);
}
}

@ -3365,7 +3365,7 @@
// Emoji graphics files url path
editormd.emoji = {
path : "https://www.webpagefx.com/tools/emoji-cheat-sheet/graphics/emojis/",
path : "/static/plugin/editormd/images/emoji/",
ext : ".png"
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save