add comment module

pangu
PeterAlbus 3 years ago
parent 75b6189e64
commit f68c09125a

@ -39,6 +39,10 @@ public class StpInterfaceImpl implements StpInterface
list.add("write-article");
list.add("modify-article");
}
if(user.getUserIdentity()<=1)
{
list.add("delete-comment");
}
if(user.getUserIdentity()<=5)
{
list.add("comment");
@ -56,7 +60,7 @@ public class StpInterfaceImpl implements StpInterface
List<String> list = new ArrayList<String>();
if(user.getUserIdentity()==0)
{
list.add("admin");
list.add("owner");
}
if(user.getUserIdentity()==5)
{

@ -21,7 +21,8 @@ import java.util.concurrent.TimeUnit;
/**
* The type Blog controller.
*
* @author PeterAlbus Created on 2021/7/20.
* @author PeterAlbus
* Created on 2021/7/20.
*/
@RestController
@CrossOrigin
@ -166,11 +167,7 @@ public class BlogController
{
String uploadPath="/home/PeterAlbus/assets/blog/imgs/cover/";
String fileName = file.getOriginalFilename();
String type="unknown";
if(fileName!=null)
{
type=fileName.substring(fileName.lastIndexOf('.'));
}
String type=TypeUtil.getType(fileName);
if(TypeUtil.isImg(type))
{
String newName= UUID.randomUUID().toString().replace("-", "").toLowerCase()+type;

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

@ -2,6 +2,7 @@ package com.peteralbus.controller;
import com.peteralbus.domain.Photo;
import com.peteralbus.service.PhotoService;
import com.peteralbus.util.TypeUtil;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -55,30 +56,11 @@ public class PhotoController
{
String uploadPath="/home/PeterAlbus/assets/blog/imgs/photo/";
String fileName = file.getOriginalFilename();
String type="unknown";
final Set<String> allowTypes = new HashSet<String>(){{
add(".jpg");
add(".jpeg");
add(".png");
add(".JPG");
add(".JPEG");
add(".PNG");
add(".webp");
add(".WEBP");
add(".tif");
add(".TIF");
add(".bmp");
add(".gif");
add(".BMP");
add(".GIF");
}};
if(fileName!=null)
String type=TypeUtil.getType(fileName);
String newName=UUID.randomUUID().toString().replace("-", "").toLowerCase()+type;
if(TypeUtil.isImg(type))
{
type=fileName.substring(fileName.lastIndexOf('.'));
}
if(allowTypes.contains(type))
{
File dest = new File(uploadPath + fileName);
File dest = new File(uploadPath + newName);
try {
// 上传的文件被保存了
file.transferTo(dest);
@ -110,26 +92,8 @@ public class PhotoController
/*pathExample:blog/imgs/photo/*/
String uploadPath="/home/PeterAlbus/assets/"+path;
String fileName=file.getOriginalFilename();
String type="unknown";
final Set<String> allowTypes = new HashSet<String>(){{
add(".jpg");
add(".jpeg");
add(".png");
add(".JPG");
add(".JPEG");
add(".PNG");
add(".tif");
add(".TIF");
add(".bmp");
add(".BMP");
add(".gif");
add(".GIF");
}};
if(fileName!=null)
{
type=fileName.substring(fileName.lastIndexOf('.'));
}
if(allowTypes.contains(type))
String type=TypeUtil.getType(fileName);
if(TypeUtil.isImg(type))
{
if(!"".equals(saveName))
{
@ -165,20 +129,8 @@ public class PhotoController
/*pathExample:blog/imgs/photo/*/
String uploadPath="/home/PeterAlbus/assets/"+path;
String fileName = file.getOriginalFilename();
String type="unknown";
final Set<String> allowTypes = new HashSet<String>(){{
add(".jpg");
add(".jpeg");
add(".png");
add(".JPG");
add(".JPEG");
add(".PNG");
}};
if(fileName!=null)
{
type=fileName.substring(fileName.lastIndexOf('.'));
}
if(allowTypes.contains(type))
String type=TypeUtil.getType(fileName);
if(TypeUtil.isImg(type))
{
File dest = new File(uploadPath + fileName);
try {

@ -8,15 +8,18 @@ import com.peteralbus.service.UserService;
import com.peteralbus.util.RandomUtil;
import com.peteralbus.util.RedisUtils;
import com.peteralbus.util.SmsUtil;
import com.peteralbus.util.TypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
@ -184,6 +187,10 @@ public class UserController
@RequestMapping("/changePassword")
public String changePassword(Long userId,String oldPassword,String newPassword)
{
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(userId))
{
return "noPermission";
}
int result=userService.changePassword(userId,oldPassword,newPassword);
if(result==-1)
{
@ -196,4 +203,136 @@ public class UserController
StpUtil.logout(userId);
return "success";
}
@RequestMapping("/setPhone")
public String setPhone(Long userId,String userPhone,String verifyCode)
{
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(userId))
{
return "noPermission";
}
User user=userService.getUserById(userId);
if(user==null)
{
return "wrongUserId";
}
String verifyCodeKey="verifyCode:"+userPhone;
if(redisUtils.exists(verifyCodeKey))
{
if(redisUtils.get(verifyCodeKey).equals(verifyCode))
{
user.setUserPhone(userPhone);
if(userService.updateUser(user)>0)
{
redisUtils.remove(verifyCodeKey);
return "success";
}
return "fail";
}
return "wrongVerifyCode";
}
return "needRequestVerifyCode";
}
@RequestMapping("/setMail")
public String setMail(Long userId,String userMail,String verifyCode)
{
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(userId))
{
return "noPermission";
}
User user=userService.getUserById(userId);
if(user==null)
{
return "wrongUserId";
}
String verifyCodeKey="verifyCode:"+userMail;
if(redisUtils.exists(verifyCodeKey))
{
if(redisUtils.get(verifyCodeKey).equals(verifyCode))
{
user.setUserMail(userMail);
if(userService.updateUser(user)>0)
{
redisUtils.remove(verifyCodeKey);
return "success";
}
return "fail";
}
return "wrongVerifyCode";
}
return "needRequestVerifyCode";
}
@PostMapping("/uploadAvatar")
public String upload(@RequestParam("file") MultipartFile file,Long userId)
{
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(userId))
{
return "noPermission";
}
String uploadPath="/home/PeterAlbus/assets/blog/imgs/avatar/";
String fileName = file.getOriginalFilename();
String type=TypeUtil.getType(fileName);
User user=userService.getUserById(userId);
if(user==null)
{
return "wrongUserId";
}
String newName;
if(user.getUserPhone()!=null)
{
newName="avatar_"+user.getUserPhone()+"_"+UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
else
{
newName="avatar_"+user.getUserMail()+"_"+UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
if(TypeUtil.isImg(type))
{
newName = newName +type;
File dest = new File(uploadPath + newName);
try {
file.transferTo(dest);
user.setUserAvatar("https://file.peteralbus.com/assets/blog/imgs/avatar/"+newName);
userService.updateUser(user);
return "https://file.peteralbus.com/assets/blog/imgs/avatar/"+newName;
} catch (IOException e) {
return "上传错误:"+e.getMessage();
}
}
return "typeError";
}
@RequestMapping("/changeUsername")
public String changeUsername(Long userId,String username)
{
if(!Long.valueOf((String) StpUtil.getLoginId()).equals(userId))
{
return "noPermission";
}
User user=userService.getUserById(userId);
if(user==null)
{
return "wrongUserId";
}
user.setUserUsername(username);
int result=userService.updateUser(user);
if(result>0)
{
return "success";
}
return "fail";
}
@RequestMapping("/getUserById")
public User getUserById(Long userId)
{
User user=userService.getUserById(userId);
user.setUserPhone(null);
user.setUserPassword(null);
user.setUserMail(null);
user.setUserSalt(null);
return user;
}
}

@ -7,12 +7,15 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.sql.Date;
import java.time.LocalDateTime;
/**
* The type Blog.
*
* @author PeterAlbus
* Created on 2021/7/21.
*/
@ -68,4 +71,11 @@ public class Blog implements Serializable
* The Is top.
*/
Integer isTop;
/**
* The Gmt modified.
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
LocalDateTime gmtModified;
}

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

@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;
/**
@ -47,12 +48,14 @@ public class BlogServiceImpl implements BlogService
public int add(Blog blog)
{
blog.setBlogTime(new Date(System.currentTimeMillis()));
blog.setGmtModified(LocalDateTime.now());
return blogMapper.insert(blog);
}
@Override
public int update(Blog blog)
{
blog.setGmtModified(LocalDateTime.now());
return blogMapper.updateById(blog);
}
}

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

@ -21,10 +21,23 @@ public class TypeUtil
add(".JPEG");
add(".PNG");
add(".webp");
add(".tif");
add(".WEBP");
add(".tif");
add(".TIF");
add(".bmp");
add(".gif");
add(".BMP");
add(".GIF");
}};
return allowTypes.contains(type);
}
static public String getType(String fileName)
{
if(fileName!=null)
{
return fileName.substring(fileName.lastIndexOf('.'));
}
return "unknown";
}
}

Loading…
Cancel
Save