From 260ae53c8d2a912b961b7440b5974451872c45ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Wed, 27 May 2020 13:28:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.sql | 79 +++++++++---------- .../blog/controller/CommentController.java | 55 +++++++++++-- .../serviceimpl/ArticleServiceImpl.java | 6 +- .../serviceimpl/CategoryServiceImpl.java | 18 ++++- .../serviceimpl/CommentServiceImpl.java | 18 +++-- .../service/serviceimpl/UserServiceImpl.java | 12 +-- .../java/cn/celess/blog/util/ModalTrans.java | 15 +++- src/main/resources/mapper/CommentMapper.xml | 21 +++-- src/main/resources/mapper/articleMapper.xml | 1 - 9 files changed, 151 insertions(+), 74 deletions(-) diff --git a/blog.sql b/blog.sql index fa4cf14..dc04e28 100644 --- a/blog.sql +++ b/blog.sql @@ -5,20 +5,20 @@ USE t_blog; CREATE TABLE `article` ( `a_id` bigint(20) primary key auto_increment, - `a_title` varchar(255) not null unique comment '文章标题', - `a_summary` varchar(255) not null comment '文章摘要', - `a_md_content` longtext not null comment '文章Markdown内容', - `a_url` tinytext default null comment '转载文章的原文链接', - `a_author_id` int not null comment '作者id', - `a_is_original` boolean default true comment '文章是否原创', - `a_reading_number` int default 0 comment '文章阅读数', - `a_like` int default 0 comment '文章点赞数', - `a_dislike` int default 0 comment '文章不喜欢数', - `a_category_id` int not null comment '文章分类id', - `a_publish_date` datetime default CURRENT_TIMESTAMP comment '文章发布时间', - `a_update_date` datetime default null comment '文章的更新时间', - `a_is_open` boolean default true comment '文章是否可见', - `is_delete` boolean not null default false comment '该数据是否被删除' + `a_title` varchar(255) COLLATE utf8mb4_unicode_ci not null unique comment '文章标题', + `a_summary` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '文章摘要', + `a_md_content` longtext COLLATE utf8mb4_unicode_ci not null comment '文章Markdown内容', + `a_url` tinytext default null comment '转载文章的原文链接', + `a_author_id` int not null comment '作者id', + `a_is_original` boolean default true comment '文章是否原创', + `a_reading_number` int default 0 comment '文章阅读数', + `a_like` int default 0 comment '文章点赞数', + `a_dislike` int default 0 comment '文章不喜欢数', + `a_category_id` int not null comment '文章分类id', + `a_publish_date` datetime default CURRENT_TIMESTAMP comment '文章发布时间', + `a_update_date` datetime default null comment '文章的更新时间', + `a_is_open` boolean default true comment '文章是否可见', + `is_delete` boolean not null default false comment '该数据是否被删除' ) DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_general_ci,comment '文章表'; @@ -35,33 +35,32 @@ CREATE TABLE `tag_category` `t_id` bigint(20) primary key auto_increment, `t_name` varchar(255) not null, `is_category` boolean not null default true, + `is_delete` boolean not null default false comment '该数据是否被删除' ) comment '标签和分类表'; CREATE TABLE `comment` ( `co_id` bigint(20) primary key auto_increment, - `co_page_path` varchar(255) not null comment '评论/留言的页面', - `co_content` text not null comment '评论/留言内容', - `co_date` datetime not null comment '评论/留言的日期', - `co_status` tinyint not null default 0 comment '评论的状态', - `co_pid` bigint not null default -1 comment '评论/留言的父id', - `co_from_author_id` int not null comment '留言者id', - `co_to_author_id` int default null comment '父评论的作者id', - `is_delete` boolean not null default false comment '该数据是否被删除' + `co_page_path` varchar(255) not null comment '评论/留言的页面', + `co_content` text COLLATE utf8mb4_unicode_ci not null comment '评论/留言内容', + `co_date` datetime not null comment '评论/留言的日期', + `co_status` tinyint not null default 0 comment '评论的状态', + `co_pid` bigint not null default -1 comment '评论/留言的父id', + `co_from_author_id` int not null comment '留言者id', + `co_to_author_id` int default null comment '父评论的作者id', + `is_delete` boolean not null default false comment '该数据是否被删除' ) DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_general_ci,comment '评论/留言表'; - - CREATE TABLE `links` ( `l_id` bigint(20) primary key auto_increment, - `l_name` varchar(255) not null comment '友站名称', - `l_is_open` boolean default true comment '是否公开', - `l_url` varchar(255) not null comment '首页地址', - `l_icon_path` varchar(255) not null comment '友链的icon地址', - `l_desc` varchar(255) not null comment '友链的说明描述', - `is_delete` boolean not null default false comment '该数据是否被删除' + `l_name` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '友站名称', + `l_is_open` boolean default true comment '是否公开', + `l_url` varchar(255) not null comment '首页地址', + `l_icon_path` varchar(255) not null comment '友链的icon地址', + `l_desc` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '友链的说明描述', + `is_delete` boolean not null default false comment '该数据是否被删除' ) comment '友站表'; CREATE TABLE `visitor` @@ -76,9 +75,9 @@ CREATE TABLE `visitor` CREATE TABLE `web_update` ( `wu_id` int primary key auto_increment, - `wu_info` varchar(255) not null comment '更新内容', - `wu_time` datetime not null comment '更新时间', - `is_delete` boolean not null default false comment '该数据是否被删除' + `wu_info` varchar(255) COLLATE utf8mb4_unicode_ci not null comment '更新内容', + `wu_time` datetime not null comment '更新时间', + `is_delete` boolean not null default false comment '该数据是否被删除' ) comment '更新内容表'; CREATE TABLE `user` @@ -86,13 +85,13 @@ CREATE TABLE `user` `u_id` int not null primary key auto_increment, `u_email` varchar(50) not null, `u_pwd` varchar(40) not null comment '密码', - `u_email_status` boolean default false comment '邮箱验证状态', - `u_avatar` varchar(255) default null comment '用户头像', - `u_desc` tinytext default null comment '用户的描述', - `u_recently_landed_time` datetime default null comment '最近的登录时间', - `u_display_name` varchar(30) default null comment '展示的昵称', - `u_role` varchar(40) not null default 'user' comment '权限组', - `status` tinyint(1) not null default 0 comment '账户状态', + `u_email_status` boolean default false comment '邮箱验证状态', + `u_avatar` varchar(255) default null comment '用户头像', + `u_desc` tinytext COLLATE utf8mb4_unicode_ci default null comment '用户的描述', + `u_recently_landed_time` datetime default null comment '最近的登录时间', + `u_display_name` varchar(30) COLLATE utf8mb4_unicode_ci default null comment '展示的昵称', + `u_role` varchar(40) not null default 'user' comment '权限组', + `status` tinyint(1) not null default 0 comment '账户状态', unique key `uni_user_id` (`u_id`), unique key `uni_user_email` (`u_email`) ) comment '用户表'; diff --git a/src/main/java/cn/celess/blog/controller/CommentController.java b/src/main/java/cn/celess/blog/controller/CommentController.java index fb87a07..8841ef3 100644 --- a/src/main/java/cn/celess/blog/controller/CommentController.java +++ b/src/main/java/cn/celess/blog/controller/CommentController.java @@ -45,12 +45,20 @@ public class CommentController { * @param page 页码 * @return Response */ - @GetMapping("/comments") - public Response commentsOfArticle(@RequestParam("pagePath") String pagePath, - @RequestParam(value = "pid", required = false, defaultValue = "-1") long pid, + @GetMapping("/comments/{pagePath}/{pid}") + public Response commentsOfArticle(@PathVariable("pagePath") String pagePath, @PathVariable(value = "pid") long pid, @RequestParam(value = "count", required = false, defaultValue = "10") int count, @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return Response.success(commentService.retrievePageByPageAndPid(pagePath, pid, page, count)); + String path = ""; + if (pagePath.contains("article+")) { + path = "article/" + pagePath.split("\\+", 2)[1]; + } else { + path = pagePath; + } + if ("*".equals(pagePath)) { + path = null; + } + return Response.success(commentService.retrievePageByPageAndPid(path, pid, page, count)); } /** @@ -61,18 +69,51 @@ public class CommentController { * @param page page * @return Response */ - // FIXME:: 左斜线转义的异常 @GetMapping("/comment/pagePath/{pagePath}") public Response retrievePage(@PathVariable("pagePath") String pagePath, @RequestParam(value = "count", required = false, defaultValue = "10") int count, @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return Response.success(commentService.retrievePage(pagePath, page, count)); + String path = ""; + if (pagePath.contains("article+")) { + path = "article/" + pagePath.split("\\+", 2)[1]; + } else { + path = pagePath; + } + if ("*".equals(pagePath)) { + path = null; + } + return Response.success(commentService.retrievePage(path, page, count)); } @GetMapping("/user/comment/pagePath/{pagePath}") public Response userComment(@PathVariable("pagePath") String pagePath, @RequestParam(value = "count", required = false, defaultValue = "10") int count, @RequestParam(value = "page", required = false, defaultValue = "1") int page) { - return Response.success(commentService.retrievePageByAuthor(pagePath, page, count)); + String path = ""; + if (pagePath.contains("article+")) { + path = "article/" + pagePath.split("\\+", 2)[1]; + } else { + path = pagePath; + } + if ("*".equals(pagePath)) { + path = null; + } + return Response.success(commentService.retrievePageByAuthor(path, page, count)); + } + + @GetMapping("/admin/comment/pagePath/{pagePath}") + public Response adminComment(@PathVariable("pagePath") String pagePath, + @RequestParam(value = "count", required = false, defaultValue = "10") int count, + @RequestParam(value = "page", required = false, defaultValue = "1") int page) { + String path = ""; + if (pagePath.contains("article+")) { + path = "article/" + pagePath.split("\\+", 2)[1]; + } else { + path = pagePath; + } + if ("*".equals(pagePath)) { + path = null; + } + return Response.success(commentService.retrievePageByPage(path, page, count)); } } diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/ArticleServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/ArticleServiceImpl.java index 7389536..50b6998 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/ArticleServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/ArticleServiceImpl.java @@ -161,7 +161,7 @@ public class ArticleServiceImpl implements ArticleService { //数据判断 if (reqBody.getTitle() != null && !reqBody.getTitle().replaceAll(" ", "").isEmpty()) { - if (articleMapper.existsByTitle(reqBody.getTitle())) { + if (!article.getTitle().equals(reqBody.getTitle()) && articleMapper.existsByTitle(reqBody.getTitle())) { throw new MyException(ResponseEnum.ARTICLE_HAS_EXIST); } article.setTitle(reqBody.getTitle()); @@ -176,7 +176,7 @@ public class ArticleServiceImpl implements ArticleService { throw new MyException(ResponseEnum.PARAMETERS_ERROR); } - if (!RegexUtil.urlMatch(reqBody.getUrl())) { + if (!reqBody.getType() && !RegexUtil.urlMatch(reqBody.getUrl())) { throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR); } article.setType(reqBody.getType()); @@ -278,7 +278,7 @@ public class ArticleServiceImpl implements ArticleService { */ @Override public PageData adminArticles(int count, int page) { - PageHelper.startPage(page, count); + PageHelper.startPage(page, count, "articleId desc"); List
articleList = articleMapper.findAll(); PageData pageData = new PageData(new PageInfo
(articleList)); List articleModelList = new ArrayList<>(); diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/CategoryServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/CategoryServiceImpl.java index a2236b2..36c32e2 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/CategoryServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/CategoryServiceImpl.java @@ -1,7 +1,9 @@ package cn.celess.blog.service.serviceimpl; import cn.celess.blog.enmu.ResponseEnum; +import cn.celess.blog.entity.Article; import cn.celess.blog.entity.Category; +import cn.celess.blog.entity.model.ArticleModel; import cn.celess.blog.entity.model.CategoryModel; import cn.celess.blog.entity.model.PageData; import cn.celess.blog.exception.MyException; @@ -67,7 +69,21 @@ public class CategoryServiceImpl implements CategoryService { PageHelper.startPage(page, count); List all = categoryMapper.findAll(); List modelList = new ArrayList<>(); - all.forEach(e -> modelList.add(ModalTrans.category(e))); + all.forEach(e -> { + CategoryModel model = ModalTrans.category(e); + List
allByCategoryId = articleMapper.findAllByCategoryId(e.getId()); + List articleModelList = new ArrayList<>(); + allByCategoryId.forEach(article -> { + ArticleModel articleModel = ModalTrans.article(article, true); + articleModel.setPreArticle(null); + articleModel.setNextArticle(null); + articleModel.setTags(null); + articleModelList.add(articleModel); + }); + model.setArticles(articleModelList); + modelList.add(model); + }); + return new PageData(new PageInfo(all), modelList); } } diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java index 1487df9..5dcfecb 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/CommentServiceImpl.java @@ -99,7 +99,7 @@ public class CommentServiceImpl implements CommentService { @Override public PageData retrievePage(String pagePath, int page, int count) { PageHelper.startPage(page, count); - List list = commentMapper.findAllByPagePath(pagePath); + List list = commentMapper.findAllByPagePathAndPid(pagePath, -1); return pageTrans(list); } @@ -122,23 +122,29 @@ public class CommentServiceImpl implements CommentService { @Override public PageData retrievePageByPageAndPid(String pagePath, long pid, int page, int count) { PageHelper.startPage(page, count); - List list = commentMapper.findAllByPagePathAndPid(pagePath, pid); - return pageTrans(list); + List list = commentMapper.findAllByPagePath(pagePath); + return pageTrans(list, true); } @Override public PageData retrievePageByPage(String pagePath, int page, int count) { PageHelper.startPage(page, count); - List list = commentMapper.findAllByPagePathAndPid(pagePath, -1); - return pageTrans(list); + List list = commentMapper.findAllByPagePath(pagePath); + return pageTrans(list, true); } private PageData pageTrans(List commentList) { + return pageTrans(commentList, false); + } + + private PageData pageTrans(List commentList, boolean noResponseList) { PageInfo p = PageInfo.of(commentList); List modelList = new ArrayList<>(); commentList.forEach(l -> { CommentModel model = ModalTrans.comment(l); - model.setRespComment(this.retrievePageByPid(model.getId())); + if (!noResponseList) { + model.setRespComment(this.retrievePageByPid(model.getId())); + } modelList.add(model); }); return new PageData(p, modelList); diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/UserServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/UserServiceImpl.java index 0491be3..10bc0b0 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/UserServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/UserServiceImpl.java @@ -140,7 +140,7 @@ public class UserServiceImpl implements UserService { redisUtil.setEx(loginReq.getEmail() + "-passwordWrongTime", count + "", 2, TimeUnit.HOURS); throw new MyException(ResponseEnum.LOGIN_FAILURE); } - UserModel trans = ModalTrans.user(user); + UserModel trans = ModalTrans.userFullInfo(user); trans.setToken(token); return trans; @@ -167,7 +167,7 @@ public class UserServiceImpl implements UserService { userMapper.updateInfo(desc, displayName, user.getId()); redisUserUtil.set(user); - return ModalTrans.user(user); + return ModalTrans.userFullInfo(user); } @Override @@ -192,7 +192,7 @@ public class UserServiceImpl implements UserService { @Override public UserModel getUserInfoBySession() { User user = redisUserUtil.get(); - return ModalTrans.user(user); + return ModalTrans.userFullInfo(user); } @Override @@ -346,7 +346,7 @@ public class UserServiceImpl implements UserService { PageHelper.startPage(page, count); List all = userMapper.findAll(); List modelList = new ArrayList<>(); - all.forEach(user -> modelList.add(ModalTrans.user(user))); + all.forEach(user -> modelList.add(ModalTrans.userFullInfo(user))); return new PageData(PageInfo.of(all), modelList); } @@ -397,7 +397,7 @@ public class UserServiceImpl implements UserService { redisUserUtil.set(user); } logger.info("修改了用户 [id={}] 的用户的资料", userReq.getId()); - return ModalTrans.user(user); + return ModalTrans.userFullInfo(user); } @Override @@ -416,6 +416,6 @@ public class UserServiceImpl implements UserService { throw new MyException(ResponseEnum.PWD_NOT_SAME); } userMapper.updatePwd(user.getEmail(), MD5Util.getMD5(newPwd)); - return ModalTrans.user(userMapper.findByEmail(user.getEmail())); + return ModalTrans.userFullInfo(userMapper.findByEmail(user.getEmail())); } } diff --git a/src/main/java/cn/celess/blog/util/ModalTrans.java b/src/main/java/cn/celess/blog/util/ModalTrans.java index 22c6eec..76848c9 100644 --- a/src/main/java/cn/celess/blog/util/ModalTrans.java +++ b/src/main/java/cn/celess/blog/util/ModalTrans.java @@ -40,8 +40,7 @@ public class ModalTrans { return article1; } - - public static UserModel user(User user) { + public static UserModel userFullInfo(User user) { if (user == null || user.getId() == -1) { return null; } @@ -50,13 +49,21 @@ public class ModalTrans { userModel.setAvatarImgUrl(user.getAvatarImgUrl() == null || user.getAvatarImgUrl().length() == 0 ? null : "http://cdn.celess.cn/" + user.getAvatarImgUrl()); - userModel.setRole(null); - userModel.setEmailStatus(null); userModel.setDisplayName(user.getDisplayName() == null ? user.getEmail() : user.getDisplayName()); userModel.setRecentlyLandedDate(DateFormatUtil.get(user.getRecentlyLandedDate())); return userModel; } + public static UserModel user(User user) { + UserModel model = userFullInfo(user); + if (model == null) { + return null; + } + model.setRole(null); + model.setEmailStatus(null); + return model; + } + public static CategoryModel category(Category category) { if (category == null) { return null; diff --git a/src/main/resources/mapper/CommentMapper.xml b/src/main/resources/mapper/CommentMapper.xml index 042c078..b7db9b5 100644 --- a/src/main/resources/mapper/CommentMapper.xml +++ b/src/main/resources/mapper/CommentMapper.xml @@ -86,21 +86,29 @@ + select count(*) - from article + from comment where is_delete = false; diff --git a/src/main/resources/mapper/articleMapper.xml b/src/main/resources/mapper/articleMapper.xml index 275a0b4..14b07e9 100644 --- a/src/main/resources/mapper/articleMapper.xml +++ b/src/main/resources/mapper/articleMapper.xml @@ -155,7 +155,6 @@ select * from articleView where isDelete = false - order by articleId desc