1 Commits

Author SHA1 Message Date
lihaiyang
c96af708df 评论功能 2025-12-23 13:48:40 +08:00
18 changed files with 174 additions and 209 deletions

11
pom.xml
View File

@@ -19,6 +19,13 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@@ -123,8 +130,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>1.8</source> <source>9</source>
<target>1.8</target> <target>9</target>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
</configuration> </configuration>
</plugin> </plugin>

View File

@@ -0,0 +1,19 @@
package com.bao.dating.common;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 评论表
*/
@Data
public class Comments {
private Long comment_id; // 评论ID
private String content; // 评论内容
private Long user_id; // 评论人ID
private Long post_id; // 关联动态ID
private LocalDateTime created_at; // 评论时间
}

View File

@@ -0,0 +1,16 @@
package com.bao.dating.common;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 动态表
*/
@Data
public class Post {
private Long post_id; // 动态ID
private String content; // 动态内容
private Long user_id; // 发布人ID
private LocalDateTime created_at; // 创建时间
}

View File

@@ -0,0 +1,48 @@
package com.bao.dating.controller;
import com.bao.dating.common.Comments;
import com.bao.dating.service.CommentsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/comments")
@CrossOrigin // 解决跨域问题(前后端分离必加)
public class CommentController {
@Resource
private CommentsService commentsService;
/**
* 添加评论
* @param comment 评论对象含content、userId、dynamicId
*/
@PostMapping("/add")
public Map<String, Object> addComment(@RequestBody Comments comment) {
boolean success = commentsService.addComment(comment);
return success ? Map.of("code", 200, "msg", "评论成功") : Map.of("code", 500, "msg", "评论失败");
}
/**
* 删除评论
* @param user_id 评论ID
*/
@DeleteMapping("/delete/{user_id}")
public Map<String, Object> deleteComment(@PathVariable Long user_id) {
boolean success = commentsService.deleteComment(user_id);
return success ? Map.of("code", 200, "msg", "删除成功") : Map.of("code", 500, "msg", "删除失败");
}
/**
* 根据动态ID查询评论列表
* @param post_id 动态ID
*/
@GetMapping("/list/{post_id}")
public Map<String, Object> getCommentList(@PathVariable Long post_id) {
List<Comments> commentList = commentsService.getCommentByPostId(post_id);
return Map.of("code", 200, "data", commentList);
}
}

View File

@@ -20,7 +20,7 @@ public class PostController {
private PostService postService; private PostService postService;
/** /**
* 上传媒体文件接口 like * 上传媒体文件接口
* @param files 媒体文件数组 * @param files 媒体文件数组
* @return 上传后的文件URL列表 * @return 上传后的文件URL列表
*/ */

View File

@@ -1,34 +0,0 @@
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);
}
}

View File

@@ -1,4 +0,0 @@
package com.bao.dating.controller;
public class text {
}

View File

@@ -0,0 +1,22 @@
package com.bao.dating.mapper;
import com.bao.dating.common.Comments;
import com.bao.dating.common.Post;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CommentsMapper {
// 添加评论
@Insert("INSERT INTO comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
int addComment(Comments comments);
// 删除评论
@Delete("DELETE FROM comments WHERE user_id = #{user_id}")
int deleteComments(@Param("user_id") Long user_id);
// 根据动态ID查询评论列表
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
}

View File

@@ -1,15 +0,0 @@
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);
}

View File

@@ -2,7 +2,6 @@ package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.Post; import com.bao.dating.pojo.entity.Post;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper @Mapper
public interface PostMapper { public interface PostMapper {
@@ -45,34 +44,4 @@ public interface PostMapper {
* @return 影响行数 * @return 影响行数
*/ */
int decreaseLikeCount(Long postId); int decreaseLikeCount(Long postId);
/**
* 查询当前动态属于哪个用户id
* @param postId 动态id
* @return 用户id
*/
Long selectUserIdByPostId(@Param("post_id") Long postId);
/**
* 查询点赞数
*
* @param postId
* @return
*/
int selectFavoriteCount(Long postId);
/**
* 收藏数+1
*
* @param postId 动态ID
* @return 影响行数
*/
int increaseFavoriteCount(Long postId);
/**
* 收藏数-1
*
* @param postId 动态ID
* @return 影响行数
*/
int decreaseFavoriteCount(Long postId);
} }

View File

@@ -0,0 +1,20 @@
package com.bao.dating.service;
import com.bao.dating.common.Comments;
import java.util.List;
public interface CommentsService{
// 添加评论
boolean addComment(Comments comments);
// 删除评论
boolean deleteComment(Long user_id);
// 根据动态ID查询评论
List<Comments> getCommentByPostId(Long post_id);
}

View File

@@ -1,10 +0,0 @@
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);
}

View File

@@ -27,11 +27,4 @@ public interface PostService {
* @return 删除的动态对象 * @return 删除的动态对象
*/ */
void deletePostById(Integer postId); void deletePostById(Integer postId);
/**
* 查询
* @param postId 动态id
* @return 用户id
*/
Long selectUserIdByPostId(Long postId);
} }

View File

@@ -0,0 +1,39 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.Comments;
import com.bao.dating.mapper.CommentsMapper;
import com.bao.dating.service.CommentsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class CommentsServiceImpl implements CommentsService {
@Resource
private CommentsMapper commentsMapper;
@Override
public boolean addComment(Comments comments) {
// 设置创建时间
comments.setCreated_at(LocalDateTime.now());
return commentsMapper.addComment(comments) > 0;
}
@Override
public boolean deleteComment(Long user_id) {
return commentsMapper.deleteComments(user_id) > 0;
}
@Override
public List<Comments> getCommentByPostId(Long post_id) {
return commentsMapper.getCommentsByPostId(post_id);
}
}

View File

@@ -1,61 +0,0 @@
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;
}
}

View File

@@ -147,15 +147,4 @@ public class PostServiceImpl implements PostService {
public void deletePostById(Integer postId) { public void deletePostById(Integer postId) {
postMapper.deletePostById(postId); postMapper.deletePostById(postId);
} }
/**
* 查询用户id
* @param postId 动态id
* @return
*/
@Override
public Long selectUserIdByPostId(Long postId) {
return postMapper.selectUserIdByPostId(postId);
}
} }

View File

@@ -1,17 +0,0 @@
<?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>

View File

@@ -42,26 +42,10 @@
<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>
<!--增加收藏数量-->
<update id="increaseFavoriteCount">
update post set favorite_count = favorite_count + 1 where post_id = #{post_id}
</update>
<!--减少收藏数量-->
<update id="decreaseFavoriteCount">
update post set favorite_count = favorite_count - 1 where post_id = #{post_id}
</update>
<!--查询点赞当前数量--> <!--查询点赞当前数量-->
<select id="selectLikeCount" resultType="java.lang.Integer"> <select id="selectLikeCount" resultType="java.lang.Integer">
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>
<!--查询当前动态的用户id-->
<select id="selectUserIdByPostId" resultType="java.lang.Long">
SELECT user_id FROM post WHERE post_id = #{post_id}
</select>
<!--查询当前收藏数量-->
<select id="selectFavoriteCount" resultType="java.lang.Integer">
select dating.post.favorite_count from dating.post where post.post_id = #{postId}
</select>
</mapper> </mapper>