From f68c09125a5acc1f43c750e515b1cae8ed58f8bc Mon Sep 17 00:00:00 2001 From: PeterAlbus Date: Tue, 29 Mar 2022 00:03:26 +0800 Subject: [PATCH] add comment module --- .../peteralbus/config/StpInterfaceImpl.java | 6 +- .../peteralbus/controller/BlogController.java | 9 +- .../controller/CommentController.java | 106 +++++++++++++ .../controller/PhotoController.java | 66 ++------ .../peteralbus/controller/UserController.java | 145 +++++++++++++++++- src/main/java/com/peteralbus/domain/Blog.java | 10 ++ .../java/com/peteralbus/domain/Comment.java | 43 ++++++ .../com/peteralbus/mapper/CommentMapper.java | 15 ++ .../peteralbus/service/CommentService.java | 69 +++++++++ .../service/impl/BlogServiceImpl.java | 3 + .../service/impl/CommentServiceImpl.java | 77 ++++++++++ .../java/com/peteralbus/util/TypeUtil.java | 15 +- 12 files changed, 496 insertions(+), 68 deletions(-) create mode 100644 src/main/java/com/peteralbus/controller/CommentController.java create mode 100644 src/main/java/com/peteralbus/domain/Comment.java create mode 100644 src/main/java/com/peteralbus/mapper/CommentMapper.java create mode 100644 src/main/java/com/peteralbus/service/CommentService.java create mode 100644 src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java diff --git a/src/main/java/com/peteralbus/config/StpInterfaceImpl.java b/src/main/java/com/peteralbus/config/StpInterfaceImpl.java index d22d7c9..7cbb47d 100644 --- a/src/main/java/com/peteralbus/config/StpInterfaceImpl.java +++ b/src/main/java/com/peteralbus/config/StpInterfaceImpl.java @@ -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 list = new ArrayList(); if(user.getUserIdentity()==0) { - list.add("admin"); + list.add("owner"); } if(user.getUserIdentity()==5) { diff --git a/src/main/java/com/peteralbus/controller/BlogController.java b/src/main/java/com/peteralbus/controller/BlogController.java index 5c05703..2b33afd 100644 --- a/src/main/java/com/peteralbus/controller/BlogController.java +++ b/src/main/java/com/peteralbus/controller/BlogController.java @@ -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; diff --git a/src/main/java/com/peteralbus/controller/CommentController.java b/src/main/java/com/peteralbus/controller/CommentController.java new file mode 100644 index 0000000..8a85380 --- /dev/null +++ b/src/main/java/com/peteralbus/controller/CommentController.java @@ -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 getCommentByBlogId(Long blogId) + { + return commentService.getCommentByBlogId(blogId); + } + + @RequestMapping("/getCommentByCommentId") + public List getCommentByCommentId(Long commentId) + { + return commentService.getCommentByCommentId(commentId); + } + + @RequestMapping("/getCommentByUserId") + public List 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"; + } +} diff --git a/src/main/java/com/peteralbus/controller/PhotoController.java b/src/main/java/com/peteralbus/controller/PhotoController.java index 308dc48..36b034b 100644 --- a/src/main/java/com/peteralbus/controller/PhotoController.java +++ b/src/main/java/com/peteralbus/controller/PhotoController.java @@ -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 allowTypes = new HashSet(){{ - 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 allowTypes = new HashSet(){{ - 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 allowTypes = new HashSet(){{ - 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 { diff --git a/src/main/java/com/peteralbus/controller/UserController.java b/src/main/java/com/peteralbus/controller/UserController.java index 6aa4846..8a8dab1 100644 --- a/src/main/java/com/peteralbus/controller/UserController.java +++ b/src/main/java/com/peteralbus/controller/UserController.java @@ -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; + } } diff --git a/src/main/java/com/peteralbus/domain/Blog.java b/src/main/java/com/peteralbus/domain/Blog.java index 060b09b..2201d08 100644 --- a/src/main/java/com/peteralbus/domain/Blog.java +++ b/src/main/java/com/peteralbus/domain/Blog.java @@ -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; } diff --git a/src/main/java/com/peteralbus/domain/Comment.java b/src/main/java/com/peteralbus/domain/Comment.java new file mode 100644 index 0000000..57a3261 --- /dev/null +++ b/src/main/java/com/peteralbus/domain/Comment.java @@ -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; +} diff --git a/src/main/java/com/peteralbus/mapper/CommentMapper.java b/src/main/java/com/peteralbus/mapper/CommentMapper.java new file mode 100644 index 0000000..473c7e3 --- /dev/null +++ b/src/main/java/com/peteralbus/mapper/CommentMapper.java @@ -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 +{ +} diff --git a/src/main/java/com/peteralbus/service/CommentService.java b/src/main/java/com/peteralbus/service/CommentService.java new file mode 100644 index 0000000..63dd79e --- /dev/null +++ b/src/main/java/com/peteralbus/service/CommentService.java @@ -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 getCommentByBlogId(Long blogId); + + /** + * Gets comment by user id. + * + * @param userId the user id + * @return the comment by user id + */ + List getCommentByUserId(Long userId); + + /** + * Gets comment by comment id. + * + * @param commentId the comment id + * @return the comment by comment id + */ + List 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); +} diff --git a/src/main/java/com/peteralbus/service/impl/BlogServiceImpl.java b/src/main/java/com/peteralbus/service/impl/BlogServiceImpl.java index 06652f2..9b72a01 100644 --- a/src/main/java/com/peteralbus/service/impl/BlogServiceImpl.java +++ b/src/main/java/com/peteralbus/service/impl/BlogServiceImpl.java @@ -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); } } diff --git a/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java b/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java new file mode 100644 index 0000000..78e276a --- /dev/null +++ b/src/main/java/com/peteralbus/service/impl/CommentServiceImpl.java @@ -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 getCommentByBlogId(Long blogId) + { + QueryWrapper commentQueryWrapper=new QueryWrapper<>(); + commentQueryWrapper.eq("comment_target",1); + commentQueryWrapper.eq("comment_target_id",blogId); + return commentMapper.selectList(commentQueryWrapper); + } + + @Override + public List getCommentByUserId(Long userId) + { + QueryWrapper commentQueryWrapper=new QueryWrapper<>(); + commentQueryWrapper.eq("comment_target_id",userId); + return commentMapper.selectList(commentQueryWrapper); + } + + @Override + public List getCommentByCommentId(Long commentId) + { + QueryWrapper 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); + } +} diff --git a/src/main/java/com/peteralbus/util/TypeUtil.java b/src/main/java/com/peteralbus/util/TypeUtil.java index 643b124..566785b 100644 --- a/src/main/java/com/peteralbus/util/TypeUtil.java +++ b/src/main/java/com/peteralbus/util/TypeUtil.java @@ -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"; + } }