Search in sources :

Example 1 with Discussion

use of top.hcode.hoj.pojo.entity.discussion.Discussion in project HOJ by HimitZH.

the class DiscussionController method addDiscussionLike.

@GetMapping("/discussion-like")
@Transactional(rollbackFor = Exception.class)
@RequiresAuthentication
public CommonResult addDiscussionLike(@RequestParam("did") Integer did, @RequestParam("toLike") Boolean toLike, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<DiscussionLike> discussionLikeQueryWrapper = new QueryWrapper<>();
    discussionLikeQueryWrapper.eq("did", did).eq("uid", userRolesVo.getUid());
    DiscussionLike discussionLike = discussionLikeService.getOne(discussionLikeQueryWrapper, false);
    if (toLike) {
        // 添加点赞
        if (discussionLike == null) {
            // 如果不存在就添加
            boolean isSave = discussionLikeService.saveOrUpdate(new DiscussionLike().setUid(userRolesVo.getUid()).setDid(did));
            if (!isSave) {
                return CommonResult.errorResponse("点赞失败,请重试尝试!");
            }
        }
        // 点赞+1
        Discussion discussion = discussionService.getById(did);
        if (discussion != null) {
            discussion.setLikeNum(discussion.getLikeNum() + 1);
            discussionService.updateById(discussion);
            // 更新点赞消息
            discussionService.updatePostLikeMsg(discussion.getUid(), userRolesVo.getUid(), did);
        }
        return CommonResult.successResponse(null, "点赞成功");
    } else {
        // 取消点赞
        if (discussionLike != null) {
            // 如果存在就删除
            boolean isDelete = discussionLikeService.removeById(discussionLike.getId());
            if (!isDelete) {
                return CommonResult.errorResponse("取消点赞失败,请重试尝试!");
            }
        }
        // 点赞-1
        UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
        discussionUpdateWrapper.setSql("like_num=like_num-1").eq("id", did);
        discussionService.update(discussionUpdateWrapper);
        return CommonResult.successResponse(null, "取消成功");
    }
}
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) DiscussionLike(top.hcode.hoj.pojo.entity.discussion.DiscussionLike) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Discussion

use of top.hcode.hoj.pojo.entity.discussion.Discussion in project HOJ by HimitZH.

the class DiscussionController method getDiscussion.

@GetMapping("/discussion")
public CommonResult getDiscussion(@RequestParam(value = "did", required = true) Integer did, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    String uid = null;
    if (userRolesVo != null) {
        uid = userRolesVo.getUid();
    }
    DiscussionVo discussion = discussionService.getDiscussion(did, uid);
    if (discussion == null) {
        return CommonResult.errorResponse("对不起,该讨论不存在!", CommonResult.STATUS_NOT_FOUND);
    }
    if (discussion.getStatus() == 1) {
        return CommonResult.errorResponse("对不起,该讨论已被封禁!", CommonResult.STATUS_FORBIDDEN);
    }
    // 浏览量+1
    UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
    discussionUpdateWrapper.setSql("view_num=view_num+1").eq("id", discussion.getId());
    discussionService.update(discussionUpdateWrapper);
    discussion.setViewNum(discussion.getViewNum() + 1);
    return CommonResult.successResponse(discussion, "获取成功");
}
Also used : DiscussionVo(top.hcode.hoj.pojo.vo.DiscussionVo) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion)

Example 3 with Discussion

use of top.hcode.hoj.pojo.entity.discussion.Discussion in project HOJ by HimitZH.

the class CommentController method addReply.

@PostMapping("/reply")
@RequiresPermissions("reply_add")
@RequiresAuthentication
public CommonResult addReply(@RequestBody ReplyDto replyDto, HttpServletRequest request) {
    if (StringUtils.isEmpty(replyDto.getReply().getContent().trim())) {
        return CommonResult.errorResponse("回复内容不能为空!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    Reply reply = replyDto.getReply();
    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 = replyService.saveOrUpdate(reply);
    if (isOk) {
        // 如果是讨论区的回复,发布成功需要增加统计该讨论的回复数
        if (replyDto.getDid() != null) {
            UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
            discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num+1");
            discussionService.update(discussionUpdateWrapper);
            // 更新消息
            replyService.updateReplyMsg(replyDto.getDid(), "Discussion", reply.getContent(), replyDto.getQuoteId(), replyDto.getQuoteType(), reply.getToUid(), reply.getFromUid());
        }
        return CommonResult.successResponse(reply, "回复成功");
    } else {
        return CommonResult.errorResponse("回复失败,请重新尝试!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Reply(top.hcode.hoj.pojo.entity.discussion.Reply) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 4 with Discussion

use of top.hcode.hoj.pojo.entity.discussion.Discussion in project HOJ by HimitZH.

the class CommentController method deleteReply.

@DeleteMapping("/reply")
@RequiresAuthentication
public CommonResult deleteReply(@RequestBody ReplyDto replyDto, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    Reply reply = replyDto.getReply();
    // 如果不是评论本人 或者不是管理员 无权限删除该评论
    if (reply.getFromUid().equals(userRolesVo.getUid()) || SecurityUtils.getSubject().hasRole("root") || SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        // 删除该数据
        boolean isOk = replyService.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");
                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) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Reply(top.hcode.hoj.pojo.entity.discussion.Reply) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 5 with Discussion

use of top.hcode.hoj.pojo.entity.discussion.Discussion in project HOJ by HimitZH.

the class CommentController method addComment.

@PostMapping("/comment")
@RequiresPermissions("comment_add")
@RequiresAuthentication
@Transactional
public CommonResult addComment(@RequestBody Comment comment, HttpServletRequest request) {
    if (StringUtils.isEmpty(comment.getContent().trim())) {
        return CommonResult.errorResponse("评论内容不能为空!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 比赛外的评论 除管理员外 只有AC 10道以上才可评论
    if (comment.getCid() == null) {
        if (!SecurityUtils.getSubject().hasRole("root") && !SecurityUtils.getSubject().hasRole("admin") && !SecurityUtils.getSubject().hasRole("problem_admin")) {
            QueryWrapper<UserAcproblem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", userRolesVo.getUid()).select("distinct pid");
            int userAcProblemCount = userAcproblemService.count(queryWrapper);
            if (userAcProblemCount < 10) {
                return CommonResult.errorResponse("对不起,您暂时不能评论!请先去提交题目通过10道以上~", CommonResult.STATUS_FORBIDDEN);
            }
        }
    }
    comment.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
    if (SecurityUtils.getSubject().hasRole("root")) {
        comment.setFromRole("root");
    } else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        comment.setFromRole("admin");
    } else {
        comment.setFromRole("user");
    }
    // 带有表情的字符串转换为编码
    comment.setContent(EmojiUtil.toHtml(comment.getContent()));
    boolean isOk = commentService.saveOrUpdate(comment);
    if (isOk) {
        CommentsVo commentsVo = new CommentsVo();
        commentsVo.setContent(comment.getContent());
        commentsVo.setId(comment.getId());
        commentsVo.setFromAvatar(comment.getFromAvatar());
        commentsVo.setFromName(comment.getFromName());
        commentsVo.setFromUid(comment.getFromUid());
        commentsVo.setLikeNum(0);
        commentsVo.setGmtCreate(comment.getGmtCreate());
        commentsVo.setReplyList(new LinkedList<>());
        // 如果是讨论区的回复,发布成功需要添加统计该讨论的回复数
        if (comment.getDid() != null) {
            Discussion discussion = discussionService.getById(comment.getDid());
            if (discussion != null) {
                discussion.setCommentNum(discussion.getCommentNum() + 1);
                discussionService.updateById(discussion);
                // 更新消息
                commentService.updateCommentMsg(discussion.getUid(), userRolesVo.getUid(), comment.getContent(), comment.getDid());
            }
        }
        return CommonResult.successResponse(commentsVo, "评论成功");
    } else {
        return CommonResult.errorResponse("评论失败,请重新尝试!");
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)27 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)15 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)13 Session (org.apache.shiro.session.Session)13 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)12 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)10 HttpSession (javax.servlet.http.HttpSession)8 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)7 Contest (top.hcode.hoj.pojo.entity.contest.Contest)7 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)6 Transactional (org.springframework.transaction.annotation.Transactional)6 Reply (top.hcode.hoj.pojo.entity.discussion.Reply)6 Comment (top.hcode.hoj.pojo.entity.discussion.Comment)5 IPage (com.baomidou.mybatisplus.core.metadata.IPage)4 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)4 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)4 UserMsgVo (top.hcode.hoj.pojo.vo.UserMsgVo)4 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 Group (top.hcode.hoj.pojo.entity.group.Group)3 UserAcproblem (top.hcode.hoj.pojo.entity.user.UserAcproblem)3