From ae032c0d53dbe491899d104b8ef9fe6a6d2a16ba Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Fri, 22 Dec 2023 10:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BA=EF=BC=8C=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=EF=BC=8C=E5=88=A0=E8=AF=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lovenav/controller/CommentController.java | 45 +++++++++++--- src/main/java/com/lovenav/dao/CommentDao.java | 4 ++ .../com/lovenav/service/CommentService.java | 8 ++- .../serviceImpl/CommentServiceImpl.java | 62 +++++++++++++++++-- src/main/resources/mybatis/CommentDao.xml | 10 ++- 5 files changed, 114 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/lovenav/controller/CommentController.java b/src/main/java/com/lovenav/controller/CommentController.java index c034345..80dd1dd 100644 --- a/src/main/java/com/lovenav/controller/CommentController.java +++ b/src/main/java/com/lovenav/controller/CommentController.java @@ -1,26 +1,53 @@ package com.lovenav.controller; +import com.alibaba.fastjson2.JSONObject; import com.lovenav.entity.Comment; import com.lovenav.service.CommentService; import com.lovenav.service.serviceImpl.CommentServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.util.HashMap; @RestController @RequestMapping("/comment") public class CommentController { @Autowired private CommentService commentService; - -// @GetMapping(value= "/AddLikeCount") -// public String AddLikeCount_Controller(HttpSession session){ -// int id = (int) session.getAttribute("id"); -// return commentService.AddLikeCount(id); -// } - @GetMapping(value= "/AddLikeCount") - public String AddLikeCount(int id){ - return commentService.AddLikeCount(id); +// 评论功能 +@RequestMapping(method = RequestMethod.POST, value = "/reply_comment") + public String Reply1(@RequestBody Comment comment){ + return commentService.Reply1(comment); } + +// 回复功能 +@RequestMapping(method = RequestMethod.POST, value = "/comment") + public String Reply2(@RequestBody Comment comment){ + return commentService.Reply2(comment); +} +// 删除功能 +@RequestMapping(method = RequestMethod.POST, value = "/delete_comment") + public String Delete(Comment comment){ + return commentService.Delete(comment); +} +@RequestMapping(method = RequestMethod.GET, value = "/addLikeCount") + public String addLikeCount(int id){ + return commentService.AddLikeCount(id); +// HttpServletResponse resp = null; +// PrintWriter writer = null; +// try { +// writer = resp.getWriter(); +// } catch (IOException e) { +// throw new RuntimeException(e); +// } +// writer.println(commentService.AddLikeCount(id)); + } + + } diff --git a/src/main/java/com/lovenav/dao/CommentDao.java b/src/main/java/com/lovenav/dao/CommentDao.java index 911061b..ea08fd0 100644 --- a/src/main/java/com/lovenav/dao/CommentDao.java +++ b/src/main/java/com/lovenav/dao/CommentDao.java @@ -5,6 +5,8 @@ import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository @Mapper public interface CommentDao { @@ -19,4 +21,6 @@ public interface CommentDao { int updateByPrimaryKeySelective(Comment record); int updateByPrimaryKey(Comment record); + + List selectByRootId(Integer rootid); } \ No newline at end of file diff --git a/src/main/java/com/lovenav/service/CommentService.java b/src/main/java/com/lovenav/service/CommentService.java index e6d5912..793d46f 100644 --- a/src/main/java/com/lovenav/service/CommentService.java +++ b/src/main/java/com/lovenav/service/CommentService.java @@ -6,5 +6,11 @@ import org.springframework.stereotype.Service; public interface CommentService { // 点赞 - public String AddLikeCount(int id); + String AddLikeCount(int id); +// 评论 + String Reply1(Comment comment); +// 回复 + String Reply2(Comment comment); +// 删除 + String Delete(Comment comment); } diff --git a/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java index 1eb272a..0b8b7dd 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/CommentServiceImpl.java @@ -1,24 +1,78 @@ package com.lovenav.service.serviceImpl; +import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.lovenav.dao.CommentDao; import com.lovenav.entity.Comment; import com.lovenav.service.CommentService; +import org.apache.ibatis.jdbc.Null; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.List; + @Service("commentService") public class CommentServiceImpl implements CommentService { @Autowired private CommentDao commentDao; - - // 点赞++ +// 评论功能 + @Override + public String Reply1(Comment comment){ + int result = commentDao.insert(comment); + HashMap hashMap = new HashMap<>(); + if(result > 0){ + hashMap.put("msg", "评论成功"); + return JSON.toJSONString(hashMap); + } + else{ + hashMap.put("msg", "评论失败"); + return JSON.toJSONString(hashMap); + } + } +// 回复功能 + @Override + public String Reply2(Comment comment){ + List list = commentDao.selectByRootId(comment.getRootCommentId()); + if(list.size()<5){ + commentDao.insert(comment); + return JSON.toJSONString(comment); + }else{ + HashMap result = new HashMap<>(); + result.put("code", 500); + result.put("msg", "回复超过上限"); + String jsonString = JSONObject.toJSONString(result); + return JSON.toJSONString(result); + } + } +// 删除 + @Override + public String Delete(Comment comment){ + int result = commentDao.deleteByPrimaryKey(comment.getId()); + HashMap hashMap = new HashMap<>(); + if (result > 0){ + hashMap.put("msg", "删除成功"); + String jsonString = JSONObject.toJSONString(hashMap); + return JSON.toJSONString(hashMap); + }else{ + hashMap.put("msg","删除失败"); + return JSON.toJSONString(hashMap); + } + } +// 点赞++ @Override public String AddLikeCount(int id){ Comment comment = commentDao.selectByPrimaryKey(id); + if(comment.getLikeCount() == 0){ + + } comment.setLikeCount(comment.getLikeCount()+1); - commentDao.updateByPrimaryKey(comment); - return JSONObject.toJSONString(comment.getLikeCount()); + int result = commentDao.updateByPrimaryKey(comment); + if(result>0){ + return JSONObject.toJSONString(comment.getLikeCount()); + }else{ + return JSONObject.toJSONString("点赞失败"); + } } } diff --git a/src/main/resources/mybatis/CommentDao.xml b/src/main/resources/mybatis/CommentDao.xml index 8b1abd7..0dd4a41 100644 --- a/src/main/resources/mybatis/CommentDao.xml +++ b/src/main/resources/mybatis/CommentDao.xml @@ -32,7 +32,7 @@ root_comment_id, like_count, comment_time, comment_status, update_time, rating ) - values (#{urlId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, + values (#{urlId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, #{rootCommentId,jdbcType=INTEGER}, #{likeCount,jdbcType=BIGINT}, #{commentTime,jdbcType=TIMESTAMP}, #{commentStatus,jdbcType=TINYINT}, #{updateTime,jdbcType=TIMESTAMP}, #{rating,jdbcType=INTEGER} ) @@ -144,4 +144,12 @@ rating = #{rating,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} + + + \ No newline at end of file