use of top.hcode.hoj.pojo.entity.discussion.CommentLike in project HOJ by HimitZH.
the class CommentManager method addCommentLike.
@Transactional(rollbackFor = Exception.class)
public void addCommentLike(Integer cid, Boolean toLike, Integer sourceId, String sourceType) throws StatusFailException {
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
commentLikeQueryWrapper.eq("cid", cid).eq("uid", userRolesVo.getUid());
CommentLike commentLike = commentLikeEntityService.getOne(commentLikeQueryWrapper, false);
if (toLike) {
// 添加点赞
if (commentLike == null) {
// 如果不存在就添加
boolean isSave = commentLikeEntityService.saveOrUpdate(new CommentLike().setUid(userRolesVo.getUid()).setCid(cid));
if (!isSave) {
throw new StatusFailException("点赞失败,请重试尝试!");
}
}
// 点赞+1
Comment comment = commentEntityService.getById(cid);
if (comment != null) {
comment.setLikeNum(comment.getLikeNum() + 1);
commentEntityService.updateById(comment);
// 当前的评论要不是点赞者的 才发送点赞消息
if (!userRolesVo.getUsername().equals(comment.getFromName())) {
commentEntityService.updateCommentLikeMsg(comment.getFromUid(), userRolesVo.getUid(), sourceId, sourceType);
}
}
} else {
// 取消点赞
if (commentLike != null) {
// 如果存在就删除
boolean isDelete = commentLikeEntityService.removeById(commentLike.getId());
if (!isDelete) {
throw new StatusFailException("取消点赞失败,请重试尝试!");
}
}
// 点赞-1
UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();
commentUpdateWrapper.setSql("like_num=like_num-1").eq("id", cid);
commentEntityService.update(commentUpdateWrapper);
}
}
use of top.hcode.hoj.pojo.entity.discussion.CommentLike in project HOJ by HimitZH.
the class CommentManager method getComments.
public CommentListVo getComments(Long cid, Integer did, Integer limit, Integer currentPage) throws StatusForbiddenException, AccessException {
// 如果有登录,则获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
if (cid == null && did != null) {
QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
discussionQueryWrapper.select("id", "gid").eq("id", did);
Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
if (discussion != null && discussion.getGid() != null) {
accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
if (!isRoot && !groupValidator.isGroupMember(userRolesVo.getUid(), discussion.getGid())) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
} else {
accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
}
} else {
accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
}
IPage<CommentVo> commentList = commentEntityService.getCommentList(limit, currentPage, cid, did, isRoot, userRolesVo != null ? userRolesVo.getUid() : null);
HashMap<Integer, Boolean> commentLikeMap = new HashMap<>();
if (userRolesVo != null) {
// 如果是有登录 需要检查是否对评论有点赞
List<Integer> commentIdList = new LinkedList<>();
for (CommentVo commentVo : commentList.getRecords()) {
commentIdList.add(commentVo.getId());
}
if (commentIdList.size() > 0) {
QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
commentLikeQueryWrapper.in("cid", commentIdList);
List<CommentLike> commentLikeList = commentLikeEntityService.list(commentLikeQueryWrapper);
// 如果存在记录需要修正Map为true
for (CommentLike tmp : commentLikeList) {
commentLikeMap.put(tmp.getCid(), true);
}
}
}
CommentListVo commentListVo = new CommentListVo();
commentListVo.setCommentList(commentList);
commentListVo.setCommentLikeMap(commentLikeMap);
return commentListVo;
}
use of top.hcode.hoj.pojo.entity.discussion.CommentLike in project HOJ by HimitZH.
the class CommentController method getComments.
@GetMapping("/comments")
public CommonResult getComments(@RequestParam(value = "cid", required = false) Long cid, @RequestParam(value = "did", required = false) Integer did, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage, HttpServletRequest request) {
// 如果有登录,则获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
IPage<CommentsVo> commentList = commentService.getCommentList(limit, currentPage, cid, did, isRoot, userRolesVo != null ? userRolesVo.getUid() : null);
HashMap<Integer, Boolean> commentLikeMap = new HashMap<>();
if (userRolesVo != null) {
// 如果是有登录 需要检查是否对评论有点赞
List<Integer> commentIdList = new LinkedList<>();
for (CommentsVo commentsVo : commentList.getRecords()) {
commentIdList.add(commentsVo.getId());
}
if (commentIdList.size() > 0) {
QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
commentLikeQueryWrapper.in("cid", commentIdList);
List<CommentLike> commentLikeList = commentLikeService.list(commentLikeQueryWrapper);
// 如果存在记录需要修正Map为true
for (CommentLike tmp : commentLikeList) {
commentLikeMap.put(tmp.getCid(), true);
}
}
}
return CommonResult.successResponse(MapUtil.builder().put("commentList", commentList).put("commentLikeMap", commentLikeMap).map(), "获取成功");
}
use of top.hcode.hoj.pojo.entity.discussion.CommentLike in project HOJ by HimitZH.
the class CommentController method addDiscussionLike.
@GetMapping("/comment-like")
@RequiresAuthentication
@Transactional
public CommonResult addDiscussionLike(@RequestParam("cid") Integer cid, @RequestParam("toLike") Boolean toLike, @RequestParam("sourceId") Integer sourceId, @RequestParam("sourceType") String sourceType, HttpServletRequest request) {
// 获取当前登录的用户
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
commentLikeQueryWrapper.eq("cid", cid).eq("uid", userRolesVo.getUid());
CommentLike commentLike = commentLikeService.getOne(commentLikeQueryWrapper, false);
if (toLike) {
// 添加点赞
if (commentLike == null) {
// 如果不存在就添加
boolean isSave = commentLikeService.saveOrUpdate(new CommentLike().setUid(userRolesVo.getUid()).setCid(cid));
if (!isSave) {
return CommonResult.errorResponse("点赞失败,请重试尝试!");
}
}
// 点赞+1
Comment comment = commentService.getById(cid);
if (comment != null) {
comment.setLikeNum(comment.getLikeNum() + 1);
commentService.updateById(comment);
commentService.updateCommentLikeMsg(comment.getFromUid(), userRolesVo.getUid(), sourceId, sourceType);
}
return CommonResult.successResponse(null, "点赞成功");
} else {
// 取消点赞
if (commentLike != null) {
// 如果存在就删除
boolean isDelete = commentLikeService.removeById(commentLike.getId());
if (!isDelete) {
return CommonResult.errorResponse("取消点赞失败,请重试尝试!");
}
}
// 点赞-1
UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();
commentUpdateWrapper.setSql("like_num=like_num-1").eq("id", cid);
commentService.update(commentUpdateWrapper);
return CommonResult.successResponse(null, "取消成功");
}
}
Aggregations