评论,回复,删评

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

@@ -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);
}

View File

@@ -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("点赞失败");
}
}
}