讲书下添加点赞评论
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package com.peanut.modules.book.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
import com.peanut.common.utils.Query;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.common.utils.ShiroUtils;
|
||||
import com.peanut.modules.book.entity.BookTeachCommentEntity;
|
||||
import com.peanut.modules.book.entity.BookTeachLikeEntity;
|
||||
import com.peanut.modules.book.entity.MyUserEntity;
|
||||
import com.peanut.modules.book.service.BookTeachCommentService;
|
||||
import com.peanut.modules.book.service.BookTeachLikeService;
|
||||
import com.peanut.modules.book.service.MyUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("book/teach")
|
||||
public class BookTeachLikeAndCommentController {
|
||||
|
||||
@Autowired
|
||||
private BookTeachLikeService likeService;
|
||||
@Autowired
|
||||
private BookTeachCommentService commentService;
|
||||
@Autowired
|
||||
private MyUserService userService;
|
||||
|
||||
/**
|
||||
* 获取点赞数量
|
||||
*/
|
||||
@RequestMapping("/getLikeCount")
|
||||
public R getLikeCount(int teachId) {
|
||||
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BookTeachLikeEntity::getDelFlag,0);
|
||||
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
|
||||
int count = likeService.count(wrapper);
|
||||
return R.ok().put("count", count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或取消点赞
|
||||
*/
|
||||
@RequestMapping("/addOrCancelLike")
|
||||
public R addOrCancelLike(int teachId) {
|
||||
R r = new R();
|
||||
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
|
||||
wrapper.eq(BookTeachLikeEntity::getUserId, ShiroUtils.getUId());
|
||||
BookTeachLikeEntity like = likeService.getOne(wrapper);
|
||||
if (like == null){
|
||||
like = new BookTeachLikeEntity();
|
||||
like.setTeachId(teachId);
|
||||
like.setUserId(ShiroUtils.getUId());
|
||||
r.put("msg","点赞成功");
|
||||
}else {
|
||||
if (0==like.getDelFlag()){
|
||||
like.setDelFlag(1);
|
||||
r.put("msg","取消成功");
|
||||
}else {
|
||||
like.setDelFlag(0);
|
||||
r.put("msg","点赞成功");
|
||||
}
|
||||
}
|
||||
likeService.saveOrUpdate(like);
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本用户当前目录是否点赞
|
||||
*/
|
||||
@RequestMapping("/ifLike")
|
||||
public boolean ifLike(int teachId) {
|
||||
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(BookTeachLikeEntity::getDelFlag,0);
|
||||
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
|
||||
wrapper.eq(BookTeachLikeEntity::getUserId,ShiroUtils.getUId());
|
||||
BookTeachLikeEntity likeEntity = likeService.getOne(wrapper);
|
||||
if (likeEntity != null){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取评论列表
|
||||
*/
|
||||
@RequestMapping("/getCommentList")
|
||||
public R getCommentList(@RequestBody Map<String, Object> params) {
|
||||
MPJLambdaWrapper<BookTeachCommentEntity> wrapper = new MPJLambdaWrapper<>();
|
||||
wrapper.eq(BookTeachCommentEntity::getTeachId,params.get("teachId"));
|
||||
wrapper.eq(BookTeachCommentEntity::getParentId,0);
|
||||
wrapper.eq(BookTeachCommentEntity::getDelFlag,0);
|
||||
IPage<BookTeachCommentEntity> page = commentService.page(
|
||||
new Query<BookTeachCommentEntity>().getPage(params),wrapper);
|
||||
//返回体添加子评论和用户信息
|
||||
if (page.getRecords().size() > 0){
|
||||
for (BookTeachCommentEntity comment:page.getRecords()){
|
||||
getSonComment(comment);
|
||||
}
|
||||
}
|
||||
return R.ok().put("page", page);
|
||||
}
|
||||
//递归查询子评论
|
||||
private void getSonComment(BookTeachCommentEntity comment){
|
||||
LambdaQueryWrapper<BookTeachCommentEntity> w = new LambdaQueryWrapper();
|
||||
w.eq(BookTeachCommentEntity::getParentId,comment.getId());
|
||||
w.eq(BookTeachCommentEntity::getDelFlag,0);
|
||||
List<BookTeachCommentEntity> list = commentService.list(w);
|
||||
if (list.size() > 0){
|
||||
for (BookTeachCommentEntity c : list){
|
||||
getSonComment(c);
|
||||
}
|
||||
}
|
||||
comment.setComments(list);
|
||||
comment.setUser(userService.getById(comment.getUserId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加修改评论
|
||||
*/
|
||||
@RequestMapping("/addComment")
|
||||
public R addComment(@RequestBody BookTeachCommentEntity comment) {
|
||||
comment.setUserId(ShiroUtils.getUId());
|
||||
commentService.saveOrUpdate(comment);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*/
|
||||
@RequestMapping("/delComment")
|
||||
public R delComment(int commentId) {
|
||||
BookTeachCommentEntity comment = commentService.getById(commentId);
|
||||
comment.setDelFlag(1);
|
||||
commentService.updateById(comment);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user