Search in sources :

Example 96 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class CommentManager method deleteReply.

public void deleteReply(ReplyDto replyDto) throws StatusForbiddenException, StatusFailException, AccessException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
    Reply reply = replyDto.getReply();
    Comment comment = commentEntityService.getById(reply.getCommentId());
    Long cid = comment.getCid();
    if (cid == null) {
        Discussion discussion = discussionEntityService.getById(comment.getDid());
        Long gid = discussion.getGid();
        if (gid == null) {
            accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
            if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !isProblemAdmin && !isAdmin) {
                throw new StatusForbiddenException("无权删除该回复");
            }
        } else {
            accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
            if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !groupValidator.isGroupAdmin(userRolesVo.getUid(), gid)) {
                throw new StatusForbiddenException("无权删除该回复");
            }
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
        Contest contest = contestEntityService.getById(cid);
        if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
            throw new StatusForbiddenException("无权删除该回复");
        }
    }
    boolean isOk = replyEntityService.removeById(reply.getId());
    if (isOk) {
        // 如果是讨论区的回复,删除成功需要减少统计该讨论的回复数
        if (replyDto.getDid() != null) {
            UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
            discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num-1");
            discussionEntityService.update(discussionUpdateWrapper);
        }
    } else {
        throw new StatusFailException("删除失败,请重新尝试");
    }
}
Also used : Comment(top.hcode.hoj.pojo.entity.discussion.Comment) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Reply(top.hcode.hoj.pojo.entity.discussion.Reply) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Example 97 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class CommentManager method addReply.

public ReplyVo addReply(ReplyDto replyDto) throws StatusFailException, StatusForbiddenException, AccessException {
    if (StringUtils.isEmpty(replyDto.getReply().getContent().trim())) {
        throw new StatusFailException("回复内容不能为空!");
    }
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
    Reply reply = replyDto.getReply();
    Comment comment = commentEntityService.getById(reply.getCommentId());
    Long cid = comment.getCid();
    if (cid == null) {
        QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
        discussionQueryWrapper.select("id", "gid").eq("id", comment.getDid());
        Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
        Long gid = discussion.getGid();
        if (gid != null) {
            accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
            if (!groupValidator.isGroupMember(userRolesVo.getUid(), gid) && !isRoot) {
                throw new StatusForbiddenException("对不起,您无权限回复!");
            }
        } else {
            accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
        }
        if (!isRoot && !isProblemAdmin && !isAdmin) {
            QueryWrapper<UserAcproblem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", userRolesVo.getUid()).select("distinct pid");
            int userAcProblemCount = userAcproblemEntityService.count(queryWrapper);
            if (userAcProblemCount < configVo.getDefaultCreateCommentACInitValue()) {
                throw new StatusForbiddenException("对不起,您暂时不能回复!请先去提交题目通过" + configVo.getDefaultCreateCommentACInitValue() + "道以上!");
            }
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
        Contest contest = contestEntityService.getById(cid);
        Long gid = contest.getGid();
        if (!comment.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), gid))) {
            throw new StatusForbiddenException("对不起,您无权限回复!");
        }
    }
    reply.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
    if (SecurityUtils.getSubject().hasRole("root")) {
        reply.setFromRole("root");
    } else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        reply.setFromRole("admin");
    } else {
        reply.setFromRole("user");
    }
    // 带有表情的字符串转换为编码
    reply.setContent(EmojiUtil.toHtml(reply.getContent()));
    boolean isOk = replyEntityService.saveOrUpdate(reply);
    if (isOk) {
        // 如果是讨论区的回复,发布成功需要增加统计该讨论的回复数
        if (replyDto.getDid() != null) {
            UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
            discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num+1");
            discussionEntityService.update(discussionUpdateWrapper);
            // 更新消息
            replyEntityService.updateReplyMsg(replyDto.getDid(), "Discussion", reply.getContent(), replyDto.getQuoteId(), replyDto.getQuoteType(), reply.getToUid(), reply.getFromUid());
        }
        ReplyVo replyVo = new ReplyVo();
        BeanUtil.copyProperties(reply, replyVo);
        replyVo.setFromTitleName(userRolesVo.getTitleName());
        replyVo.setFromTitleColor(userRolesVo.getTitleColor());
        return replyVo;
    } else {
        throw new StatusFailException("回复失败,请重新尝试!");
    }
}
Also used : Comment(top.hcode.hoj.pojo.entity.discussion.Comment) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Reply(top.hcode.hoj.pojo.entity.discussion.Reply) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Aggregations

UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)97 Transactional (org.springframework.transaction.annotation.Transactional)41 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)40 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)34 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)28 Session (org.apache.shiro.session.Session)24 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)21 Judge (top.hcode.hoj.pojo.entity.judge.Judge)17 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)16 HttpSession (javax.servlet.http.HttpSession)14 Problem (top.hcode.hoj.pojo.entity.problem.Problem)14 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)13 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)11 Group (top.hcode.hoj.pojo.entity.group.Group)11 Date (java.util.Date)10 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)10 Contest (top.hcode.hoj.pojo.entity.contest.Contest)8 User (com.baomidou.mybatisplus.samples.wrapper.entity.User)5 Result (org.jeecg.common.api.vo.Result)5 LoginUser (org.jeecg.common.system.vo.LoginUser)5