Merge remote-tracking branch 'upstream/master'

This commit is contained in:
KilLze
2025-12-19 01:05:50 +08:00
13 changed files with 174 additions and 22 deletions

View File

@@ -1,9 +1,10 @@
package com.bao.dating; package com.bao.dating;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.bao.dating.mapper")
@SpringBootApplication @SpringBootApplication
public class DatingApplication { public class DatingApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -57,4 +57,5 @@ public class Result<T> {
public static <T> Result<T> error(ResultCode code, String msg) { public static <T> Result<T> error(ResultCode code, String msg) {
return new Result<>(code.code(), msg, null); return new Result<>(code.code(), msg, null);
} }
} }

View File

@@ -0,0 +1,36 @@
package com.bao.dating.controller;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.mapper.PostLikeMapper;
import com.bao.dating.service.PostLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Objects;
@RestController
@RequestMapping("/posts")
public class PostLikeController {
@Autowired
private PostLikeService postLikeService;
@PostMapping("/{postId}/likes")
public Result<?> likePost(@PathVariable Long postId, @RequestBody Map<String, Long> body){
// 从请求体中取出 user_id
Long userId = body.get("user_id");
if (userId ==null){
return Result.error(ResultCode.PARAM_ERROR);
}
return postLikeService.likePost(postId,userId);
}
@DeleteMapping("/{postId}/likes")
public void unlike(@PathVariable Long postId, @RequestBody Map<String, Long> body){
// 从请求体中获取 user_id
Long userId = body.get("user_id");
postLikeService.unlike(postId,userId);
}
}

View File

@@ -0,0 +1,16 @@
package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.PostLike;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PostLikeMapper {
//根据 postId 和 userId 查询点赞记录 用于判断是否已经点赞
PostLike findByPostAndUser(@Param("postId") Long postId, @Param("userId") Long userId);
int insert(PostLike postLike);
// 根据 postId + userId 删除点赞
int deleteByPostIdAndUserId(@Param("postId") Long postId, @Param("userId") Long userId);
}

View File

@@ -5,16 +5,5 @@ 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);
/**
* 根据动态ID删除动态
* @param postId
*/
void deletePostById(Integer postId);
} }

View File

@@ -12,9 +12,9 @@ import java.util.List;
@Data @Data
public class Post { public class Post {
private Integer postId; private Long postId;
private Integer userId; private Long userId;
private String content; private String content;
@@ -33,4 +33,4 @@ public class Post {
private LocalDateTime createdAt; private LocalDateTime createdAt;
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
} }

View File

@@ -11,11 +11,11 @@ import java.time.LocalDateTime;
@Data @Data
public class PostFavorite { public class PostFavorite {
private Integer favoriteId; private Long favoriteId;
private Integer userId; private Long userId;
private Integer postId; private Long postId;
private LocalDateTime createdAt; private LocalDateTime createdAt;
} }

View File

@@ -10,11 +10,11 @@ import java.time.LocalDateTime;
*/ */
@Data @Data
public class PostLike { public class PostLike {
private Integer likeId; private Long likeId;
private Integer userId; private Long userId;
private Integer postId; private Long postId;
private LocalDateTime createdAt; private LocalDateTime createdAt;
} }

View File

@@ -12,7 +12,7 @@ import java.time.LocalDateTime;
@Data @Data
public class User { public class User {
private Integer userId; private Long userId;
private String userName; private String userName;

View File

@@ -0,0 +1,16 @@
package com.bao.dating.service;
import com.bao.dating.common.Result;
public interface PostLikeService {
/**
* 点赞指定的 post
*
* @param postId 帖子ID
* @param userId 用户ID
* @return 统一返回结果
*/
Result<?> likePost(Long postId, Long userId);
//取消点赞
void unlike(Long postId, Long userId);
}

View File

@@ -0,0 +1,64 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.mapper.PostLikeMapper;
import com.bao.dating.mapper.PostMapper;
import com.bao.dating.pojo.entity.PostLike;
import com.bao.dating.service.PostLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
@Service
public class PostLikeServiceImpl implements PostLikeService {
@Autowired
private PostMapper postMapper;
@Autowired
private PostLikeMapper postLikeMapper;
@Override
@Transactional // 保证点赞 + 点赞数更新是一个事务
public Result<?> likePost(Long postId, Long userId) {
// 查询是否已经点过赞(防止重复点赞)
PostLike exist = postLikeMapper.findByPostAndUser(postId, userId);
if (exist != null){
return Result.error(ResultCode.SUCCESS_REVIEW,"您已经点过赞了。");
}
// 创建点赞对象
PostLike postLike = new PostLike();
postLike.setPostId(postId);
postLike.setUserId(userId);
//插入点赞记录
postLikeMapper.insert(postLike);
//点赞数+1
postMapper.increaseLikeCount(postId);
//查询最新的点赞数
Integer i = postMapper.selectLikeCount(postId);
//封装返回数据
Map<String,Object> data = new HashMap<>();
data.put("like_id",postLike.getLikeId());
data.put("post_id",postLike.getPostId());
data.put("user_id",postLike.getUserId());
data.put("like_count",i);
return Result.success(ResultCode.SUCCESS_REVIEW,"点赞成功",data);
}
@Override
@Transactional
public void unlike(Long postId, Long userId) {
// 删除点赞记录
int i = postLikeMapper.deleteByPostIdAndUserId(postId, userId);
// 如果确实删除成功,才减少点赞数
if (i > 0){
postMapper.decreaseLikeCount(postId);
}
}
}

View File

@@ -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.PostLikeMapper">
<select id="findByPostAndUser" resultType="com.bao.dating.pojo.entity.PostLike">
select * from dating.post_like where user_id = #{userId} and post_id = #{postId}
</select>
<insert id="insert" useGeneratedKeys="true" keyProperty="likeId">
INSERT INTO dating.post_like (post_id, user_id,created_at) VALUES (#{postId}, #{userId},NOW())
</insert>
<delete id="deleteByPostIdAndUserId">
delete from dating.post_like where post_id = #{postId} and user_id = #{userId}
</delete>
</mapper>

View File

@@ -26,6 +26,18 @@
</if> </if>
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt}) #{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
</insert> </insert>
<!--增加点赞数量-->
<update id="increaseLikeCount">
update dating.post set like_count = like_count + 1 where post.post_id = #{postId}
</update>
<update id="decreaseLikeCount">
update dating.post set like_count= like_count - 1 where post.post_id = #{postId}
</update>
<!--查询点赞当前数量-->
<select id="selectLikeCount" resultType="java.lang.Integer">
select dating.post.like_count from dating.post where post.post_id = #{postId}
</select>
<delete id="deletePostById" parameterType="int"> <delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE post_id = #{postId} DELETE FROM post WHERE post_id = #{postId}