完成收藏模块
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
|
import com.bao.dating.service.PostService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/posts")
|
||||||
|
public class PostFavoriteController {
|
||||||
|
@Autowired
|
||||||
|
private PostFavoriteService postFavoriteService;
|
||||||
|
@PostMapping("/{post_id}/favorites")
|
||||||
|
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
|
if (user == null){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
Long userId = user.getUserId();
|
||||||
|
return postFavoriteService.postFavorite(userId,postId);
|
||||||
|
}
|
||||||
|
@DeleteMapping("/{post_id}/favorites")
|
||||||
|
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
|
||||||
|
if (user == null){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
Long userId = user.getUserId();
|
||||||
|
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/main/java/com/bao/dating/mapper/PostFavoriteMapper.java
Normal file
15
src/main/java/com/bao/dating/mapper/PostFavoriteMapper.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface PostFavoriteMapper {
|
||||||
|
//查询当前已收藏所有用户
|
||||||
|
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||||
|
int addPostFavorite(PostFavorite postFavorite);
|
||||||
|
int deletePostFavorite(@Param("postId") Long postId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface PostFavoriteService {
|
||||||
|
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||||
|
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||||
|
import com.bao.dating.mapper.PostMapper;
|
||||||
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostFavoriteServiceImpl implements PostFavoriteService {
|
||||||
|
@Autowired
|
||||||
|
private PostFavoriteMapper postFavoriteMapper;
|
||||||
|
@Autowired
|
||||||
|
private PostMapper postMapper;
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public Result<Map<String, Long>> postFavorite(Long userid, Long postId) {
|
||||||
|
Long userId = postMapper.selectUserIdByPostId(postId);
|
||||||
|
if (userid.equals(userId)){
|
||||||
|
return Result.error(ResultCode.FORBIDDEN,"无法收藏自己发布动态");
|
||||||
|
}
|
||||||
|
List<Long> allUserId = postFavoriteMapper.selectUserIDByPostID(postId);
|
||||||
|
if (allUserId.contains(userid)){
|
||||||
|
return Result.error(ResultCode.FORBIDDEN,"已收藏");
|
||||||
|
}
|
||||||
|
PostFavorite postFavorite = new PostFavorite();
|
||||||
|
postFavorite.setPostId(postId);
|
||||||
|
postFavorite.setUserId(userid);
|
||||||
|
postFavorite.setCreatedAt(LocalDateTime.now());
|
||||||
|
postFavoriteMapper.addPostFavorite(postFavorite);
|
||||||
|
postMapper.increaseFavoriteCount(postId);
|
||||||
|
Long count = (long) postMapper.selectFavoriteCount(postId);
|
||||||
|
Map<String, Long> data = new HashMap<>();
|
||||||
|
data.put("favorite_id",postFavorite.getFavoriteId());
|
||||||
|
data.put("post_id",postId);
|
||||||
|
data.put("user_id",userid);
|
||||||
|
data.put("current_favorites_count",count);
|
||||||
|
return Result.success(ResultCode.SUCCESS_REVIEW,"收藏成功",data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public Result<?> deletePostFavorite(Long userid, Long postId) {
|
||||||
|
List<Long> allUserId = postFavoriteMapper.selectUserIDByPostID(postId);
|
||||||
|
if (! allUserId.contains(userid)){
|
||||||
|
return Result.error(ResultCode.FORBIDDEN,"请先收藏");
|
||||||
|
}
|
||||||
|
postFavoriteMapper.deletePostFavorite(postId);
|
||||||
|
postMapper.decreaseFavoriteCount(postId);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.bao.dating.mapper.PostFavoriteMapper">
|
||||||
|
<!--添加动态收藏-->
|
||||||
|
<insert id="addPostFavorite">
|
||||||
|
insert into post_favorite values (null,#{userId},#{postId},#{createdAt})
|
||||||
|
</insert>
|
||||||
|
<!--删除动态收藏-->
|
||||||
|
<delete id="deletePostFavorite">
|
||||||
|
delete from post_favorite where post_id = #{postId}
|
||||||
|
</delete>
|
||||||
|
<!--查询当前已收藏所有用户-->
|
||||||
|
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||||
|
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user