Search in sources :

Example 31 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper 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 32 with UpdateWrapper

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

the class GroupContestProblemManager method deleteContestProblem.

public void deleteContestProblem(Long pid, Long cid) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Contest contest = contestEntityService.getById(cid);
    if (contest == null) {
        throw new StatusNotFoundException("该比赛不存在!");
    }
    Long gid = contest.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUid().equals(contest.getUid()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).eq("pid", pid);
    boolean isOk = contestProblemEntityService.remove(contestProblemQueryWrapper);
    if (isOk) {
        UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
        judgeUpdateWrapper.eq("cid", cid).eq("pid", pid);
        judgeEntityService.remove(judgeUpdateWrapper);
    } else {
        throw new StatusFailException("删除失败!");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Session(org.apache.shiro.session.Session)

Example 33 with UpdateWrapper

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

the class GroupProblemManager method changeProblemAuth.

public void changeProblemAuth(Long pid, Integer auth) throws StatusForbiddenException, StatusNotFoundException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Problem problem = problemEntityService.getById(pid);
    if (problem == null) {
        throw new StatusNotFoundException("该题目不存在!");
    }
    Long gid = problem.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUsername().equals(problem.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    UpdateWrapper<Problem> problemUpdateWrapper = new UpdateWrapper<>();
    problemUpdateWrapper.eq("id", pid).set("auth", auth).set("modified_user", userRolesVo.getUsername());
    boolean isOk = problemEntityService.update(problemUpdateWrapper);
    if (!isOk) {
        throw new StatusFailException("修改失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) Problem(top.hcode.hoj.pojo.entity.problem.Problem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 34 with UpdateWrapper

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

the class GroupTrainingManager method updateTraining.

@Transactional(rollbackFor = Exception.class)
public void updateTraining(TrainingDto trainingDto) throws StatusForbiddenException, StatusNotFoundException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Long tid = trainingDto.getTraining().getId();
    Training training = trainingEntityService.getById(tid);
    if (training == null) {
        throw new StatusNotFoundException("该训练不存在!");
    }
    Long gid = training.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUsername().equals(training.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    trainingDto.getTraining().setIsGroup(training.getIsGroup());
    trainingEntityService.updateById(trainingDto.getTraining());
    if (trainingDto.getTraining().getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        if (!Objects.equals(training.getPrivatePwd(), trainingDto.getTraining().getPrivatePwd())) {
            UpdateWrapper<TrainingRegister> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", tid);
            trainingRegisterEntityService.remove(updateWrapper);
        }
    }
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getGid() != null && trainingCategory.getGid().longValue() != gid) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    if (trainingCategory.getId() == null) {
        try {
            trainingCategory.setGid(gid);
            trainingCategoryEntityService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
        }
    }
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryEntityService.getOne(new QueryWrapper<MappingTrainingCategory>().eq("tid", training.getId()), false);
    if (mappingTrainingCategory == null) {
        mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
        adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
    } else {
        if (!mappingTrainingCategory.getCid().equals(trainingCategory.getId())) {
            UpdateWrapper<MappingTrainingCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId()).set("cid", trainingCategory.getId());
            boolean isOk = mappingTrainingCategoryEntityService.update(null, updateWrapper);
            if (isOk) {
                adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
            } else {
                throw new StatusFailException("修改失败");
            }
        }
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with UpdateWrapper

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

the class GroupTrainingProblemManager method addProblemFromGroup.

@Transactional(rollbackFor = Exception.class)
public void addProblemFromGroup(String problemId, Long tid) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Training training = trainingEntityService.getById(tid);
    if (training == null) {
        throw new StatusNotFoundException("该训练不存在!");
    }
    Long gid = training.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUsername().equals(training.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
    problemQueryWrapper.eq("problem_id", problemId).eq("gid", gid);
    Problem problem = problemEntityService.getOne(problemQueryWrapper);
    if (problem == null) {
        throw new StatusNotFoundException("该题目不存在或不是团队题目!");
    }
    QueryWrapper<TrainingProblem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", problem.getId()).or().eq("display_id", problem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(queryWrapper);
    if (trainingProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean isOk = trainingProblemEntityService.save(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (isOk) {
        UpdateWrapper<Training> trainingUpdateWrapper = new UpdateWrapper<>();
        trainingUpdateWrapper.set("gmt_modified", new Date()).eq("id", tid);
        trainingEntityService.update(trainingUpdateWrapper);
        adminTrainingRecordManager.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
    } else {
        throw new StatusFailException("添加失败!");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) Date(java.util.Date) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

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