Commit 7300d045 by 段启岩

博客点赞完成

parent f79e7403
package cn.meteor.beyondclouds.modules.blog.api; package cn.meteor.beyondclouds.modules.blog.api;
import cn.meteor.beyondclouds.common.dto.PageDTO;
import cn.meteor.beyondclouds.common.form.PageForm;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
import cn.meteor.beyondclouds.core.annotation.CurrentSubject;
import cn.meteor.beyondclouds.core.annotation.PreventDuplicate;
import cn.meteor.beyondclouds.core.annotation.ReplaceWithRemarks;
import cn.meteor.beyondclouds.core.api.Response; import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.authentication.Subject; import cn.meteor.beyondclouds.core.authentication.Subject;
import cn.meteor.beyondclouds.core.exception.ServiceException; import cn.meteor.beyondclouds.core.exception.ServiceException;
import cn.meteor.beyondclouds.core.flow.AccessInfo;
import cn.meteor.beyondclouds.core.flow.CollectAccessInfo;
import cn.meteor.beyondclouds.core.flow.ParamType;
import cn.meteor.beyondclouds.core.validation.groups.InsertGroup;
import cn.meteor.beyondclouds.core.validation.groups.UpdateGroup;
import cn.meteor.beyondclouds.modules.blog.dto.BlogDetailDTO;
import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.exception.BlogCategoryServiceException;
import cn.meteor.beyondclouds.modules.blog.exception.BlogCommentServiceException;
import cn.meteor.beyondclouds.modules.blog.exception.BlogPraiseServiceException; import cn.meteor.beyondclouds.modules.blog.exception.BlogPraiseServiceException;
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import cn.meteor.beyondclouds.modules.blog.form.BlogForm;
import cn.meteor.beyondclouds.modules.blog.service.IBlogPraiseService; import cn.meteor.beyondclouds.modules.blog.service.IBlogPraiseService;
import cn.meteor.beyondclouds.modules.blog.service.IBlogService;
import cn.meteor.beyondclouds.util.AccessInfoUtils;
import cn.meteor.beyondclouds.util.SubjectUtils; import cn.meteor.beyondclouds.util.SubjectUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/** /**
* @author gaoTong * @author gaoTong
* @date 2020/1/31 9:27 * @date 2020/1/31 9:27
...@@ -54,33 +28,54 @@ public class BlogPraiseApi { ...@@ -54,33 +28,54 @@ public class BlogPraiseApi {
this.blogPraiseService = blogPraiseService; this.blogPraiseService = blogPraiseService;
} }
@ApiOperation("点赞") @ApiOperation("博客点赞")
@PostMapping("/blog/{targetId}/praise") @PostMapping("/blog/{blogId}/praise")
public Response praise (@PathVariable("targetId") String targetId, public Response bogPraise (@PathVariable("blogId") String blogId) {
@RequestParam("targetType") Integer targetType) { Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId();
try {
blogPraiseService.praiseBlog(currentUserId, blogId);
return Response.success();
} catch (BlogPraiseServiceException e) {
return Response.error(e);
}
}
@ApiOperation("取消博客点赞")
@DeleteMapping("/blog/{blogId}/praise")
public Response deleteBlogPraise (@PathVariable("blogId") String blogId) {
Subject subject = SubjectUtils.getSubject(); Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId(); String currentUserId = (String) subject.getId();
try { try {
blogPraiseService.praise(currentUserId, targetId, targetType); blogPraiseService.deleteBlogPraise(currentUserId, blogId);
return Response.success(); return Response.success();
} catch (ServiceException e) { } catch (BlogPraiseServiceException e) {
e.printStackTrace();
return Response.error(e); return Response.error(e);
} }
} }
@ApiOperation("取消点赞") @ApiOperation("博客评论点赞")
@DeleteMapping("/blog/{targetId}/praise") @PostMapping("/blog/comment{commentId}/praise")
public Response deletePraise (@PathVariable("targetId") String targetId) { public Response blogCommentPraise (@PathVariable("commentId") String commentId) {
Subject subject = SubjectUtils.getSubject(); Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId(); String currentUserId = (String) subject.getId();
try {
blogPraiseService.praiseBlogComment(currentUserId, commentId);
return Response.success();
} catch (BlogPraiseServiceException e) {
return Response.error(e);
}
}
// TODO 实现取消点赞功能,包括博客,评论的取消点赞 @ApiOperation("取消博客评论点赞")
@DeleteMapping("/blog/comment{commentId}/praise")
public Response deleteBlogCommentPraise (@PathVariable("commentId") String commentId) {
Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId();
try { try {
blogPraiseService.deletePraise(currentUserId, targetId); blogPraiseService.deleteBlogCommentPraise(currentUserId, commentId);
return Response.success(); return Response.success();
} catch (ServiceException e) { } catch (BlogPraiseServiceException e) {
e.printStackTrace();
return Response.error(e); return Response.error(e);
} }
} }
......
...@@ -8,7 +8,7 @@ import cn.meteor.beyondclouds.core.IErrorCode; ...@@ -8,7 +8,7 @@ import cn.meteor.beyondclouds.core.IErrorCode;
*/ */
public enum BlogPraiseErrorCode implements IErrorCode { public enum BlogPraiseErrorCode implements IErrorCode {
BLOG_PRAISE_EXIST(8008,"您已经点过赞了"), BLOG_PRAISE_EXIST(8008,"您已经点过赞了"),
NO_DELETE_PRIVILEGES(8009,"没有权限取消赞") NO_PRAISE_FOUND(8009,"您没有点过赞")
; ;
private long code; private long code;
......
package cn.meteor.beyondclouds.modules.blog.enums;
import lombok.Getter;
/**
* @author meteor
*/
@Getter
public enum BlogPraiseType {
/**
* 博客点赞
*/
BLOG_PRAISE(1),
/**
* 博客评论点赞
*/
BLOG_COMMENT_PRAISE(2),
;
private Integer praiseType;
BlogPraiseType(Integer praiseType) {
this.praiseType = praiseType;
}
}
...@@ -17,17 +17,30 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -17,17 +17,30 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface IBlogPraiseService extends IService<BlogPraise> { public interface IBlogPraiseService extends IService<BlogPraise> {
/** /**
* 点赞 * 博客点赞
* @param userId * @param userId
* @param targetId * @param targetId
* @param targetType
*/ */
void praise(String userId , String targetId , Integer targetType) throws BlogServiceException, BlogPraiseServiceException, BlogCommentServiceException; void praiseBlog(String userId, String targetId) throws BlogPraiseServiceException;
/** /**
* 取消点赞 * 取消博客点赞
* @param userId * @param userId
* @param targetId * @param blogId
*/
void deleteBlogPraise(String userId , String blogId) throws BlogPraiseServiceException;
/**
* 博客评论点赞
* @param currentUserId
* @param commentId
*/
void praiseBlogComment(String currentUserId, String commentId) throws BlogPraiseServiceException;
/**
* 删除博客评论点赞
* @param currentUserId
* @param commentId
*/ */
void deletePraise(String userId , String targetId) throws BlogPraiseServiceException; void deleteBlogCommentPraise(String currentUserId, String commentId) throws BlogPraiseServiceException;
} }
...@@ -6,6 +6,7 @@ import cn.meteor.beyondclouds.modules.blog.entity.BlogPraise; ...@@ -6,6 +6,7 @@ import cn.meteor.beyondclouds.modules.blog.entity.BlogPraise;
import cn.meteor.beyondclouds.modules.blog.enums.BlogCommentErrorCode; import cn.meteor.beyondclouds.modules.blog.enums.BlogCommentErrorCode;
import cn.meteor.beyondclouds.modules.blog.enums.BlogErrorCode; import cn.meteor.beyondclouds.modules.blog.enums.BlogErrorCode;
import cn.meteor.beyondclouds.modules.blog.enums.BlogPraiseErrorCode; import cn.meteor.beyondclouds.modules.blog.enums.BlogPraiseErrorCode;
import cn.meteor.beyondclouds.modules.blog.enums.BlogPraiseType;
import cn.meteor.beyondclouds.modules.blog.exception.BlogCommentServiceException; import cn.meteor.beyondclouds.modules.blog.exception.BlogCommentServiceException;
import cn.meteor.beyondclouds.modules.blog.exception.BlogPraiseServiceException; import cn.meteor.beyondclouds.modules.blog.exception.BlogPraiseServiceException;
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException; import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
...@@ -17,7 +18,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; ...@@ -17,7 +18,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/** /**
* <p> * <p>
...@@ -45,66 +45,91 @@ public class BlogPraiseServiceImpl extends ServiceImpl<BlogPraiseMapper, BlogPra ...@@ -45,66 +45,91 @@ public class BlogPraiseServiceImpl extends ServiceImpl<BlogPraiseMapper, BlogPra
} }
@Override @Override
public void praise(String userId, String targetId, Integer targetType) throws BlogServiceException, BlogPraiseServiceException, BlogCommentServiceException { public void praiseBlog(String userId, String blogId) throws BlogPraiseServiceException {
//判断是博客内容点赞还是博客评论 // 1.判断博客是否存在
if (1 == targetType) { Blog blog = blogService.getById(blogId);
//判断当前博客是否存在 if (null == blog) {
Blog blog = blogService.getById(targetId); throw new BlogPraiseServiceException(BlogErrorCode.BLOG_NOT_FOUND);
if (null != blog) {
//判断该用户是否已经点过赞
QueryWrapper<BlogPraise> blogExtqueryWrapper = new QueryWrapper<>();
blogExtqueryWrapper.eq("user_id", userId);
blogExtqueryWrapper.eq("target_id", targetId);
BlogPraise blogExtPraise = getOne(blogExtqueryWrapper);
//如果没有点过赞则进行点赞
if (null == blogExtPraise) {
BlogPraise blogPraise = new BlogPraise();
blogPraise.setUserId(userId);
blogPraise.setTargetType(targetType);
blogPraise.setTargetId(targetId);
save(blogPraise);
} else {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.BLOG_PRAISE_EXIST);
} }
} else {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND); // 2.判断该用户是否已经点过赞
QueryWrapper<BlogPraise> blogPraiseQueryWrapper = new QueryWrapper<>();
blogPraiseQueryWrapper.eq("user_id", userId);
blogPraiseQueryWrapper.eq("target_id", blogId);
blogPraiseQueryWrapper.eq("target_type", BlogPraiseType.BLOG_PRAISE.getPraiseType());
BlogPraise blogPraise = getOne(blogPraiseQueryWrapper);
if (null != blogPraise) {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.BLOG_PRAISE_EXIST);
} }
} else if (2==targetType){
//判断当前评论是否存在 // 3.保存点赞
BlogComment blogComment = blogCommentService.getById(targetId); blogPraise = new BlogPraise();
if (null != blogComment) { blogPraise.setTargetId(blogId);
//判断该用户是否已经对此评论点过赞了 blogPraise.setTargetType(BlogPraiseType.BLOG_PRAISE.getPraiseType());
QueryWrapper<BlogPraise> blogCommentQueryWrapper = new QueryWrapper<>();
blogCommentQueryWrapper.eq("user_id", userId);
blogCommentQueryWrapper.eq("target_id", targetId);
BlogPraise blogCommentPraise = getOne(blogCommentQueryWrapper);
if (null == blogCommentPraise) {
BlogPraise blogPraise = new BlogPraise();
blogPraise.setUserId(userId); blogPraise.setUserId(userId);
blogPraise.setTargetType(targetType);
blogPraise.setTargetId(targetId);
save(blogPraise); save(blogPraise);
} else {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.BLOG_PRAISE_EXIST);
}
} else {
throw new BlogCommentServiceException(BlogCommentErrorCode.COMMENT_NOT_FOUND);
}
}
} }
@Override @Override
public void deletePraise(String userId, String targetId) throws BlogPraiseServiceException { public void deleteBlogPraise(String userId, String blogId) throws BlogPraiseServiceException {
//判断是否是该用户点的 // 1.判断是否点过
QueryWrapper<BlogPraise> queryWrapper = new QueryWrapper<>(); QueryWrapper<BlogPraise> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId); queryWrapper.eq("user_id", userId);
queryWrapper.eq("target_id", targetId); queryWrapper.eq("target_id", blogId);
queryWrapper.eq("target_type", BlogPraiseType.BLOG_PRAISE.getPraiseType());
BlogPraise blogPraise = getOne(queryWrapper); BlogPraise blogPraise = getOne(queryWrapper);
if (null == blogPraise) { if (null == blogPraise) {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.NO_DELETE_PRIVILEGES); throw new BlogPraiseServiceException(BlogPraiseErrorCode.NO_PRAISE_FOUND);
} }
// 2.取消点赞
remove(queryWrapper); remove(queryWrapper);
}
@Override
public void praiseBlogComment(String currentUserId, String commentId) throws BlogPraiseServiceException {
// 1.判断评论是否存在
BlogComment blogComment = blogCommentService.getById(commentId);
if (null == blogComment) {
throw new BlogPraiseServiceException(BlogCommentErrorCode.COMMENT_NOT_FOUND);
}
// 2.判断该用户是否已经点过赞
QueryWrapper<BlogPraise> blogPraiseQueryWrapper = new QueryWrapper<>();
blogPraiseQueryWrapper.eq("user_id", currentUserId);
blogPraiseQueryWrapper.eq("target_id", commentId);
blogPraiseQueryWrapper.eq("target_type", BlogPraiseType.BLOG_COMMENT_PRAISE.getPraiseType());
BlogPraise blogPraise = getOne(blogPraiseQueryWrapper);
if (null != blogPraise) {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.BLOG_PRAISE_EXIST);
}
// 3.保存点赞
blogPraise = new BlogPraise();
blogPraise.setTargetId(commentId);
blogPraise.setTargetType(BlogPraiseType.BLOG_COMMENT_PRAISE.getPraiseType());
blogPraise.setUserId(currentUserId);
save(blogPraise);
}
@Override
public void deleteBlogCommentPraise(String currentUserId, String commentId) throws BlogPraiseServiceException {
// 1.判断是否点过赞
QueryWrapper<BlogPraise> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", currentUserId);
queryWrapper.eq("target_id", commentId);
queryWrapper.eq("target_type", BlogPraiseType.BLOG_COMMENT_PRAISE.getPraiseType());
BlogPraise blogPraise = getOne(queryWrapper);
if (null == blogPraise) {
throw new BlogPraiseServiceException(BlogPraiseErrorCode.NO_PRAISE_FOUND);
}
// 2.取消点赞
remove(queryWrapper);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment