forked from makefriends/dating
Compare commits
1 Commits
05e71576c6
...
feature-Co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c96af708df |
11
pom.xml
11
pom.xml
@@ -19,6 +19,13 @@
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.5.10</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@@ -123,8 +130,8 @@
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
19
src/main/java/com/bao/dating/common/Comments.java
Normal file
19
src/main/java/com/bao/dating/common/Comments.java
Normal 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; // 评论时间
|
||||
}
|
||||
16
src/main/java/com/bao/dating/common/Post.java
Normal file
16
src/main/java/com/bao/dating/common/Post.java
Normal 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; // 创建时间
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
22
src/main/java/com/bao/dating/mapper/CommentsMapper.java
Normal file
22
src/main/java/com/bao/dating/mapper/CommentsMapper.java
Normal 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);
|
||||
}
|
||||
20
src/main/java/com/bao/dating/service/CommentsService.java
Normal file
20
src/main/java/com/bao/dating/service/CommentsService.java
Normal 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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user