This commit is contained in:
禾几海
2020-05-27 13:28:48 +08:00
parent 21adedea84
commit 260ae53c8d
9 changed files with 151 additions and 74 deletions

View File

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

View File

@@ -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<ArticleModel> adminArticles(int count, int page) {
PageHelper.startPage(page, count);
PageHelper.startPage(page, count, "articleId desc");
List<Article> articleList = articleMapper.findAll();
PageData<ArticleModel> pageData = new PageData<ArticleModel>(new PageInfo<Article>(articleList));
List<ArticleModel> articleModelList = new ArrayList<>();

View File

@@ -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<Category> all = categoryMapper.findAll();
List<CategoryModel> modelList = new ArrayList<>();
all.forEach(e -> modelList.add(ModalTrans.category(e)));
all.forEach(e -> {
CategoryModel model = ModalTrans.category(e);
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(e.getId());
List<ArticleModel> 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<CategoryModel>(new PageInfo<Category>(all), modelList);
}
}

View File

@@ -99,7 +99,7 @@ public class CommentServiceImpl implements CommentService {
@Override
public PageData<CommentModel> retrievePage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePath(pagePath);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, -1);
return pageTrans(list);
}
@@ -122,23 +122,29 @@ public class CommentServiceImpl implements CommentService {
@Override
public PageData<CommentModel> retrievePageByPageAndPid(String pagePath, long pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, pid);
return pageTrans(list);
List<Comment> list = commentMapper.findAllByPagePath(pagePath);
return pageTrans(list, true);
}
@Override
public PageData<CommentModel> retrievePageByPage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, -1);
return pageTrans(list);
List<Comment> list = commentMapper.findAllByPagePath(pagePath);
return pageTrans(list, true);
}
private PageData<CommentModel> pageTrans(List<Comment> commentList) {
return pageTrans(commentList, false);
}
private PageData<CommentModel> pageTrans(List<Comment> commentList, boolean noResponseList) {
PageInfo<Comment> p = PageInfo.of(commentList);
List<CommentModel> 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<CommentModel>(p, modelList);

View File

@@ -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<User> all = userMapper.findAll();
List<UserModel> modelList = new ArrayList<>();
all.forEach(user -> modelList.add(ModalTrans.user(user)));
all.forEach(user -> modelList.add(ModalTrans.userFullInfo(user)));
return new PageData<UserModel>(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()));
}
}

View File

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

View File

@@ -86,21 +86,29 @@
<select id="findAllByPagePath" resultMap="commentViewResultMap">
select *
from commentView
where pagePath = #{pagePath}
<if test="pagePath!= null ">
where pagePath = #{pagePath}
</if>
</select>
<select id="findAllByPagePathAndFromUser" resultMap="commentViewResultMap">
select *
from commentView
where pagePath = #{pagePath}
and fromAuthorId = #{userId}
where
<if test="pagePath!= null ">
pagePath = #{pagePath} and
</if>
fromAuthorId = #{userId}
</select>
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap">
select *
from commentView
where pagePath = #{pagePath}
and pid = #{pid}
where
<if test="pagePath!= null ">
pagePath = #{pagePath} and
</if>
pid = #{pid}
</select>
<select id="countByPagePath" resultType="java.lang.Long">
@@ -108,6 +116,7 @@
from commentView
where pagePath = #{pagePath}
</select>
<select id="getLastestComment" resultMap="commentViewResultMap">
select *
from commentView
@@ -117,7 +126,7 @@
<select id="count" resultType="long">
select count(*)
from article
from comment
where is_delete = false;
</select>

View File

@@ -155,7 +155,6 @@
select *
from articleView
where isDelete = false
order by articleId desc
</select>