讲书下添加点赞评论

This commit is contained in:
wuchunlei
2023-11-29 14:49:29 +08:00
parent 70748a2e4c
commit 06359588f6
10 changed files with 284 additions and 0 deletions

View File

@@ -9,6 +9,7 @@
package com.peanut.common.utils;
import com.peanut.common.exception.RRException;
import com.peanut.modules.book.entity.MyUserEntity;
import com.peanut.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
@@ -36,6 +37,11 @@ public class ShiroUtils {
public static Long getUserId() {
return getUserEntity().getUserId();
}
public static Integer getUId() {
MyUserEntity user = (MyUserEntity)SecurityUtils.getSubject().getPrincipal();
return user.getId();
}
public static void setSessionAttribute(Object key, Object value) {
getSession().setAttribute(key, value);

View File

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

View File

@@ -0,0 +1,9 @@
package com.peanut.modules.book.dao;
import com.github.yulichang.base.MPJBaseMapper;
import com.peanut.modules.book.entity.BookTeachCommentEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookTeachCommentDao extends MPJBaseMapper<BookTeachCommentEntity> {
}

View File

@@ -0,0 +1,9 @@
package com.peanut.modules.book.dao;
import com.github.yulichang.base.MPJBaseMapper;
import com.peanut.modules.book.entity.BookTeachLikeEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookTeachLikeDao extends MPJBaseMapper<BookTeachLikeEntity> {
}

View File

@@ -0,0 +1,42 @@
package com.peanut.modules.book.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
@TableName("book_teach_comment")
public class BookTeachCommentEntity {
private static final long serialVersionUID = 1L;
@TableId
@TableField("id")
private Integer id;
@TableField("parent_id")
private Integer parentId;
@TableField(exist = false)
private List<BookTeachCommentEntity> comments;
@TableField("user_id")
private Integer userId;
@TableField(exist = false)
private MyUserEntity user;
@TableField("teach_id")
private Integer teachId;
private String content;
@TableField("create_time")
private Date createTime;
@TableField("del_flag")
private Integer delFlag;
}

View File

@@ -0,0 +1,26 @@
package com.peanut.modules.book.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("book_teach_like")
public class BookTeachLikeEntity {
private static final long serialVersionUID = 1L;
@TableId
@TableField("id")
private Integer id;
@TableField("user_id")
private Integer userId;
@TableField("teach_id")
private Integer teachId;
@TableField("del_flag")
private Integer delFlag;
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.book.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.book.entity.BookTeachCommentEntity;
public interface BookTeachCommentService extends IService<BookTeachCommentEntity> {
}

View File

@@ -0,0 +1,7 @@
package com.peanut.modules.book.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.book.entity.BookTeachLikeEntity;
public interface BookTeachLikeService extends IService<BookTeachLikeEntity> {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.book.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.book.dao.BookTeachCommentDao;
import com.peanut.modules.book.entity.BookTeachCommentEntity;
import com.peanut.modules.book.service.BookTeachCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookTeachCommentService")
public class BookTeachCommentServiceImpl extends ServiceImpl<BookTeachCommentDao, BookTeachCommentEntity> implements BookTeachCommentService {
}

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.book.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.book.dao.BookTeachLikeDao;
import com.peanut.modules.book.entity.BookTeachLikeEntity;
import com.peanut.modules.book.service.BookTeachLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("bookTeachLikeService")
public class BookTeachLikeServiceImpl extends ServiceImpl<BookTeachLikeDao, BookTeachLikeEntity> implements BookTeachLikeService {
}