Commit 808953cc by 段启岩

动态点赞完成

parent 2bb8e4ad
...@@ -27,35 +27,31 @@ public class PostPraiseApi { ...@@ -27,35 +27,31 @@ public class PostPraiseApi {
this.postPraiseService = postPraiseService; this.postPraiseService = postPraiseService;
} }
@ApiOperation("点赞") @ApiOperation("动态点赞")
@PostMapping("/post/{targetId}/praise") @PostMapping("/post/{postId}/praise")
public Response praise (@PathVariable("targetId") String targetId, public Response praise(@PathVariable("postId") String postId) {
@RequestParam("targetType") Integer targetType) {
Subject subject = SubjectUtils.getSubject(); Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId(); String currentUserId = (String) subject.getId();
try { try {
postPraiseService.postPraise(currentUserId,targetId,targetType); postPraiseService.postPraise(currentUserId, postId);
return Response.success(); return Response.success();
} catch (PostServiceException e) { } catch (PostServiceException e) {
e.printStackTrace();
return Response.error(e); return Response.error(e);
} }
} }
@ApiOperation("取消点赞") @ApiOperation("取消点赞")
@DeleteMapping("/post/{targetId}/praise") @DeleteMapping("/post/{postId}/praise")
public Response deletePraise (@PathVariable("targetId") String targetId, public Response deletePraise(@PathVariable("postId") String postId) {
@RequestParam("targetType") Integer targetType) {
Subject subject = SubjectUtils.getSubject(); Subject subject = SubjectUtils.getSubject();
String currentUserId = (String) subject.getId(); String currentUserId = (String) subject.getId();
try { try {
postPraiseService.delPostPraise(currentUserId,targetId,targetType); postPraiseService.delPostPraise(currentUserId, postId);
return Response.success(); return Response.success();
} catch (PostServiceException e) { } catch (PostServiceException e) {
e.printStackTrace();
return Response.error(e); return Response.error(e);
} }
} }
......
package cn.meteor.beyondclouds.modules.post.enums;
import lombok.Getter;
/**
* @author meteor
*/
@Getter
public enum PostPraiseType {
/**
* 动态点赞
*/
POST_PRAISE(1),
/**
* 给动态评论点赞
*/
POST_COMMENT_PRAISE(2)
;
private Integer praiseType;
PostPraiseType(Integer praiseType) {
this.praiseType = praiseType;
}
}
...@@ -17,18 +17,16 @@ public interface IPostPraiseService extends IService<PostPraise> { ...@@ -17,18 +17,16 @@ public interface IPostPraiseService extends IService<PostPraise> {
/** /**
* 动态以及动态评论的点赞 * 动态以及动态评论的点赞
* @param currentUserId * @param currentUserId
* @param targetId * @param postId
* @param targetType
* @throws PostServiceException * @throws PostServiceException
*/ */
void postPraise(String currentUserId, String targetId, Integer targetType) throws PostServiceException; void postPraise(String currentUserId, String postId) throws PostServiceException;
/** /**
* 取消动态以及动态评论的点赞 * 取消动态以及动态评论的点赞
* @param currentUserId * @param currentUserId
* @param targetId * @param postId
* @param targetType
* @throws PostServiceException * @throws PostServiceException
*/ */
void delPostPraise(String currentUserId, String targetId, Integer targetType) throws PostServiceException; void delPostPraise(String currentUserId, String postId) throws PostServiceException;
} }
package cn.meteor.beyondclouds.modules.post.service.impl; package cn.meteor.beyondclouds.modules.post.service.impl;
import cn.meteor.beyondclouds.modules.post.entity.Post;
import cn.meteor.beyondclouds.modules.post.entity.PostPraise; import cn.meteor.beyondclouds.modules.post.entity.PostPraise;
import cn.meteor.beyondclouds.modules.post.enums.PostCommentErrorCode;
import cn.meteor.beyondclouds.modules.post.enums.PostErrorCode; import cn.meteor.beyondclouds.modules.post.enums.PostErrorCode;
import cn.meteor.beyondclouds.modules.post.enums.PostPraiseType;
import cn.meteor.beyondclouds.modules.post.exception.PostServiceException; import cn.meteor.beyondclouds.modules.post.exception.PostServiceException;
import cn.meteor.beyondclouds.modules.post.mapper.PostPraiseMapper; import cn.meteor.beyondclouds.modules.post.mapper.PostPraiseMapper;
import cn.meteor.beyondclouds.modules.post.service.IPostCommentService; import cn.meteor.beyondclouds.modules.post.service.IPostCommentService;
...@@ -13,11 +14,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; ...@@ -13,11 +14,9 @@ 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 java.util.List;
/** /**
* <p> * <p>
* 服务实现类 * 服务实现类
* </p> * </p>
* *
* @author 段启岩 * @author 段启岩
...@@ -40,49 +39,45 @@ public class PostPraiseServiceImpl extends ServiceImpl<PostPraiseMapper, PostPra ...@@ -40,49 +39,45 @@ public class PostPraiseServiceImpl extends ServiceImpl<PostPraiseMapper, PostPra
} }
@Override @Override
public void postPraise(String currentUserId, String targetId, Integer targetType) throws PostServiceException{ public void postPraise(String currentUserId, String postId) throws PostServiceException {
//1. 判断目标类型是否正确 // 1.判断动态是否存在
if(!List.of(1,2).contains(targetType)) { Post post = postService.getById(postId);
throw new PostServiceException(PostErrorCode.POST_PRAISE_TYPE_ERROR); if (null == post) {
throw new PostServiceException(PostErrorCode.POST_NOT_FOUND);
} }
//2. 判断用户是否给目标动态或动态评论点过赞 // 2. 判断用户是否给目标动态点过赞
QueryWrapper<PostPraise> postPraiseQueryWrapper = new QueryWrapper<>(); QueryWrapper<PostPraise> postPraiseQueryWrapper = new QueryWrapper<>();
postPraiseQueryWrapper postPraiseQueryWrapper
.eq("target_id",targetId) .eq("target_id", postId)
.eq("user_id",currentUserId); .eq("user_id", currentUserId)
.eq("target_type", PostPraiseType.POST_PRAISE.getPraiseType());
PostPraise postPraise = getOne(postPraiseQueryWrapper); PostPraise postPraise = getOne(postPraiseQueryWrapper);
if (null != postPraise) { if (null != postPraise) {
throw new PostServiceException(PostErrorCode.POST_PRAISE_EXIST); throw new PostServiceException(PostErrorCode.POST_PRAISE_EXIST);
} }
//3. 判断对应的动态或评论是否存在
if ((targetType == 1) && (null == postService.getById(targetId))) {
throw new PostServiceException(PostErrorCode.POST_NOT_FOUND);
}
if ((targetType == 2) && (null == postCommentService.getById(targetId))) {
throw new PostServiceException(PostCommentErrorCode.COMMENT_NOT_FOUND);
}
//4. 点赞 //4. 点赞
postPraise = new PostPraise(); postPraise = new PostPraise();
postPraise.setUserId(currentUserId); postPraise.setUserId(currentUserId);
postPraise.setTargetId(targetId); postPraise.setTargetId(postId);
postPraise.setTargetType(targetType); postPraise.setTargetType(PostPraiseType.POST_PRAISE.getPraiseType());
save(postPraise); save(postPraise);
} }
@Override @Override
public void delPostPraise(String currentUserId, String targetId, Integer targetType) throws PostServiceException{ public void delPostPraise(String currentUserId, String postId) throws PostServiceException {
//1. 判断用户是否给目标动态或动态评论点过赞 //1. 判断用户是否给目标动态或动态评论点过赞
QueryWrapper<PostPraise> postPraiseQueryWrapper = new QueryWrapper<>(); QueryWrapper<PostPraise> postPraiseQueryWrapper = new QueryWrapper<>();
postPraiseQueryWrapper postPraiseQueryWrapper
.eq("target_id",targetId) .eq("target_id", postId)
.eq("user_id",currentUserId); .eq("user_id", currentUserId)
.eq("target_type", PostPraiseType.POST_PRAISE.getPraiseType());
PostPraise postPraise = getOne(postPraiseQueryWrapper); PostPraise postPraise = getOne(postPraiseQueryWrapper);
if (null == postPraise) { if (null == postPraise) {
......
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