提交 7c998b77 编写于 作者: LKJKJOIUIU's avatar LKJKJOIUIU

收藏取消收藏接口

上级 9ab16508
......@@ -61,4 +61,8 @@ public class PostController {
return postService.getLifePostByTag(tag);
}
@GetMapping("/post/CollectionCount/{userId}/{postId}")
public CommonResult updateCollectionCountById(@PathVariable long userId,@PathVariable long postId) {
return postService.updateCollectionCountById(userId,postId);
}
}
......@@ -5,6 +5,7 @@ import com.nav.vo.result.LifePostResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/**
......@@ -42,7 +43,7 @@ public interface PostDao {
* @param postId:文章id
* @return:int :点赞数
**/
public int updateLikeCountById(@Param("userId") long userId, @Param("postId") long postId);
public int updateLikeCountById(@Param("userId") long userId, @Param("postId") long postId,@Param("createTime") Date createTime,@Param("modifiedTime")Date modifiedTime);
/**
* 方法说明:根据文章id更新浏览量
......@@ -90,4 +91,35 @@ public interface PostDao {
* @return:int :点赞数
**/
public int updateDownLikeCountById(@Param("userId") long userId, @Param("postId") long postId);
/**
* 方法说明:根据用户id和文章id查找该用户是否收藏过
* @author zyx
* @date 2022/5/1 11:32
* @param userId:用户id
* @param postId:文章id
* @return:int :1已收藏,0未收藏
**/
public int selectCollectionById(@Param("userId") long userId, @Param("postId") long postId);
/**
* 方法说明:根据用户id和文章id增加收藏数
* @author zyx
* @date 2022/5/1 11:35
* @param userId:用户id
* @param postId:文章id
* @return:int :收藏数
**/
public int updateCollectionCountById(@Param("userId") long userId, @Param("postId") long postId,@Param("createTime") Date createTime,@Param("modifiedTime")Date modifiedTime);
/**
* 方法说明:根据用户id和文章id减少收藏数
* @author zyx
* @date 2022/5/1 11:37
* @param userId:用户id
* @param postId:文章id
* @return:int :收藏数
**/
public int updateDownCollectionCountById(@Param("userId") long userId, @Param("postId") long postId);
}
......@@ -109,4 +109,14 @@ public interface PostService {
* @return:com.nav.common.api.CommonResult
**/
public CommonResult updateLikeCount( long userId,long postId);
/**
* 方法说明:先判断用户是否收藏过,如果已收藏,则取消收藏,否则收藏数加一
* @author zyx
* @date 2022/5/1 11:42
* @param userId:用户id
* @param postId:文章id
* @return:com.nav.common.api.CommonResult
**/
public CommonResult updateCollectionCountById(long userId,long postId);
}
......@@ -9,7 +9,10 @@ import com.nav.vo.result.LifePostResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
* 类说明
......@@ -36,7 +39,7 @@ public class PostServiceImpl implements PostService{
@Override
public CommonResult updateLikeCountById(long userId,long postId) {
return CommonResult.success(postDao.updateLikeCountById(userId,postId),"请求成功");
return CommonResult.success(postDao.updateLikeCountById(userId,postId,new Date(),new Date()),"请求成功");
}
@Override
......@@ -74,7 +77,7 @@ public class PostServiceImpl implements PostService{
@Override
public CommonResult updateLikeCount(long userId, long postId) {
int isLike=getLikeById(userId,postId);
if(isLike==1){
if(isLike>=1){
//取消点赞
return updateDownLikeCountById(userId,postId);
}
......@@ -83,4 +86,17 @@ public class PostServiceImpl implements PostService{
return updateLikeCountById(userId,postId);
}
}
@Override
public CommonResult updateCollectionCountById(long userId, long postId) {
int isCollect=postDao.selectCollectionById(userId,postId);
if(isCollect>=1){
//取消收藏
return CommonResult.success(postDao.updateDownCollectionCountById(userId,postId),"请求成功");
}
else{
//收藏
return CommonResult.success(postDao.updateCollectionCountById(userId,postId,new Date(),new Date()),"请求成功");
}
}
}
......@@ -4,7 +4,7 @@ server:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.95.151.202/nav?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&allowMultiQueries=true
url: jdbc:mysql://47.95.151.202/nav?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: Nav_123456
servlet:
......
......@@ -25,7 +25,7 @@
set like_count = like_count+1
where id = #{postId};
insert into praise_list(user_id,post_id) values(#{userId},#{postId});
insert into praise_list(user_id,post_id,gmt_create,gmt_modified) values(#{userId},#{postId},#{createTime},#{modifiedTime});
select like_count
from post
......@@ -82,4 +82,36 @@
where post_id=#{postId} and user_id=#{userId};
</select>
<select id="selectCollectionById" resultType="int">
select count(*)
from collection_list
where post_id=#{postId} and user_id=#{userId};
</select>
<!--收藏-->
<select id="updateCollectionCountById" resultType="int">
update post
set collection_count = collection_count+1
where id = #{postId};
insert into collection_list(user_id,post_id,gmt_create,gmt_modified) values(#{userId},#{postId},#{createTime},#{modifiedTime});
select collection_count
from post
where post.id = #{postId};
</select>
<!--取消收藏-->
<select id="updateDownCollectionCountById" resultType="int">
update post
set collection_count = collection_count-1
where id = #{postId};
delete from collection_list where post_id=#{postId} and user_id = #{userId};
select collection_count
from post
where post.id = #{postId};
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册