Skip to content

Commit a5ad758

Browse files
author
shiminghui
committed
# 文章 评论的时间格式化显示# 文章详情添加所属 标签 文章分类的显示# 文章编辑功能# 对评论进行回复
1 parent 6be9d1d commit a5ad758

File tree

95 files changed

+854
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+854
-243
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Vue + SpringBoot实现的博客系统
3030
![image](https://github.com/shimh-develop/blog-vue-springboot/blob/master/document/detail.png)
3131

3232
## 评论
33-
![image](https://github.com/shimh-develop/blog-vue-springboot/blob/master/document/comment.png)
33+
![image](https://github.com/shimh-develop/blog-vue-springboot/blob/master/document/comment3.png)
3434

3535
# 技术
3636

@@ -65,7 +65,7 @@ Vue + SpringBoot实现的博客系统
6565
- 文章分类-标签:列表、详情
6666
- 文章归档
6767
- 文章:写文章、文章详情
68-
- 评论:文章添加评论
68+
- 评论:文章添加评论 对评论回复
6969
- 文章列表滑动分页
7070

7171
## 后端
@@ -76,8 +76,7 @@ Vue + SpringBoot实现的博客系统
7676
- 操作日志记录
7777

7878
# 待实现功能
79-
- 评论的分页
80-
- 评论回复、点赞
79+
- 评论的分页 点赞
8180
- 留言板
8281
- 第三方登录
8382
- ......

blog-api/src/main/java/com/shimh/controller/ArticleController.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ public Result listNewArticles() {
8484
@GetMapping("/{id}")
8585
@FastJsonView(
8686
exclude = {
87-
@FastJsonFilter(clazz = Article.class, props = {"category", "comments"}),
88-
@FastJsonFilter(clazz = ArticleBody.class, props = {"contentHtml"}),
89-
@FastJsonFilter(clazz = Tag.class, props = {"avatar"})},
90-
include = {@FastJsonFilter(clazz = User.class, props = {"nickname", "avatar"})})
87+
@FastJsonFilter(clazz = Article.class, props = {"comments"}),
88+
@FastJsonFilter(clazz = ArticleBody.class, props = {"contentHtml"})})
9189
@LogAnnotation(module = "文章", operation = "根据id获取文章")
9290
public Result getArticleById(@PathVariable("id") Integer id) {
9391

@@ -108,10 +106,10 @@ public Result getArticleById(@PathVariable("id") Integer id) {
108106
@GetMapping("/view/{id}")
109107
@FastJsonView(
110108
exclude = {
111-
@FastJsonFilter(clazz = Article.class, props = {"category", "comments"}),
109+
@FastJsonFilter(clazz = Article.class, props = {"comments"}),
112110
@FastJsonFilter(clazz = ArticleBody.class, props = {"contentHtml"}),
113111
@FastJsonFilter(clazz = Tag.class, props = {"avatar"})},
114-
include = {@FastJsonFilter(clazz = User.class, props = {"nickname", "avatar"})})
112+
include = {@FastJsonFilter(clazz = User.class, props = {"id", "nickname", "avatar"})})
115113
@LogAnnotation(module = "文章", operation = "根据id获取文章,添加阅读数")
116114
public Result getArticleAndAddViews(@PathVariable("id") Integer id) {
117115

@@ -156,12 +154,12 @@ public Result listArticlesByCategory(@PathVariable Integer id) {
156154
return Result.success(articles);
157155
}
158156

159-
@PostMapping("/create")
157+
@PostMapping("/publish")
160158
@RequiresAuthentication
161-
@LogAnnotation(module = "文章", operation = "添加文章")
159+
@LogAnnotation(module = "文章", operation = "发布文章")
162160
public Result saveArticle(@Validated @RequestBody Article article) {
163161

164-
Integer articleId = articleService.saveArticle(article);
162+
Integer articleId = articleService.publishArticle(article);
165163

166164
Result r = Result.success();
167165
r.simple().put("articleId", articleId);

blog-api/src/main/java/com/shimh/controller/CommentController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public Result getCommentById(@PathVariable("id") Integer id) {
7171
@GetMapping("/article/{id}")
7272
@FastJsonView(
7373
exclude = {
74-
@FastJsonFilter(clazz = Comment.class, props = {"article"})},
75-
include = {@FastJsonFilter(clazz = User.class, props = {"nickname", "avatar"})})
74+
@FastJsonFilter(clazz = Comment.class, props = {"article", "parent"})},
75+
include = {@FastJsonFilter(clazz = User.class, props = {"id", "nickname", "avatar"})})
7676
@LogAnnotation(module = "评论", operation = "根据文章获取评论")
7777
public Result listCommentsByArticle(@PathVariable("id") Integer id) {
7878

@@ -125,7 +125,7 @@ public Result deleteCommentById(@PathVariable("id") Integer id) {
125125
@FastJsonView(
126126
exclude = {
127127
@FastJsonFilter(clazz = Comment.class, props = {"article"})},
128-
include = {@FastJsonFilter(clazz = User.class, props = {"nickname", "avatar"})})
128+
include = {@FastJsonFilter(clazz = User.class, props = {"id", "nickname", "avatar"})})
129129
@RequiresAuthentication
130130
@LogAnnotation(module = "评论", operation = "添加评论,增加评论数")
131131
public Result saveCommentAndChangeCounts(@RequestBody Comment comment) {

blog-api/src/main/java/com/shimh/controller/LoginController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Result login(@RequestBody User user) {
4848
}
4949

5050
@PostMapping("/register")
51+
//@RequiresRoles(Base.ROLE_ADMIN)
5152
@LogAnnotation(module = "注册", operation = "注册")
5253
public Result register(@RequestBody User user) {
5354

blog-api/src/main/java/com/shimh/controller/UserController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import javax.servlet.http.HttpServletRequest;
66

7+
import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
8+
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;
79
import com.shimh.common.annotation.LogAnnotation;
10+
import com.shimh.entity.Article;
811
import org.apache.shiro.SecurityUtils;
912
import org.apache.shiro.authz.annotation.RequiresAuthentication;
1013
import org.apache.shiro.authz.annotation.RequiresRoles;
@@ -43,6 +46,7 @@ public class UserController {
4346

4447
@GetMapping
4548
@LogAnnotation(module = "用户", operation = "获取所有用户")
49+
@RequiresRoles(Base.ROLE_ADMIN)
4650
public Result listUsers() {
4751
List<User> users = userService.findAll();
4852

@@ -51,6 +55,7 @@ public Result listUsers() {
5155

5256
@GetMapping("/{id}")
5357
@LogAnnotation(module = "用户", operation = "根据id获取用户")
58+
@RequiresRoles(Base.ROLE_ADMIN)
5459
public Result getUserById(@PathVariable("id") Long id) {
5560

5661
Result r = new Result();
@@ -68,6 +73,8 @@ public Result getUserById(@PathVariable("id") Long id) {
6873
}
6974

7075
@GetMapping("/currentUser")
76+
@FastJsonView(
77+
include = {@FastJsonFilter(clazz = User.class, props = {"id", "account", "nickname", "avatar"})})
7178
@LogAnnotation(module = "用户", operation = "获取当前登录用户")
7279
public Result getCurrentUser(HttpServletRequest request) {
7380

blog-api/src/main/java/com/shimh/entity/Comment.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.shimh.entity;
22

33
import java.util.Date;
4+
import java.util.List;
45

5-
import javax.persistence.Column;
6-
import javax.persistence.Entity;
7-
import javax.persistence.FetchType;
8-
import javax.persistence.JoinColumn;
9-
import javax.persistence.ManyToOne;
10-
import javax.persistence.Table;
11-
import javax.persistence.Temporal;
12-
import javax.persistence.TemporalType;
13-
import javax.persistence.Transient;
6+
import javax.persistence.*;
147

8+
import org.hibernate.annotations.NotFound;
9+
import org.hibernate.annotations.NotFoundAction;
1510
import org.hibernate.validator.constraints.NotBlank;
1611

1712
import com.alibaba.fastjson.annotation.JSONField;
@@ -37,6 +32,11 @@ public class Comment extends BaseEntity<Integer> {
3732
@JoinColumn(name = "author_id")
3833
private User author;
3934

35+
/**
36+
* 类型 0 文章的评论 1 评论的评论 2 评论的回复 @
37+
*/
38+
@Column(name = "level",length = 1)
39+
private String level;
4040

4141
/**
4242
* 创建时间
@@ -51,6 +51,20 @@ public class Comment extends BaseEntity<Integer> {
5151
@JoinColumn(name = "article_id")
5252
private Article article;
5353

54+
@OneToMany
55+
@JoinColumn(name = "parent_id",nullable = true)
56+
private List<Comment> childrens;
57+
58+
@ManyToOne
59+
@JoinColumn(name = "parent_id")
60+
@NotFound(action= NotFoundAction.IGNORE)
61+
private Comment parent;
62+
63+
64+
@ManyToOne(fetch = FetchType.LAZY)
65+
@JoinColumn(name = "to_uid")
66+
private User toUser;
67+
5468

5569
public String getContent() {
5670
return content;
@@ -91,5 +105,35 @@ public void setArticle(Article article) {
91105
this.article = article;
92106
}
93107

108+
public List<Comment> getChildrens() {
109+
return childrens;
110+
}
94111

112+
public void setChildrens(List<Comment> childrens) {
113+
this.childrens = childrens;
114+
}
115+
116+
public Comment getParent() {
117+
return parent;
118+
}
119+
120+
public void setParent(Comment parent) {
121+
this.parent = parent;
122+
}
123+
124+
public User getToUser() {
125+
return toUser;
126+
}
127+
128+
public void setToUser(User toUser) {
129+
this.toUser = toUser;
130+
}
131+
132+
public String getLevel() {
133+
return level;
134+
}
135+
136+
public void setLevel(String level) {
137+
this.level = level;
138+
}
95139
}

blog-api/src/main/java/com/shimh/repository/CommentRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public interface CommentRepository extends JpaRepository<Comment, Integer> {
1616

17-
List<Comment> findByArticleOrderByCreateDateDesc(Article a);
17+
List<Comment> findByArticleAndLevelOrderByCreateDateDesc(Article a, String level);
1818

1919

2020
}

blog-api/src/main/java/com/shimh/service/ArticleService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public interface ArticleService {
2222

2323
Article getArticleById(Integer id);
2424

25+
Integer publishArticle(Article article);
26+
2527
Integer saveArticle(Article article);
2628

2729
Integer updateArticle(Article article);

blog-api/src/main/java/com/shimh/service/impl/ArticleServiceImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,22 @@ public Article getArticleById(Integer id) {
5555
return articleRepository.getOne(id);
5656
}
5757

58+
@Override
59+
@Transactional
60+
public Integer publishArticle(Article article) {
61+
62+
if(null != article.getId()){
63+
return this.updateArticle(article);
64+
}else{
65+
return this.saveArticle(article);
66+
}
67+
68+
}
69+
5870
@Override
5971
@Transactional
6072
public Integer saveArticle(Article article) {
73+
6174
User currentUser = UserUtils.getCurrentUser();
6275

6376
if (null != currentUser) {

blog-api/src/main/java/com/shimh/service/impl/CommentServiceImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.shimh.service.impl;
22

3+
import java.util.ArrayList;
34
import java.util.Date;
45
import java.util.List;
56

7+
import com.shimh.entity.User;
8+
import com.shimh.repository.UserRepository;
69
import org.springframework.beans.factory.annotation.Autowired;
710
import org.springframework.stereotype.Service;
811
import org.springframework.transaction.annotation.Transactional;
@@ -25,6 +28,9 @@ public class CommentServiceImpl implements CommentService {
2528
@Autowired
2629
private ArticleRepository articleRepository;
2730

31+
@Autowired
32+
private UserRepository userRepository;
33+
2834

2935
@Autowired
3036
private CommentRepository commentRepository;
@@ -57,7 +63,7 @@ public void deleteCommentById(Integer id) {
5763
public List<Comment> listCommentsByArticle(Integer id) {
5864
Article a = new Article();
5965
a.setId(id);
60-
return commentRepository.findByArticleOrderByCreateDateDesc(a);
66+
return commentRepository.findByArticleAndLevelOrderByCreateDateDesc(a, "0");
6167
}
6268

6369
@Override
@@ -71,6 +77,17 @@ public Comment saveCommentAndChangeCounts(Comment comment) {
7177
comment.setAuthor(UserUtils.getCurrentUser());
7278
comment.setCreateDate(new Date());
7379

80+
//设置level
81+
if(null == comment.getParent()){
82+
comment.setLevel("0");
83+
}else{
84+
if(null == comment.getToUser()){
85+
comment.setLevel("1");
86+
}else{
87+
comment.setLevel("2");
88+
}
89+
}
90+
7491
return commentRepository.save(comment);
7592

7693
}

0 commit comments

Comments
 (0)