-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathCommentController.java
More file actions
165 lines (146 loc) · 7.11 KB
/
CommentController.java
File metadata and controls
165 lines (146 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.jiaruiblog.api.controller;
import com.jiaruiblog.api.auth.Permission;
import com.jiaruiblog.application.service.ICommentService;
import com.jiaruiblog.application.service.converter.CommentConverter;
import com.jiaruiblog.common.ApiResult;
import com.jiaruiblog.common.exception.BusinessException;
import com.jiaruiblog.common.exception.ErrorCode;
import com.jiaruiblog.domain.entity.po.Comment;
import com.jiaruiblog.domain.entity.po.FileDocument;
import com.jiaruiblog.domain.entity.dto.BasePageDTO;
import com.jiaruiblog.domain.entity.dto.BatchIdDTO;
import com.jiaruiblog.domain.entity.dto.CommentDTO;
import com.jiaruiblog.domain.entity.dto.CommentListDTO;
import com.jiaruiblog.domain.entity.vo.CommentWithUserVO;
import com.jiaruiblog.domain.entity.vo.PageVO;
import com.jiaruiblog.common.enums.PermissionEnum;
import com.jiaruiblog.infrastructure.repository.DocumentRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 评论系统的控制器
* @author luojiarui
**/
@Tag(name = "评论模块", description = "评论相关接口")
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/api/v1/comment")
public class CommentController {
@Resource
ICommentService commentService;
@Resource
CommentConverter commentConverter;
@Resource
DocumentRepository documentRepository;
@Operation(summary = "新增单个评论", description = "添加新的评论")
@PostMapping(value = "/auth/insert")
public ApiResult<Void> insert(@RequestBody CommentDTO commentDTO, HttpServletRequest request) {
commentService.insert(getComment(commentDTO, request));
return ApiResult.success();
}
@Operation(summary = "更新评论", description = "修改现有评论内容")
@PostMapping(value = "/auth/update")
public ApiResult<Void> update(@RequestBody CommentDTO commentDTO, HttpServletRequest request) {
commentService.update(getComment(commentDTO, request));
return ApiResult.success();
}
@Operation(summary = "删除评论", description = "根据ID删除单个评论")
@DeleteMapping(value = "/auth/remove")
public ApiResult<Void> remove(@RequestBody Comment comment, HttpServletRequest request) {
String userId = (String) request.getAttribute("id");
if (!StringUtils.hasText(comment.getId())) {
throw new BusinessException(ErrorCode.COMMENT_NOT_FOUND);
}
commentService.remove(comment, userId);
return ApiResult.success();
}
@Permission(value = PermissionEnum.ADMIN)
@Operation(summary = "批量删除评论", description = "管理员批量删除评论")
@DeleteMapping(value = "/auth/removeBatch")
public ApiResult<Void> removeBatch(@RequestBody BatchIdDTO batchIdDTO) {
List<String> commentIdList = batchIdDTO.getIds();
if (CollectionUtils.isEmpty(commentIdList)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
commentService.removeBatch(commentIdList);
return ApiResult.success();
}
@Operation(summary = "查询文档评论", description = "根据文档ID获取相关评论")
@PostMapping(value = "/list")
public ApiResult<Map<String, Object>> queryById(@RequestBody CommentListDTO comment) {
Map<String, Object> result = commentService.queryById(comment);
return ApiResult.success(result);
}
private Comment getComment(CommentDTO commentDTO, HttpServletRequest request) {
commentDTO = Optional.ofNullable(commentDTO).orElseThrow(() ->
new BusinessException(ErrorCode.PARAMS_ERROR));
if (!StringUtils.hasText(commentDTO.getContent()) || !StringUtils.hasText(commentDTO.getDocId())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Comment comment = new Comment();
comment.setContent(commentDTO.getContent());
comment.setDocId(commentDTO.getDocId());
comment.setUserName((String) request.getAttribute("username"));
comment.setUserId((String) request.getAttribute("id"));
return comment;
}
@Operation(summary = "查询用户评论", description = "查询当前用户的评论列表")
@PostMapping(value = "/auth/myComments")
public ApiResult<PageVO<CommentWithUserVO>> queryMyComments(@RequestBody BasePageDTO pageDTO, HttpServletRequest request) {
String userId = (String) request.getAttribute("id");
PageVO<Comment> commentPage = commentService.queryAllComments(pageDTO, userId, false);
PageVO<CommentWithUserVO> result = buildCommentWithUserVO(commentPage);
return ApiResult.success(result);
}
@Operation(summary = "查询所有评论", description = "管理员查询所有用户的评论列表")
@Permission(PermissionEnum.ADMIN)
@PostMapping(value = "/auth/allComments")
public ApiResult<PageVO<CommentWithUserVO>> queryAllComments(@RequestBody BasePageDTO pageDTO) {
PageVO<Comment> commentPage = commentService.queryAllComments(pageDTO, null, true);
PageVO<CommentWithUserVO> result = buildCommentWithUserVO(commentPage);
return ApiResult.success(result);
}
private PageVO<CommentWithUserVO> buildCommentWithUserVO(PageVO<Comment> commentPage) {
if (commentPage == null || commentPage.getList() == null || commentPage.getList().isEmpty()) {
return PageVO.<CommentWithUserVO>builder()
.total(0)
.list(new ArrayList<>())
.pageNum(commentPage != null ? commentPage.getPageNum() : 1)
.pageSize(commentPage != null ? commentPage.getPageSize() : 10)
.build();
}
List<String> docIdList = commentPage.getList().stream()
.map(Comment::getDocId)
.collect(Collectors.toList());
List<FileDocument> documents = documentRepository.findByIdList(docIdList);
Map<String, String> docNameMap = documents.stream()
.collect(Collectors.toMap(FileDocument::getId, FileDocument::getName));
List<CommentWithUserVO> voList = commentPage.getList().stream()
.map(comment -> {
String docName = docNameMap.get(comment.getDocId());
if (docName == null) {
docName = comment.getDocName(); // fallback to stored docName when document is deleted
}
return commentConverter.toVO(comment, docName);
})
.collect(Collectors.toList());
return PageVO.<CommentWithUserVO>builder()
.total(commentPage.getTotal())
.list(voList)
.pageNum(commentPage.getPageNum())
.pageSize(commentPage.getPageSize())
.build();
}
}