|
@@ -0,0 +1,169 @@
|
|
|
+package com.ruoyi.generator.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.ruoyi.common.core.controller.BaseController;
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
+import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.generator.domain.Community.CommunityArticleComment;
|
|
|
+import com.ruoyi.generator.domain.Community.CommunityCommentLike;
|
|
|
+import com.ruoyi.generator.domain.Community.CommunityCommentReply;
|
|
|
+import com.ruoyi.generator.mapper.community.CommunityCommentLikeMapper;
|
|
|
+import com.ruoyi.generator.service.ICommunityArticleCommentService;
|
|
|
+import com.ruoyi.generator.service.ICommunityCommentReplyService;
|
|
|
+import com.ruoyi.generator.vo.CommunityArticleCommentVo;
|
|
|
+import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 评论接口
|
|
|
+ *
|
|
|
+ * @author fangzhen
|
|
|
+ */
|
|
|
+@Api(tags = "评论接口")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/community/comment")
|
|
|
+public class CommunityCommentController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICommunityCommentReplyService communityCommentReplyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICommunityArticleCommentService communityArticleCommentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysUserMapper sysUserMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommunityCommentLikeMapper communityCommentLikeMapper;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文章评论
|
|
|
+ */
|
|
|
+ @ApiOperation("获取文章评论")
|
|
|
+ @GetMapping()
|
|
|
+ @Transactional
|
|
|
+ //@Anonymous
|
|
|
+ public AjaxResult comment(String articleId, int currentPage, int limit) {
|
|
|
+ if (Objects.isNull(articleId)) {
|
|
|
+ return AjaxResult.error("参数异常!");
|
|
|
+ }
|
|
|
+ Page<CommunityArticleComment> page = new Page<>(currentPage, limit);
|
|
|
+ List<CommunityArticleComment> records = communityArticleCommentService.page(page, new QueryWrapper<CommunityArticleComment>()
|
|
|
+ .eq("article_id", articleId).and((wrapper) -> {
|
|
|
+ wrapper.ne("is_delete", true).or().isNull("is_delete");
|
|
|
+ }).orderByDesc("create_time")).getRecords();
|
|
|
+
|
|
|
+ CommunityArticleCommentVo commentVo = null;
|
|
|
+ List<CommunityArticleCommentVo> commentVos = new ArrayList<>();
|
|
|
+ for (CommunityArticleComment record : records) {
|
|
|
+ commentVo = new CommunityArticleCommentVo();
|
|
|
+ BeanUtils.copyProperties(record, commentVo);
|
|
|
+ commentVos.add(commentVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userId = SecurityUtils.getUserId();
|
|
|
+ for (CommunityArticleCommentVo vo : commentVos) {
|
|
|
+ Long commentUserId = vo.getUserId();
|
|
|
+ //获取评论的用户信息
|
|
|
+ SysUser sysUser = sysUserMapper.selectUserById(commentUserId);
|
|
|
+ vo.setUsername(sysUser.getUserName());
|
|
|
+ vo.setAvatar(sysUser.getAvatar());
|
|
|
+ //当前登录用户是否已点赞
|
|
|
+ List<CommunityCommentLike> commentLikes = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
|
|
|
+ .eq("comment_id", vo.getId())
|
|
|
+ .eq("user_id", userId));
|
|
|
+ vo.setCommentLike(!commentLikes.isEmpty());
|
|
|
+
|
|
|
+ //该评论的点赞数量
|
|
|
+ vo.setCommentLikeCount(communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>().eq("comment_id", vo.getId())).size());
|
|
|
+ }
|
|
|
+
|
|
|
+ return AjaxResult.success(commentVos);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取评论回复
|
|
|
+ */
|
|
|
+ @ApiOperation("获取评论回复")
|
|
|
+ @GetMapping("/reply")
|
|
|
+ @Transactional
|
|
|
+ //@Anonymous
|
|
|
+ public AjaxResult reply(String commentId, int currentPage, int limit) {
|
|
|
+ if (Objects.isNull(commentId)) {
|
|
|
+ return AjaxResult.error("参数异常!");
|
|
|
+ }
|
|
|
+ Page<CommunityCommentReply> page = new Page<>(currentPage, limit);
|
|
|
+ List<CommunityCommentReply> replies = communityCommentReplyService
|
|
|
+ .page(page, new QueryWrapper<CommunityCommentReply>()
|
|
|
+ .eq("comment_id", commentId).and((wrapper) -> {
|
|
|
+ wrapper.ne("is_delete", true).or().isNull("is_delete");
|
|
|
+ })
|
|
|
+ .orderByDesc("create_time")).getRecords();
|
|
|
+ return AjaxResult.success(replies);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对文章评论进行回复
|
|
|
+ */
|
|
|
+ @ApiOperation("对文章评论进行回复")
|
|
|
+ @PostMapping("/reply")
|
|
|
+ @Transactional
|
|
|
+ //@Anonymous
|
|
|
+ public AjaxResult reply(@RequestBody CommunityCommentReply communityCommentReply) {
|
|
|
+ if (Objects.isNull(communityCommentReply.getCommentId())
|
|
|
+ || Objects.isNull(communityCommentReply.getContent())) {
|
|
|
+ return AjaxResult.error("参数异常!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userId = SecurityUtils.getUserId();
|
|
|
+ communityCommentReply.setUserId(userId);
|
|
|
+ communityCommentReply.setDelete(false);
|
|
|
+ communityCommentReply.setCreateBy(userId);
|
|
|
+ communityCommentReply.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
|
|
|
+ communityCommentReplyService.save(communityCommentReply);
|
|
|
+ return AjaxResult.success("新增回复成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除回复
|
|
|
+ */
|
|
|
+ @ApiOperation("删除回复")
|
|
|
+ @DeleteMapping("/deleteReply")
|
|
|
+ @Transactional
|
|
|
+ //@Anonymous
|
|
|
+ public AjaxResult deleteReply(String replyId) {
|
|
|
+ if (Objects.isNull(replyId)) {
|
|
|
+ return AjaxResult.error("参数异常!");
|
|
|
+ }
|
|
|
+
|
|
|
+ CommunityCommentReply communityCommentReply = communityCommentReplyService.getOne(new QueryWrapper<CommunityCommentReply>()
|
|
|
+ .eq("id", replyId)
|
|
|
+ .and((wrapper) -> {
|
|
|
+ wrapper.ne("is_delete", true).or().isNull("is_delete");
|
|
|
+ }));
|
|
|
+ if (Objects.isNull(communityCommentReply)) {
|
|
|
+ return AjaxResult.error("未找到该回复!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userId = SecurityUtils.getUserId();
|
|
|
+ communityCommentReply.setDelete(true);
|
|
|
+ communityCommentReply.setUpdateBy(userId);
|
|
|
+ communityCommentReply.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
|
|
|
+ communityCommentReplyService.updateById(communityCommentReply);
|
|
|
+ return AjaxResult.success("删除回复成功!");
|
|
|
+ }
|
|
|
+}
|