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("删除失败,请重新尝试");
}
}
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("回复失败,请重新尝试!");
}
}
Aggregations