评论,回复,删评

This commit is contained in:
sjm 2023-12-22 10:03:33 +08:00
parent 1399bfd1e5
commit ae032c0d53
5 changed files with 114 additions and 15 deletions

View File

@ -1,26 +1,53 @@
package com.lovenav.controller; package com.lovenav.controller;
import com.alibaba.fastjson2.JSONObject;
import com.lovenav.entity.Comment; import com.lovenav.entity.Comment;
import com.lovenav.service.CommentService; import com.lovenav.service.CommentService;
import com.lovenav.service.serviceImpl.CommentServiceImpl; import com.lovenav.service.serviceImpl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
@RestController @RestController
@RequestMapping("/comment") @RequestMapping("/comment")
public class CommentController { public class CommentController {
@Autowired @Autowired
private CommentService commentService; private CommentService commentService;
// 评论功能
// @GetMapping(value= "/AddLikeCount") @RequestMapping(method = RequestMethod.POST, value = "/reply_comment")
// public String AddLikeCount_Controller(HttpSession session){ public String Reply1(@RequestBody Comment comment){
// int id = (int) session.getAttribute("id"); return commentService.Reply1(comment);
// return commentService.AddLikeCount(id);
// }
@GetMapping(value= "/AddLikeCount")
public String AddLikeCount(int id){
return commentService.AddLikeCount(id);
} }
// 回复功能
@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));
}
} }

View File

@ -5,6 +5,8 @@ import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
@Mapper @Mapper
public interface CommentDao { public interface CommentDao {
@ -19,4 +21,6 @@ public interface CommentDao {
int updateByPrimaryKeySelective(Comment record); int updateByPrimaryKeySelective(Comment record);
int updateByPrimaryKey(Comment record); int updateByPrimaryKey(Comment record);
List<Integer> selectByRootId(Integer rootid);
} }

View File

@ -6,5 +6,11 @@ import org.springframework.stereotype.Service;
public interface CommentService { public interface CommentService {
// 点赞 // 点赞
public String AddLikeCount(int id); String AddLikeCount(int id);
// 评论
String Reply1(Comment comment);
// 回复
String Reply2(Comment comment);
// 删除
String Delete(Comment comment);
} }

View File

@ -1,24 +1,78 @@
package com.lovenav.service.serviceImpl; package com.lovenav.service.serviceImpl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.lovenav.dao.CommentDao; import com.lovenav.dao.CommentDao;
import com.lovenav.entity.Comment; import com.lovenav.entity.Comment;
import com.lovenav.service.CommentService; import com.lovenav.service.CommentService;
import org.apache.ibatis.jdbc.Null;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
@Service("commentService") @Service("commentService")
public class CommentServiceImpl implements CommentService { public class CommentServiceImpl implements CommentService {
@Autowired @Autowired
private CommentDao commentDao; private CommentDao commentDao;
// 评论功能
// 点赞++ @Override
public String Reply1(Comment comment){
int result = commentDao.insert(comment);
HashMap<String, Object> 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<Integer> list = commentDao.selectByRootId(comment.getRootCommentId());
if(list.size()<5){
commentDao.insert(comment);
return JSON.toJSONString(comment);
}else{
HashMap<String, Object> 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<String, Object> 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 @Override
public String AddLikeCount(int id){ public String AddLikeCount(int id){
Comment comment = commentDao.selectByPrimaryKey(id); Comment comment = commentDao.selectByPrimaryKey(id);
if(comment.getLikeCount() == 0){
}
comment.setLikeCount(comment.getLikeCount()+1); comment.setLikeCount(comment.getLikeCount()+1);
commentDao.updateByPrimaryKey(comment); int result = commentDao.updateByPrimaryKey(comment);
if(result>0){
return JSONObject.toJSONString(comment.getLikeCount()); return JSONObject.toJSONString(comment.getLikeCount());
}else{
return JSONObject.toJSONString("点赞失败");
}
} }
} }

View File

@ -144,4 +144,12 @@
rating = #{rating,jdbcType=INTEGER} rating = #{rating,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER}
</update> </update>
<select id="selectByRootId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
id
from ln_comment
where root_comment_id = #{rootCommentId,jdbcType=INTEGER}
</select>
</mapper> </mapper>