Search in sources :

Example 1 with DiscussionLike

use of top.hcode.hoj.pojo.entity.discussion.DiscussionLike 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 DiscussionLike

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

the class DiscussionManager method addDiscussionLike.

@Transactional(rollbackFor = Exception.class)
public void addDiscussionLike(Integer did, boolean toLike) throws StatusFailException, StatusForbiddenException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    Discussion discussion = discussionEntityService.getById(did);
    if (discussion.getGid() != null) {
        boolean isRoot = SecurityUtils.getSubject().hasRole("root");
        if (!isRoot && !discussion.getUid().equals(userRolesVo.getUid()) && !groupValidator.isGroupMember(userRolesVo.getUid(), discussion.getGid())) {
            throw new StatusForbiddenException("对不起,您无权限操作!");
        }
    }
    QueryWrapper<DiscussionLike> discussionLikeQueryWrapper = new QueryWrapper<>();
    discussionLikeQueryWrapper.eq("did", did).eq("uid", userRolesVo.getUid());
    DiscussionLike discussionLike = discussionLikeEntityService.getOne(discussionLikeQueryWrapper, false);
    if (toLike) {
        // 添加点赞
        if (discussionLike == null) {
            // 如果不存在就添加
            boolean isSave = discussionLikeEntityService.saveOrUpdate(new DiscussionLike().setUid(userRolesVo.getUid()).setDid(did));
            if (!isSave) {
                throw new StatusFailException("点赞失败,请重试尝试!");
            }
        }
        // 点赞+1
        UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
        discussionUpdateWrapper.eq("id", discussion.getId()).setSql("like_num=like_num+1");
        discussionEntityService.update(discussionUpdateWrapper);
        // 当前帖子要不是点赞者的 才发送点赞消息
        if (!userRolesVo.getUsername().equals(discussion.getAuthor())) {
            discussionEntityService.updatePostLikeMsg(discussion.getUid(), userRolesVo.getUid(), did, discussion.getGid());
        }
    } else {
        // 取消点赞
        if (discussionLike != null) {
            // 如果存在就删除
            boolean isDelete = discussionLikeEntityService.removeById(discussionLike.getId());
            if (!isDelete) {
                throw new StatusFailException("取消点赞失败,请重试尝试!");
            }
        }
        // 点赞-1
        UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
        discussionUpdateWrapper.setSql("like_num=like_num-1").eq("id", did);
        discussionEntityService.update(discussionUpdateWrapper);
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) DiscussionLike(top.hcode.hoj.pojo.entity.discussion.DiscussionLike) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)2 DiscussionLike (top.hcode.hoj.pojo.entity.discussion.DiscussionLike)2 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)2 HttpSession (javax.servlet.http.HttpSession)1 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)1 Session (org.apache.shiro.session.Session)1 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)1 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)1