Search in sources :

Example 1 with CommentLike

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);
    }
}
Also used : CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) Comment(top.hcode.hoj.pojo.entity.discussion.Comment) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CommentLike

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;
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Example 3 with CommentLike

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(), "获取成功");
}
Also used : HashMap(java.util.HashMap) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) LinkedList(java.util.LinkedList) CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo)

Example 4 with CommentLike

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, "取消成功");
    }
}
Also used : CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) Comment(top.hcode.hoj.pojo.entity.discussion.Comment) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 CommentLike (top.hcode.hoj.pojo.entity.discussion.CommentLike)4 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 HttpSession (javax.servlet.http.HttpSession)2 Session (org.apache.shiro.session.Session)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Comment (top.hcode.hoj.pojo.entity.discussion.Comment)2 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)2 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)1 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)1 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)1 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)1 CommentsVo (top.hcode.hoj.pojo.vo.CommentsVo)1