Commit 808953cc by 段启岩

动态点赞完成

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