Search in sources :

Example 71 with UpdateWrapper

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

the class CommentController method deleteComment.

@DeleteMapping("/comment")
@RequiresAuthentication
@Transactional
public CommonResult deleteComment(@RequestBody Comment comment, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 如果不是评论本人 或者不是管理员 无权限删除该评论
    if (comment.getFromUid().equals(userRolesVo.getUid()) || SecurityUtils.getSubject().hasRole("root") || SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        // 获取需要删除该评论的回复数
        int replyNum = replyService.count(new QueryWrapper<Reply>().eq("comment_id", comment.getId()));
        // 删除该数据 包括关联外键的reply表数据
        boolean isDeleteComment = commentService.removeById(comment.getId());
        // 同时需要删除该评论的回复表数据
        replyService.remove(new UpdateWrapper<Reply>().eq("comment_id", comment.getId()));
        if (isDeleteComment) {
            // 如果是讨论区的回复,删除成功需要减少统计该讨论的回复数
            if (comment.getDid() != null) {
                UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
                discussionUpdateWrapper.eq("id", comment.getDid()).setSql("comment_num=comment_num-" + (replyNum + 1));
                discussionService.update(discussionUpdateWrapper);
            }
            return CommonResult.successResponse(null, "删除成功");
        } else {
            return CommonResult.errorResponse("删除失败,请重新尝试");
        }
    } else {
        return CommonResult.errorResponse("无权删除该评论", CommonResult.STATUS_FORBIDDEN);
    }
}
Also used : 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) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 72 with UpdateWrapper

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

the class AdminContestController method updateContest.

@PutMapping("")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult updateContest(@RequestBody AdminContestVo adminContestVo, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和比赛拥有者才能操作
    if (!isRoot && !userRolesVo.getUid().equals(adminContestVo.getUid())) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    Contest contest = BeanUtil.copyProperties(adminContestVo, Contest.class, "starAccount");
    JSONObject accountJson = new JSONObject();
    accountJson.set("star_account", adminContestVo.getStarAccount());
    contest.setStarAccount(accountJson.toString());
    Contest oldContest = contestService.getById(contest.getId());
    boolean result = contestService.saveOrUpdate(contest);
    if (result) {
        if (!contest.getAuth().equals(Constants.Contest.AUTH_PUBLIC.getCode())) {
            if (!Objects.equals(oldContest.getPwd(), contest.getPwd())) {
                // 改了比赛密码则需要删掉已有的注册比赛用户
                UpdateWrapper<ContestRegister> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("cid", contest.getId());
                contestRegisterService.remove(updateWrapper);
            }
        }
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) JSONObject(cn.hutool.json.JSONObject) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 73 with UpdateWrapper

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

the class AdminProblemController method updateProblem.

@PutMapping("")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult updateProblem(@RequestBody ProblemDto problemDto, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    // 只有超级管理员和题目管理员、题目创建者才能操作
    if (!isRoot && !isProblemAdmin && !userRolesVo.getUsername().equals(problemDto.getProblem().getAuthor())) {
        return CommonResult.errorResponse("对不起,你无权限修改题目!", CommonResult.STATUS_FORBIDDEN);
    }
    String problemId = problemDto.getProblem().getProblemId().toUpperCase();
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", problemId);
    Problem problem = problemService.getOne(queryWrapper);
    // 如果problem_id不是原来的且已存在该problem_id,则修改失败!
    if (problem != null && problem.getId().longValue() != problemDto.getProblem().getId()) {
        return CommonResult.errorResponse("当前的Problem ID 已被使用,请重新更换新的!", CommonResult.STATUS_FAIL);
    }
    // 记录修改题目的用户
    problemDto.getProblem().setModifiedUser(userRolesVo.getUsername());
    boolean result = problemService.adminUpdateProblem(problemDto);
    if (result) {
        // 更新成功
        if (problem == null) {
            // 说明改了problemId,同步一下judge表
            UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
            judgeUpdateWrapper.eq("pid", problemDto.getProblem().getId()).set("display_pid", problemId);
            judgeService.update(judgeUpdateWrapper);
        }
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : 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) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 74 with UpdateWrapper

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

the class UserRecordEntityServiceImpl method updateRecord.

/**
 * @MethodNameupdateRecord
 * @Params  * @param null
 * @Description 本方法不启用,不适合数据一致性
 * @Return
 * @Since 2021/6/2
 */
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
@Async
@Override
@Deprecated
public void updateRecord(String uid, Long submitId, Long pid, Integer score) {
    QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
    judgeQueryWrapper.isNotNull("score").eq("cid", // 非比赛提交
    0).eq("pid", pid).eq("uid", uid).ne("submit_id", submitId).orderByDesc("score").last("limit 1");
    Judge lastHighScoreJudge = judgeEntityService.getOne(judgeQueryWrapper);
    // 之前没有提交过,那么就需要修改
    boolean result = true;
    if (lastHighScoreJudge == null) {
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + score).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    } else if (lastHighScoreJudge.getScore() < score) {
        // 如果之前该题目最高得分的提交比现在得分低,也需要修改
        int addValue = score - lastHighScoreJudge.getScore();
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    }
    if (result) {
        return;
    } else {
        // 失败则开始尝试
        tryAgainUpdate(uid, score);
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Async(org.springframework.scheduling.annotation.Async) Transactional(org.springframework.transaction.annotation.Transactional)

Example 75 with UpdateWrapper

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

the class RemoteJudgeContext method buildJudgeStrategy.

private RemoteJudgeStrategy buildJudgeStrategy(RemoteJudgeDTO remoteJudgeDTO) {
    RemoteJudgeStrategy remoteJudgeStrategy = RemoteJudgeFactory.selectJudge(remoteJudgeDTO.getOj());
    if (remoteJudgeStrategy == null) {
        // 更新此次提交状态为系统失败!
        UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
        judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()).set("error_message", "The judge server does not support this oj:" + remoteJudgeDTO.getOj()).eq("submit_id", remoteJudgeDTO.getJudgeId());
        judgeEntityService.update(judgeUpdateWrapper);
        return null;
    }
    remoteJudgeStrategy.setRemoteJudgeDTO(remoteJudgeDTO);
    return remoteJudgeStrategy;
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) RemoteJudgeStrategy(top.hcode.hoj.remoteJudge.task.RemoteJudgeStrategy) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

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