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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations