add comment module
parent
75b6189e64
commit
f68c09125a
@ -0,0 +1,106 @@
|
||||
package com.peteralbus.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.peteralbus.domain.Comment;
|
||||
import com.peteralbus.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The type Comment controller.
|
||||
*
|
||||
* @author PeterAlbus
|
||||
* Created on 2022/3/28.
|
||||
*/
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/comment")
|
||||
public class CommentController
|
||||
{
|
||||
CommentService commentService;
|
||||
|
||||
@Autowired
|
||||
public void setCommentService(CommentService commentService)
|
||||
{
|
||||
this.commentService = commentService;
|
||||
}
|
||||
|
||||
@RequestMapping("/getCommentByBlogId")
|
||||
public List<Comment> getCommentByBlogId(Long blogId)
|
||||
{
|
||||
return commentService.getCommentByBlogId(blogId);
|
||||
}
|
||||
|
||||
@RequestMapping("/getCommentByCommentId")
|
||||
public List<Comment> getCommentByCommentId(Long commentId)
|
||||
{
|
||||
return commentService.getCommentByCommentId(commentId);
|
||||
}
|
||||
|
||||
@RequestMapping("/getCommentByUserId")
|
||||
public List<Comment> getCommentByUserId(Long userId)
|
||||
{
|
||||
return commentService.getCommentByUserId(userId);
|
||||
}
|
||||
|
||||
@RequestMapping("/getCommentById")
|
||||
public Comment getCommentById(Long commentId)
|
||||
{
|
||||
return commentService.getCommentById(commentId);
|
||||
}
|
||||
|
||||
@RequestMapping("/addComment")
|
||||
public String addComment(Comment comment)
|
||||
{
|
||||
if(!StpUtil.isLogin())
|
||||
{
|
||||
return "notLogin";
|
||||
}
|
||||
comment.setGmtCreate(LocalDateTime.now());
|
||||
comment.setGmtModified(LocalDateTime.now());
|
||||
if(comment.getCommentUserId()!=-1)
|
||||
{
|
||||
comment.setCommentUserId(Long.valueOf((String) StpUtil.getLoginId()));
|
||||
}
|
||||
if(commentService.addComment(comment)>0)
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
return "fail";
|
||||
}
|
||||
|
||||
@RequestMapping("/updateComment")
|
||||
public String updateComment(Comment comment)
|
||||
{
|
||||
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(comment.getCommentUserId()))
|
||||
{
|
||||
return "noPermission";
|
||||
}
|
||||
comment.setGmtModified(LocalDateTime.now());
|
||||
if(commentService.updateComment(comment)>0)
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
return "fail";
|
||||
}
|
||||
|
||||
@RequestMapping("/deleteComment")
|
||||
public String deleteComment(Comment comment)
|
||||
{
|
||||
final String deleteComment="delete-comment";
|
||||
if((!Long.valueOf((String) StpUtil.getLoginId()).equals(comment.getCommentUserId()))||StpUtil.hasPermission(deleteComment))
|
||||
{
|
||||
return "noPermission";
|
||||
}
|
||||
if(commentService.deleteComment(comment)>0)
|
||||
{
|
||||
return "success";
|
||||
}
|
||||
return "fail";
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.peteralbus.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* The type Comment.
|
||||
*
|
||||
* @author PeterAlbus
|
||||
* Created on 2022/3/28.
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@TableName("`comment`")
|
||||
public class Comment
|
||||
{
|
||||
@TableId(type= IdType.ASSIGN_ID)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
Long commentId;
|
||||
Integer commentTarget;
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
Long commentTargetId;
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
Long commentUserId;
|
||||
String commentContent;
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
LocalDateTime gmtCreate;
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
LocalDateTime gmtModified;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.peteralbus.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.peteralbus.domain.Comment;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* The interface Comment mapper.
|
||||
* @author PeterAlbus
|
||||
* Created on 2022/3/28
|
||||
*/
|
||||
@Mapper
|
||||
public interface CommentMapper extends BaseMapper<Comment>
|
||||
{
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.peteralbus.service;
|
||||
|
||||
import com.peteralbus.domain.Comment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The interface Comment service.
|
||||
*
|
||||
* @author PeterAlbus Created on 2022/3/28.
|
||||
*/
|
||||
public interface CommentService
|
||||
{
|
||||
/**
|
||||
* Gets comment by blog id.
|
||||
*
|
||||
* @param blogId the blog id
|
||||
* @return the comment by blog id
|
||||
*/
|
||||
List<Comment> getCommentByBlogId(Long blogId);
|
||||
|
||||
/**
|
||||
* Gets comment by user id.
|
||||
*
|
||||
* @param userId the user id
|
||||
* @return the comment by user id
|
||||
*/
|
||||
List<Comment> getCommentByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* Gets comment by comment id.
|
||||
*
|
||||
* @param commentId the comment id
|
||||
* @return the comment by comment id
|
||||
*/
|
||||
List<Comment> getCommentByCommentId(Long commentId);
|
||||
|
||||
/**
|
||||
* Gets comment by id.
|
||||
*
|
||||
* @param commentId the comment id
|
||||
* @return the comment by id
|
||||
*/
|
||||
Comment getCommentById(Long commentId);
|
||||
|
||||
/**
|
||||
* Add comment int.
|
||||
*
|
||||
* @param comment the comment
|
||||
* @return the int
|
||||
*/
|
||||
int addComment(Comment comment);
|
||||
|
||||
/**
|
||||
* Update comment int.
|
||||
*
|
||||
* @param comment the comment
|
||||
* @return the int
|
||||
*/
|
||||
int updateComment(Comment comment);
|
||||
|
||||
/**
|
||||
* Delete comment int.
|
||||
*
|
||||
* @param comment the comment
|
||||
* @return the int
|
||||
*/
|
||||
int deleteComment(Comment comment);
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.peteralbus.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peteralbus.domain.Comment;
|
||||
import com.peteralbus.mapper.CommentMapper;
|
||||
import com.peteralbus.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The type Comment service.
|
||||
* @author PeterAlbus
|
||||
* Created on 2022/3/28.
|
||||
*/
|
||||
@Service
|
||||
public class CommentServiceImpl implements CommentService
|
||||
{
|
||||
CommentMapper commentMapper;
|
||||
|
||||
@Autowired
|
||||
public void setCommentMapper(CommentMapper commentMapper)
|
||||
{
|
||||
this.commentMapper = commentMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Comment> getCommentByBlogId(Long blogId)
|
||||
{
|
||||
QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
|
||||
commentQueryWrapper.eq("comment_target",1);
|
||||
commentQueryWrapper.eq("comment_target_id",blogId);
|
||||
return commentMapper.selectList(commentQueryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Comment> getCommentByUserId(Long userId)
|
||||
{
|
||||
QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
|
||||
commentQueryWrapper.eq("comment_target_id",userId);
|
||||
return commentMapper.selectList(commentQueryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Comment> getCommentByCommentId(Long commentId)
|
||||
{
|
||||
QueryWrapper<Comment> commentQueryWrapper=new QueryWrapper<>();
|
||||
commentQueryWrapper.eq("comment_target",2);
|
||||
commentQueryWrapper.eq("comment_target_id",commentId);
|
||||
return commentMapper.selectList(commentQueryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comment getCommentById(Long commentId)
|
||||
{
|
||||
return commentMapper.selectById(commentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addComment(Comment comment)
|
||||
{
|
||||
return commentMapper.insert(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateComment(Comment comment)
|
||||
{
|
||||
return commentMapper.updateById(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteComment(Comment comment)
|
||||
{
|
||||
return commentMapper.deleteById(comment);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue