评论,回复,删评
This commit is contained in:
parent
1399bfd1e5
commit
ae032c0d53
@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<Integer> selectByRootId(Integer rootid);
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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<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
|
||||
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("点赞失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,4 +144,12 @@
|
||||
rating = #{rating,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
|
||||
<select id="selectByRootId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
select
|
||||
id
|
||||
from ln_comment
|
||||
where root_comment_id = #{rootCommentId,jdbcType=INTEGER}
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user