Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
段启岩
beyond-clouds
Commits
36733c3b
Commit
36733c3b
authored
Feb 03, 2020
by
Author name
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2.3动态评论完成
parent
ff564007
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
282 additions
and
5 deletions
+282
-5
src/main/java/cn/meteor/beyondclouds/modules/blog/api/BlogCommentApi.java
+1
-1
src/main/java/cn/meteor/beyondclouds/modules/blog/service/IBlogCommentService.java
+1
-1
src/main/java/cn/meteor/beyondclouds/modules/blog/service/impl/BlogCommentServiceImpl.java
+1
-1
src/main/java/cn/meteor/beyondclouds/modules/blog/service/impl/BlogServiceImpl.java
+4
-1
src/main/java/cn/meteor/beyondclouds/modules/post/api/PostApi.java
+4
-0
src/main/java/cn/meteor/beyondclouds/modules/post/api/PostCommentApi.java
+78
-0
src/main/java/cn/meteor/beyondclouds/modules/post/enums/PostCommentErrorCode.java
+4
-0
src/main/java/cn/meteor/beyondclouds/modules/post/form/PostCommentForm.java
+19
-0
src/main/java/cn/meteor/beyondclouds/modules/post/service/IPostCommentService.java
+33
-0
src/main/java/cn/meteor/beyondclouds/modules/post/service/impl/PostCommentServiceImpl.java
+136
-0
src/main/resources/application.yml
+1
-1
No files found.
src/main/java/cn/meteor/beyondclouds/modules/blog/api/BlogCommentApi.java
View file @
36733c3b
...
...
@@ -38,7 +38,7 @@ public class BlogCommentApi {
public
Response
createComment
(
@PathVariable
(
"blogId"
)
String
blogId
,
@RequestBody
@Valid
BlogCommentForm
blogCommentForm
,
@CurrentSubject
Subject
subject
)
{
try
{
blogCommentService
.
c
ommentCreate
(
blogId
,
blogCommentForm
.
getParentId
(),
blogCommentService
.
c
reateComment
(
blogId
,
blogCommentForm
.
getParentId
(),
blogCommentForm
.
getComment
(),
(
String
)
subject
.
getId
());
return
Response
.
success
();
}
catch
(
BlogServiceException
|
BlogCommentServiceException
e
)
{
...
...
src/main/java/cn/meteor/beyondclouds/modules/blog/service/IBlogCommentService.java
View file @
36733c3b
...
...
@@ -24,7 +24,7 @@ public interface IBlogCommentService extends IService<BlogComment> {
* @param comment
* @param userId
*/
void
c
ommentCreate
(
String
blogId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
BlogServiceException
,
BlogCommentServiceException
;
void
c
reateComment
(
String
blogId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
BlogServiceException
,
BlogCommentServiceException
;
/**
* 删除评论
...
...
src/main/java/cn/meteor/beyondclouds/modules/blog/service/impl/BlogCommentServiceImpl.java
View file @
36733c3b
...
...
@@ -45,7 +45,7 @@ public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogC
* @throws BlogCommentServiceException
*/
@Override
public
void
c
ommentCreate
(
String
blogId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
BlogServiceException
,
BlogCommentServiceException
{
public
void
c
reateComment
(
String
blogId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
BlogServiceException
,
BlogCommentServiceException
{
//判断博客是否存在
...
...
src/main/java/cn/meteor/beyondclouds/modules/blog/service/impl/BlogServiceImpl.java
View file @
36733c3b
...
...
@@ -209,7 +209,9 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
@Override
public
IPage
<
Blog
>
getBlogPage
(
Integer
pageNumber
,
Integer
pageSize
)
{
IPage
<
Blog
>
page
=
new
Page
<>(
pageNumber
,
pageSize
);
return
page
(
page
);
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
orderByDesc
(
"create_time"
);
return
page
(
page
,
queryWrapper
);
}
/**
...
...
@@ -223,6 +225,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
public
IPage
<
Blog
>
getUserBlogPage
(
Integer
pageNumber
,
Integer
pageSize
,
String
userId
)
{
QueryWrapper
myBlogQueryWrapper
=
new
QueryWrapper
();
myBlogQueryWrapper
.
eq
(
"user_id"
,
userId
);
myBlogQueryWrapper
.
orderByDesc
(
"create_time"
);
IPage
<
Blog
>
page
=
new
Page
<>(
pageNumber
,
pageSize
);
return
page
(
page
,
myBlogQueryWrapper
);
}
...
...
src/main/java/cn/meteor/beyondclouds/modules/post/api/PostApi.java
View file @
36733c3b
...
...
@@ -53,6 +53,10 @@ public class PostApi {
@PostMapping
(
"/post"
)
public
Response
publishPost
(
@RequestBody
@Valid
PostForm
postForm
,
BindingResult
result
,
@CurrentSubject
Subject
subject
)
{
if
(
result
.
hasErrors
())
{
return
Response
.
fieldError
(
result
.
getFieldError
());
}
//对图片进行类型转换
Post
post
=
new
Post
();
BeanUtils
.
copyProperties
(
postForm
,
post
);
...
...
src/main/java/cn/meteor/beyondclouds/modules/post/api/PostCommentApi.java
View file @
36733c3b
package
cn
.
meteor
.
beyondclouds
.
modules
.
post
.
api
;
import
cn.meteor.beyondclouds.common.form.PageForm
;
import
cn.meteor.beyondclouds.common.vo.PageVO
;
import
cn.meteor.beyondclouds.core.annotation.CurrentSubject
;
import
cn.meteor.beyondclouds.core.api.Response
;
import
cn.meteor.beyondclouds.core.bean.Subject
;
import
cn.meteor.beyondclouds.core.exception.ServiceException
;
import
cn.meteor.beyondclouds.modules.post.entity.PostComment
;
import
cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException
;
import
cn.meteor.beyondclouds.modules.post.exception.PostServiceException
;
import
cn.meteor.beyondclouds.modules.post.form.PostCommentForm
;
import
cn.meteor.beyondclouds.modules.post.service.IPostCommentService
;
import
cn.meteor.beyondclouds.modules.tag.entity.Tag
;
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.*
;
import
javax.validation.Valid
;
import
java.nio.channels.Pipe
;
/**
* @author gaoTong
* @date 2020/2/2 9:42
*/
@Api
(
tags
=
"动态评论API"
)
@RestController
@RequestMapping
(
"/api"
)
public
class
PostCommentApi
{
private
IPostCommentService
postCommentService
;
@Autowired
public
void
setPostCommentService
(
IPostCommentService
postCommentService
)
{
this
.
postCommentService
=
postCommentService
;
}
@ApiOperation
(
"发布评论"
)
@PostMapping
(
"/post/{postId}/comment"
)
public
Response
publishComment
(
@PathVariable
(
"postId"
)
String
postId
,
@RequestBody
@Valid
PostCommentForm
postCommentForm
,
BindingResult
result
,
@CurrentSubject
Subject
subject
)
{
if
(
result
.
hasErrors
())
{
return
Response
.
fieldError
(
result
.
getFieldError
());
}
try
{
postCommentService
.
createComment
(
postId
,
postCommentForm
.
getParentId
(),
postCommentForm
.
getComment
(),
(
String
)
subject
.
getId
());
return
Response
.
success
();
}
catch
(
ServiceException
e
)
{
e
.
printStackTrace
();
return
Response
.
error
(
e
);
}
}
@ApiOperation
(
"删除评论"
)
@DeleteMapping
(
"/post/comment/{commentId}"
)
public
Response
deleteComment
(
@PathVariable
(
"commentId"
)
Integer
commentId
,
@CurrentSubject
Subject
subject
)
{
try
{
postCommentService
.
deleteComment
(
commentId
,
(
String
)
subject
.
getId
());
return
Response
.
success
();
}
catch
(
PostCommentServiceException
e
)
{
e
.
printStackTrace
();
return
Response
.
error
(
e
);
}
}
@ApiOperation
(
"评论列表"
)
@GetMapping
(
"/post/{postId}/comments"
)
public
Response
<
PageVO
<
PostComment
>>
getPostCommentPage
(
@PathVariable
(
"postId"
)
String
postId
,
@Valid
PageForm
pageForm
,
@RequestParam
(
value
=
"parent_id"
,
required
=
false
)
Integer
parentId
)
{
try
{
IPage
<
PostComment
>
postCommentIPage
=
postCommentService
.
getCommentPage
(
pageForm
.
getPage
(),
pageForm
.
getSize
(),
postId
,
parentId
);
PageVO
<
PostComment
>
postCommentPageVO
=
new
PageVO
<>(
postCommentIPage
);
return
Response
.
success
(
postCommentPageVO
);
}
catch
(
PostCommentServiceException
e
)
{
e
.
printStackTrace
();
return
Response
.
error
(
e
);
}
}
}
src/main/java/cn/meteor/beyondclouds/modules/post/enums/PostCommentErrorCode.java
View file @
36733c3b
...
...
@@ -7,6 +7,10 @@ import cn.meteor.beyondclouds.core.IErrorCode;
* @date 2020/2/2 14:43
*/
public
enum
PostCommentErrorCode
implements
IErrorCode
{
COMMENT_NOT_FOUND
(
10003
,
"该评论不存在"
),
NO_DELETE_PRIVILEGES
(
10004
,
"没有权限删除评论"
),
PARENT_COMMENT_NOT_FOUND
(
10006
,
"父评论不存在"
),
SUN_COMMENT_NOT_FOUND
(
1007
,
"子级评论不存在"
)
;
private
long
code
;
...
...
src/main/java/cn/meteor/beyondclouds/modules/post/form/PostCommentForm.java
0 → 100644
View file @
36733c3b
package
cn
.
meteor
.
beyondclouds
.
modules
.
post
.
form
;
import
lombok.Data
;
import
javax.validation.constraints.NotEmpty
;
/**
* @author gaoTong
* @date 2020/2/2 21:28
*/
@Data
public
class
PostCommentForm
{
private
Integer
parentId
;
@NotEmpty
(
message
=
"评论内容不能为空"
)
private
String
comment
;
}
src/main/java/cn/meteor/beyondclouds/modules/post/service/IPostCommentService.java
View file @
36733c3b
package
cn
.
meteor
.
beyondclouds
.
modules
.
post
.
service
;
import
cn.meteor.beyondclouds.modules.blog.entity.BlogComment
;
import
cn.meteor.beyondclouds.modules.post.entity.PostComment
;
import
cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException
;
import
cn.meteor.beyondclouds.modules.post.exception.PostServiceException
;
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
com.baomidou.mybatisplus.extension.service.IService
;
/**
...
...
@@ -13,4 +17,33 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public
interface
IPostCommentService
extends
IService
<
PostComment
>
{
/**
* 创建评论
* @param postId
* @param parentId
* @param comment
* @param userId
*/
void
createComment
(
String
postId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
PostServiceException
,
PostCommentServiceException
;
/**
* 删除评论
* @param commentId
* @param userId
*/
void
deleteComment
(
Integer
commentId
,
String
userId
)
throws
PostCommentServiceException
;
/**
* 评论列表
* @param pageNumber
* @param size
* @param postId
* @param parentId
* @return
*/
IPage
<
PostComment
>
getCommentPage
(
Integer
pageNumber
,
Integer
size
,
String
postId
,
Integer
parentId
)
throws
PostCommentServiceException
;
}
src/main/java/cn/meteor/beyondclouds/modules/post/service/impl/PostCommentServiceImpl.java
View file @
36733c3b
package
cn
.
meteor
.
beyondclouds
.
modules
.
post
.
service
.
impl
;
import
cn.meteor.beyondclouds.modules.post.entity.Post
;
import
cn.meteor.beyondclouds.modules.post.entity.PostComment
;
import
cn.meteor.beyondclouds.modules.post.enums.PostCommentErrorCode
;
import
cn.meteor.beyondclouds.modules.post.enums.PostErrorCode
;
import
cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException
;
import
cn.meteor.beyondclouds.modules.post.exception.PostServiceException
;
import
cn.meteor.beyondclouds.modules.post.mapper.PostCommentMapper
;
import
cn.meteor.beyondclouds.modules.post.service.IPostCommentService
;
import
cn.meteor.beyondclouds.modules.post.service.IPostService
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.Assert
;
/**
* <p>
...
...
@@ -17,4 +28,129 @@ import org.springframework.stereotype.Service;
@Service
public
class
PostCommentServiceImpl
extends
ServiceImpl
<
PostCommentMapper
,
PostComment
>
implements
IPostCommentService
{
private
IPostService
postService
;
@Autowired
public
void
setPostService
(
IPostService
postService
)
{
this
.
postService
=
postService
;
}
/**
* 发布评论
* @param postId
* @param parentId
* @param comment
* @param userId
*/
@Override
public
void
createComment
(
String
postId
,
Integer
parentId
,
String
comment
,
String
userId
)
throws
PostServiceException
,
PostCommentServiceException
{
//1.判断动态是否存在
Post
post
=
postService
.
getById
(
postId
);
if
(
null
==
post
)
{
throw
new
PostServiceException
(
PostErrorCode
.
POST_NOT_FOUND
);
}
//2.如果有parentId判断父评论是否存在
PostComment
parentComment
=
null
;
if
(
null
!=
parentId
)
{
QueryWrapper
<
PostComment
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"post_id"
,
postId
);
queryWrapper
.
eq
(
"comment_id"
,
parentId
);
parentComment
=
getOne
(
queryWrapper
);
if
(
null
==
parentComment
)
{
throw
new
PostCommentServiceException
(
PostCommentErrorCode
.
PARENT_COMMENT_NOT_FOUND
);
}
}
//3.保存评论
PostComment
postComment
=
new
PostComment
();
postComment
.
setParentId
(
parentId
);
postComment
.
setComment
(
comment
);
postComment
.
setUserId
(
userId
);
postComment
.
setPostId
(
postId
);
save
(
postComment
);
//更新深度和路径
if
(
null
==
parentComment
)
{
//一级评论
postComment
.
setDepth
(
0
);
postComment
.
setThread
(
"/"
+
postComment
.
getCommentId
());
}
else
{
//子级评论
postComment
.
setDepth
(
parentComment
.
getDepth
()+
1
);
postComment
.
setThread
(
parentComment
.
getThread
()+
"/"
+
postComment
.
getCommentId
());
}
updateById
(
postComment
);
}
/**
* 删除评论
* @param commentId
* @param userId
* @throws PostCommentServiceException
*/
@Override
public
void
deleteComment
(
Integer
commentId
,
String
userId
)
throws
PostCommentServiceException
{
Assert
.
notNull
(
commentId
,
"commentId must not be null"
);
Assert
.
notNull
(
userId
,
"userId must not be null"
);
//1.判断是不是自己的评论
PostComment
postComment
=
getById
(
commentId
);
if
(
null
==
postComment
)
{
throw
new
PostCommentServiceException
(
PostCommentErrorCode
.
COMMENT_NOT_FOUND
);
}
//2.判断是不是自己的评论
if
(!
userId
.
equals
(
postComment
.
getUserId
()))
{
//3.判断是不是自己的博客
Post
post
=
postService
.
getById
(
postComment
.
getPostId
());
if
(!
post
.
getUserId
().
equals
(
userId
))
{
throw
new
PostCommentServiceException
(
PostCommentErrorCode
.
NO_DELETE_PRIVILEGES
);
}
}
QueryWrapper
postCommentQueryWrapper
=
new
QueryWrapper
();
postCommentQueryWrapper
.
like
(
"thread"
,
postComment
.
getThread
());
remove
(
postCommentQueryWrapper
);
}
/**
* 评论列表
* @param pageNumber
* @param size
* @param postId
* @param parentId
* @return
* @throws PostCommentServiceException
*/
@Override
public
IPage
<
PostComment
>
getCommentPage
(
Integer
pageNumber
,
Integer
size
,
String
postId
,
Integer
parentId
)
throws
PostCommentServiceException
{
Assert
.
notNull
(
postId
,
"postId must not null"
);
IPage
<
PostComment
>
page
=
new
Page
(
pageNumber
,
size
);
//如果parentId为null,则只获取一级评论
if
(
null
==
parentId
)
{
QueryWrapper
postCommentQueryWrapper
=
new
QueryWrapper
();
postCommentQueryWrapper
.
eq
(
"post_id"
,
postId
);
postCommentQueryWrapper
.
eq
(
"depth"
,
0
);
postCommentQueryWrapper
.
orderByDesc
(
"create_time"
);
return
page
(
page
,
postCommentQueryWrapper
);
}
//如果parentId不为null,则获取其子查询
PostComment
postComment
=
getById
(
parentId
);
if
(
null
==
postComment
)
{
throw
new
PostCommentServiceException
(
PostCommentErrorCode
.
PARENT_COMMENT_NOT_FOUND
);
}
QueryWrapper
postCommentQueryWrapper
=
new
QueryWrapper
();
postCommentQueryWrapper
.
eq
(
"parent_id"
,
parentId
);
//防止假传parentId
PostComment
sunComment
=
getOne
(
postCommentQueryWrapper
);
if
(
null
==
sunComment
)
{
throw
new
PostCommentServiceException
(
PostCommentErrorCode
.
SUN_COMMENT_NOT_FOUND
);
}
return
page
(
page
,
postCommentQueryWrapper
);
}
}
src/main/resources/application.yml
View file @
36733c3b
...
...
@@ -3,7 +3,7 @@ spring:
driver-class-name
:
com.mysql.cj.jdbc.Driver
url
:
jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username
:
root
password
:
1
00Centa30821%mysql
password
:
1
23123
swagger
:
enable
:
true
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment