添加注释

This commit is contained in:
KilLze
2025-12-19 01:41:04 +08:00
parent 0f3c9e78a6
commit c6f3cb72dd
6 changed files with 85 additions and 12 deletions

View File

@@ -6,11 +6,29 @@ import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface PostLikeMapper { public interface PostLikeMapper {
//根据 postId 和 userId 查询点赞记录 用于判断是否已经点赞 /**
* 根据 postId 和 userId 查询点赞记录 用于判断是否已经点赞
* @param postId
* @param userId
* @return
*/
PostLike findByPostAndUser(@Param("postId") Long postId, @Param("userId") Long userId); PostLike findByPostAndUser(@Param("postId") Long postId, @Param("userId") Long userId);
/**
* 插入点赞记录
*
* @param postLike
* @return
*/
int insert(PostLike postLike); int insert(PostLike postLike);
// 根据 postId + userId 删除点赞 /**
* 删除点赞记录
* 根据 postId + userId 删除点赞
*
* @param postId
* @param userId
* @return
*/
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId); int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
} }

View File

@@ -5,13 +5,44 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface PostMapper { public interface PostMapper {
/**
* 插入动态
*
* @param post 动态对象
* @return 插入的行数
*/
int insert(Post post); int insert(Post post);
int increaseLikeCount(Long postId); /**
* 根据ID删除动态
*
* @param postId 动态ID
* @return 删除的行数
*/
int deletePostById(Integer postId);
/***
* 查询点赞数
*
* @param postId
* @return
*/
int selectLikeCount(Long postId); int selectLikeCount(Long postId);
int decreaseLikeCount(Long postId);
int deletePostById(Integer postId); /**
* 点赞数+1
*
* @param postId 动态ID
* @return 影响行数
*/
int increaseLikeCount(Long postId);
/**
* 点赞数-1
*
* @param postId 动态ID
* @return 影响行数
*/
int decreaseLikeCount(Long postId);
} }

View File

@@ -7,7 +7,7 @@ import java.util.List;
@Data @Data
public class PostRequestDTO { public class PostRequestDTO {
private Integer userId; private Long userId;
private String content; private String content;
private List<String> tags; private List<String> tags;
private List<String> mediaUrls; private List<String> mediaUrls;

View File

@@ -11,6 +11,12 @@ public interface PostLikeService {
* @return 统一返回结果 * @return 统一返回结果
*/ */
Result<?> likePost(Long postId, Long userId); Result<?> likePost(Long postId, Long userId);
//取消点赞
/**
* 取消点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
*/
void unlike(Long postId, Long userId); void unlike(Long postId, Long userId);
} }

View File

@@ -24,6 +24,13 @@ public class PostLikeServiceImpl implements PostLikeService {
/**
* 点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
* @return 统一返回结果
*/
@Override @Override
@Transactional // 保证点赞 + 点赞数更新是一个事务 @Transactional // 保证点赞 + 点赞数更新是一个事务
public Result<?> likePost(Long postId, Long userId) { public Result<?> likePost(Long postId, Long userId) {
@@ -51,6 +58,12 @@ public class PostLikeServiceImpl implements PostLikeService {
return Result.success(ResultCode.SUCCESS_REVIEW,"点赞成功",data); return Result.success(ResultCode.SUCCESS_REVIEW,"点赞成功",data);
} }
/**
* 取消点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
*/
@Override @Override
@Transactional @Transactional
public void unlike(Long postId, Long userId) { public void unlike(Long postId, Long userId) {

View File

@@ -3,7 +3,8 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bao.dating.mapper.PostMapper"> <mapper namespace="com.bao.dating.mapper.PostMapper">
<!-- 动态添加 -->
<!--动态添加-->
<insert id="insert" useGeneratedKeys="true" keyProperty="postId"> <insert id="insert" useGeneratedKeys="true" keyProperty="postId">
INSERT INTO post INSERT INTO post
(user_id, content, (user_id, content,
@@ -26,10 +27,18 @@
</if> </if>
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt}) #{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
</insert> </insert>
<!--动态删除-->
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE post_id = #{postId}
</delete>
<!--增加点赞数量--> <!--增加点赞数量-->
<update id="increaseLikeCount"> <update id="increaseLikeCount">
update dating.post set like_count = like_count + 1 where post.post_id = #{postId} update dating.post set like_count = like_count + 1 where post.post_id = #{postId}
</update> </update>
<!--减少点赞数量-->
<update id="decreaseLikeCount"> <update id="decreaseLikeCount">
update dating.post set like_count= like_count - 1 where post.post_id = #{postId} update dating.post set like_count= like_count - 1 where post.post_id = #{postId}
</update> </update>
@@ -39,8 +48,4 @@
select dating.post.like_count from dating.post where post.post_id = #{postId} select dating.post.like_count from dating.post where post.post_id = #{postId}
</select> </select>
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE post_id = #{postId}
</delete>
</mapper> </mapper>