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, "取消成功");
}
}
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);
}
}
Aggregations