|
|
@ -1,7 +1,9 @@ |
|
|
|
package cc.bnblogs.controller; |
|
|
|
package cc.bnblogs.controller; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cc.bnblogs.Exception.CustomException; |
|
|
|
import cc.bnblogs.common.*; |
|
|
|
import cc.bnblogs.common.*; |
|
|
|
import cc.bnblogs.config.WebConfig; |
|
|
|
import cc.bnblogs.config.WebConfig; |
|
|
|
|
|
|
|
import cc.bnblogs.enums.ErrorCodeEnum; |
|
|
|
import cc.bnblogs.enums.ResultEnum; |
|
|
|
import cc.bnblogs.enums.ResultEnum; |
|
|
|
import cc.bnblogs.pojo.Article; |
|
|
|
import cc.bnblogs.pojo.Article; |
|
|
|
import cc.bnblogs.pojo.Category; |
|
|
|
import cc.bnblogs.pojo.Category; |
|
|
@ -99,10 +101,13 @@ public class IndexController { |
|
|
|
@GetMapping("/tag/{id}.html") |
|
|
|
@GetMapping("/tag/{id}.html") |
|
|
|
public String tagList(@PathVariable Integer id, |
|
|
|
public String tagList(@PathVariable Integer id, |
|
|
|
@RequestParam(required = false,defaultValue = "1") Integer pageNumber, |
|
|
|
@RequestParam(required = false,defaultValue = "1") Integer pageNumber, |
|
|
|
Model model){ |
|
|
|
Model model) throws CustomException { |
|
|
|
Tag tag = tagService.detail(id); |
|
|
|
Tag tag = tagService.detail(id); |
|
|
|
|
|
|
|
if (Objects.isNull(tag)) { |
|
|
|
|
|
|
|
throw new CustomException(); |
|
|
|
|
|
|
|
} |
|
|
|
model.addAttribute("tag",tag); |
|
|
|
model.addAttribute("tag",tag); |
|
|
|
// todo id不存在时要特殊处理
|
|
|
|
|
|
|
|
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); |
|
|
|
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); |
|
|
|
articleSearch.setTid(id); |
|
|
|
articleSearch.setTid(id); |
|
|
|
PageHelper<Article> articlePage = articleService.search(articleSearch); |
|
|
|
PageHelper<Article> articlePage = articleService.search(articleSearch); |
|
|
@ -123,10 +128,12 @@ public class IndexController { |
|
|
|
@GetMapping("/category/{id}.html") |
|
|
|
@GetMapping("/category/{id}.html") |
|
|
|
public String categoryList(@PathVariable Integer id, |
|
|
|
public String categoryList(@PathVariable Integer id, |
|
|
|
@RequestParam(required = false,defaultValue = "1") Integer pageNumber, |
|
|
|
@RequestParam(required = false,defaultValue = "1") Integer pageNumber, |
|
|
|
Model model){ |
|
|
|
Model model) throws CustomException { |
|
|
|
Category category = categoryService.detail(id); |
|
|
|
Category category = categoryService.detail(id); |
|
|
|
|
|
|
|
if (Objects.isNull(category)) { |
|
|
|
|
|
|
|
throw new CustomException(); |
|
|
|
|
|
|
|
} |
|
|
|
model.addAttribute("category",category); |
|
|
|
model.addAttribute("category",category); |
|
|
|
// todo 分类不存在时要特殊处理
|
|
|
|
|
|
|
|
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); |
|
|
|
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5); |
|
|
|
articleSearch.setCid(id); |
|
|
|
articleSearch.setCid(id); |
|
|
|
PageHelper<Article> articlePage = articleService.search(articleSearch); |
|
|
|
PageHelper<Article> articlePage = articleService.search(articleSearch); |
|
|
@ -178,22 +185,54 @@ public class IndexController { |
|
|
|
return "tags"; |
|
|
|
return "tags"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/error") |
|
|
|
/** |
|
|
|
public String error() { |
|
|
|
* 404错误请求 |
|
|
|
return "error"; |
|
|
|
* @param model MVC model |
|
|
|
|
|
|
|
* @return 错误页 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@GetMapping("/error/404.html") |
|
|
|
|
|
|
|
public String error404(Model model) { |
|
|
|
|
|
|
|
model.addAttribute("error", ErrorCodeEnum.ERROR_404); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 500错误请求 |
|
|
|
|
|
|
|
* @param model MVC model |
|
|
|
|
|
|
|
* @return 错误页 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@GetMapping("/error/500.html") |
|
|
|
|
|
|
|
public String error500(Model model) { |
|
|
|
|
|
|
|
model.addAttribute("error", ErrorCodeEnum.ERROR_500); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 文章详情页 |
|
|
|
|
|
|
|
* @param id 文章id |
|
|
|
|
|
|
|
* @param model MVC model |
|
|
|
|
|
|
|
* @param request HttpServletRequest请求 |
|
|
|
|
|
|
|
* @param response HttpServletResponse响应 |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{id}.html") |
|
|
|
@GetMapping("/{id}.html") |
|
|
|
public String detail(@PathVariable Integer id, Model model, |
|
|
|
public String detail(@PathVariable Integer id, Model model, |
|
|
|
HttpServletRequest request, HttpServletResponse response) { |
|
|
|
HttpServletRequest request, HttpServletResponse response) |
|
|
|
|
|
|
|
throws CustomException { |
|
|
|
Article article = articleService.detail(id); |
|
|
|
Article article = articleService.detail(id); |
|
|
|
|
|
|
|
// 文章不存在
|
|
|
|
|
|
|
|
if (Objects.isNull(article)) { |
|
|
|
|
|
|
|
throw new CustomException(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果cookie不存在
|
|
|
|
// 如果cookie不存在
|
|
|
|
if (Objects.isNull(CookieUtil.getCookie(request,Article.VIEW_PREFIX + id))) { |
|
|
|
if (Objects.isNull(CookieUtil.getCookie(request,Article.VIEW_PREFIX + id))) { |
|
|
|
// 更新文章浏览量
|
|
|
|
// 更新文章浏览量
|
|
|
|
articleService.viewArticle(id); |
|
|
|
articleService.viewArticle(id); |
|
|
|
CookieUtil.setCookie(response,Article.VIEW_PREFIX + id,"true"); |
|
|
|
CookieUtil.setCookie(response,Article.VIEW_PREFIX + id,"true"); |
|
|
|
} |
|
|
|
} |
|
|
|
// todo 文章内容不存在时应该抛出异常
|
|
|
|
|
|
|
|
model.addAttribute("article",article); |
|
|
|
model.addAttribute("article",article); |
|
|
|
PageHelper<Comment> commentPage = commentService.show(id,1); |
|
|
|
PageHelper<Comment> commentPage = commentService.show(id,1); |
|
|
|
// 返回第一页评论
|
|
|
|
// 返回第一页评论
|
|
|
|