You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

277 lines
9.4 KiB

package cc.bnblogs.controller;
import cc.bnblogs.Exception.CustomException;
import cc.bnblogs.common.*;
import cc.bnblogs.config.WebConfig;
import cc.bnblogs.enums.ErrorCodeEnum;
import cc.bnblogs.enums.ResultEnum;
import cc.bnblogs.pojo.Article;
import cc.bnblogs.pojo.Category;
import cc.bnblogs.pojo.Comment;
import cc.bnblogs.pojo.Tag;
import cc.bnblogs.service.*;
import cc.bnblogs.utils.CookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author zfp@bnblogs.cc
* @createTime: 2022/10/19
*/
@Controller
public class IndexController {
private final FriendsService friendsService;
private final NavigationService navigationService;
private final TagService tagService;
private final ArticleService articleService;
private final BannerService bannerService;
private final CategoryService categoryService;
private final LRUCache lruCache;
private final CommentService commentService;
private final WebSite webSite;
public IndexController(FriendsService friendsService, NavigationService navigationService,
TagService tagService, ArticleService articleService, BannerService bannerService,
CategoryService categoryService, LRUCache lruCache, CommentService commentService, WebSite webSite) {
this.friendsService = friendsService;
this.navigationService = navigationService;
this.tagService = tagService;
this.articleService = articleService;
this.bannerService = bannerService;
this.categoryService = categoryService;
this.lruCache = lruCache;
this.commentService = commentService;
this.webSite = webSite;
}
@ModelAttribute
private void indexModel(Model model) {
// todo 可以在这里定义这个controller公用的model的属性
model.addAttribute("friends",friendsService.list());
model.addAttribute("navigations",navigationService.show());
// 标签云
model.addAttribute("tags",tagService.list((int)tagService.count()));
model.addAttribute("hots",articleService.hotList(5));
int size = lruCache.size();
List<String> keywords = lruCache.list();
Collections.reverse(keywords);
if (size < WebConfig.MAX_LRU_CACHE_SIZE) {
// 关键字不够时用标签来补充
keywords.addAll(tagService.list( WebConfig.MAX_LRU_CACHE_SIZE - size).stream()
.map(Tag::getName).collect(Collectors.toList()));
}
model.addAttribute("keywords",keywords);
}
// 前台访问路由
@GetMapping("/")
public String index(@RequestParam(required = false, defaultValue = "1") Integer pageNumber,Model model) {
pageNumber = pageNumber < 1 ? 1: pageNumber;
model.addAttribute("articlePage",articleService.search(ArticleSearch.indexSearch(pageNumber,5)));
model.addAttribute("banners",bannerService.list());
return "index";
}
/**
* 对应标签的列表页
* 格式: http://localhost:8080/tag/{id}.html?pageNumber=1
* @param id 标签id
* @param pageNumber 页号
* @param model model
* @return 标签列表页
*/
@GetMapping("/tag/{id}.html")
public String tagList(@PathVariable Integer id,
@RequestParam(required = false,defaultValue = "1") Integer pageNumber,
Model model) throws CustomException {
Tag tag = tagService.detail(id);
if (Objects.isNull(tag)) {
throw new CustomException();
}
model.addAttribute("tag",tag);
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5);
articleSearch.setTid(id);
PageHelper<Article> articlePage = articleService.search(articleSearch);
model.addAttribute("articlePage",articlePage);
model.addAttribute("pageType","tag");
return "list";
}
/**
* 对应分类的列表页
* 格式: http://localhost:8080/category/{id}.html?pageNumber=1
* @param id 分类id
* @param pageNumber 页号
* @param model model
* @return 分类列表页
*/
@GetMapping("/category/{id}.html")
public String categoryList(@PathVariable Integer id,
@RequestParam(required = false,defaultValue = "1") Integer pageNumber,
Model model) throws CustomException {
Category category = categoryService.detail(id);
if (Objects.isNull(category)) {
throw new CustomException();
}
model.addAttribute("category",category);
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5);
articleSearch.setCid(id);
PageHelper<Article> articlePage = articleService.search(articleSearch);
model.addAttribute("articlePage",articlePage);
model.addAttribute("pageType","category");
return "list";
}
/**
* 返回搜索到的文章
* @param pageNumber 页号
* @param keyword 搜索关键字(同时搜索标题和内容中是否包含该关键字)
* @param model model
* @return 搜索结果
*/
@GetMapping("/search.html")
public String search(@RequestParam(required = false,defaultValue = "1") Integer pageNumber,
String keyword,
Model model){
lruCache.add(keyword);
model.addAttribute("keyword",keyword);
ArticleSearch articleSearch = ArticleSearch.indexSearch(pageNumber,5);
articleSearch.setKeyword(keyword);
PageHelper<Article> articlePage = articleService.search(articleSearch);
model.addAttribute("articlePage",articlePage);
model.addAttribute("pageType","search");
return "list";
}
/**
* 前台分类列表页
* @param model model
* @return 所有分类
*/
@GetMapping("/category.html")
public String category(Model model) {
model.addAttribute("categories",categoryService.show());
return "category";
}
/**
* 前台标签列表页
* @param model 引入的mvc模型
* @return 所有标签
*/
@GetMapping("/tag.html")
public String tag(Model model) {
model.addAttribute("tagList",tagService.show());
return "tags";
}
/**
* 404错误请求
* @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")
public String detail(@PathVariable Integer id, Model model,
HttpServletRequest request, HttpServletResponse response)
throws CustomException {
Article article = articleService.detail(id);
// 文章不存在
if (Objects.isNull(article)) {
throw new CustomException();
}
// 如果cookie不存在
if (Objects.isNull(CookieUtil.getCookie(request,Article.VIEW_PREFIX + id))) {
// 更新文章浏览量
articleService.viewArticle(id);
CookieUtil.setCookie(response,Article.VIEW_PREFIX + id,"true");
}
model.addAttribute("article",article);
PageHelper<Comment> commentPage = commentService.show(id,1);
// 返回第一页评论
model.addAttribute("commentPage",commentPage);
return "detail";
}
/**
* 提交评论
* @param comment 文章评论
* @return 操作结果
*/
@PostMapping("/comment")
@ResponseBody
public Result<String> comment(Comment comment) {
// // 邮箱是否和管理员相同
// if (StringUtils.equals(comment.getEmail(),webSite.getMail())) {
// return Result.error(ResultEnum.RESULT_MAIL_FAILED);
// }
// // 昵称不能和管理员重名
// if (StringUtils.equals(comment.getNickname(),webSite.getNickname())) {
// return Result.error(ResultEnum.RESULT_COMMENT_NAME_FAILED);
// }
commentService.save(comment);
return Result.success();
}
/**
* 评论局部刷新
* @param id 评论id
* @param pageNumber 页号
* @param model MVCmodel
* @return
*/
@GetMapping("/comment/{id}")
public String comments(@PathVariable Integer id, @RequestParam(required = false, defaultValue = "1") Integer pageNumber, Model model) {
pageNumber = Math.max(1, pageNumber);
PageHelper<Comment> commentPage = commentService.show(id, pageNumber);
model.addAttribute("commentPage", commentPage);
return "detail::comments";
}
}