246 lines
8.2 KiB
Java
246 lines
8.2 KiB
Java
package com.quinn.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.quinn.pojo.Blog;
|
|
import com.quinn.pojo.BlogCategory;
|
|
import com.quinn.pojo.BlogStar;
|
|
import com.quinn.pojo.Comment;
|
|
import com.quinn.service.BlogCategoryService;
|
|
import com.quinn.service.BlogService;
|
|
import com.quinn.service.BlogStarService;
|
|
import com.quinn.service.CommentService;
|
|
import com.quinn.utils.QuinnUtils;
|
|
import com.quinn.vo.BlogStarReq;
|
|
import com.quinn.vo.QuestionWriteForm;
|
|
import com.quinn.vo.StarValue;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* <p>
|
|
* 前端控制器
|
|
* </p>
|
|
*
|
|
* @author limqsh
|
|
* @since 2020-06-29
|
|
*/
|
|
@Controller
|
|
public class BlogController extends BaseModelController{
|
|
|
|
@Autowired
|
|
BlogCategoryService blogCategoryService;
|
|
@Autowired
|
|
BlogService blogService;
|
|
@Autowired
|
|
CommentService commentService;
|
|
@Resource
|
|
BlogStarService blogStarService;
|
|
|
|
// 列表展示
|
|
@GetMapping("/blog")
|
|
public String blogList(Model model){
|
|
|
|
Page<Blog> pageParam = new Page<>(1, 10);
|
|
blogService.page(pageParam,new QueryWrapper<Blog>().orderByDesc("sort").orderByDesc("gmt_create"));
|
|
// 结果
|
|
List<Blog> blogList = pageParam.getRecords();
|
|
model.addAttribute("blogList",blogList);
|
|
model.addAttribute("pageParam",pageParam);
|
|
|
|
List<Blog> topBlogList = blogService.getTopBlog();
|
|
model.addAttribute("topBlogList",topBlogList);
|
|
|
|
// 分类信息
|
|
List<BlogCategory> categoryList = blogCategoryService.list(null);
|
|
model.addAttribute("categoryList",categoryList);
|
|
|
|
return "blog/list";
|
|
}
|
|
|
|
@GetMapping("/blog/{page}/{limit}")
|
|
public String blogListPage(
|
|
@PathVariable int page,
|
|
@PathVariable int limit,
|
|
Model model){
|
|
|
|
if (page < 1){
|
|
page = 1;
|
|
}
|
|
Page<Blog> pageParam = new Page<>(page, limit);
|
|
blogService.page(pageParam,new QueryWrapper<Blog>().orderByDesc("sort").orderByDesc("gmt_create"));
|
|
|
|
// 结果
|
|
List<Blog> blogList = pageParam.getRecords();
|
|
model.addAttribute("blogList",blogList);
|
|
model.addAttribute("pageParam",pageParam);
|
|
|
|
List<Blog> topBlogList = blogService.getTopBlog();
|
|
model.addAttribute("topBlogList",topBlogList);
|
|
|
|
// 分类信息
|
|
List<BlogCategory> categoryList = blogCategoryService.list(null);
|
|
model.addAttribute("categoryList",categoryList);
|
|
|
|
return "blog/list";
|
|
}
|
|
|
|
// 写文章
|
|
@GetMapping("/blog/write")
|
|
public String toWrite(Model model){
|
|
List<BlogCategory> categoryList = blogCategoryService.list(null);
|
|
model.addAttribute("categoryList",categoryList);
|
|
return "blog/write";
|
|
}
|
|
|
|
@PostMapping("/blog/write")
|
|
public synchronized String write(QuestionWriteForm questionWriteForm){
|
|
// 构建问题对象
|
|
Blog blog = new Blog();
|
|
|
|
blog.setBid(QuinnUtils.getUuid());
|
|
blog.setTitle(questionWriteForm.getTitle());
|
|
blog.setContent(questionWriteForm.getContent());
|
|
blog.setSort(0);
|
|
blog.setViews(0);
|
|
|
|
blog.setAuthorId(questionWriteForm.getAuthorId());
|
|
blog.setAuthorName(questionWriteForm.getAuthorName());
|
|
blog.setAuthorAvatar(questionWriteForm.getAuthorAvatar());
|
|
|
|
BlogCategory category = blogCategoryService.getById(questionWriteForm.getCategoryId());
|
|
blog.setCategoryId(questionWriteForm.getCategoryId());
|
|
blog.setCategoryName(category.getCategory());
|
|
blog.setGmtCreate(QuinnUtils.getTime());
|
|
blog.setGmtUpdate(QuinnUtils.getTime());
|
|
// 存储对象
|
|
blogService.save(blog);
|
|
|
|
// 重定向到列表页面
|
|
return "redirect:/blog";
|
|
}
|
|
|
|
// 阅读文章
|
|
@GetMapping("/blog/read/{bid}")
|
|
public String read(HttpServletRequest request, @PathVariable("bid") String bid, Model model){
|
|
Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
|
|
blog.setViews(blog.getViews()+1);
|
|
blogService.updateById(blog);
|
|
model.addAttribute("blog",blog);
|
|
|
|
StarValue starValue = new StarValue();
|
|
starValue.setStar(false);
|
|
String loginUserId = getLoginUserId(request);
|
|
if (!StringUtils.isEmpty(loginUserId)){
|
|
List<BlogStar> stars = blogStarService.list(new QueryWrapper<BlogStar>().eq("user_id", loginUserId));
|
|
if (!CollectionUtils.isEmpty(stars)){
|
|
starValue.setStar(true);
|
|
}
|
|
}
|
|
model.addAttribute("starValue",starValue);
|
|
|
|
List<Comment> commentList = commentService.list(new QueryWrapper<Comment>().eq("topic_id", bid).orderByDesc("gmt_create"));
|
|
model.addAttribute("commentList",commentList);
|
|
return "blog/read";
|
|
}
|
|
|
|
// 收藏
|
|
@PostMapping("/blog/star")
|
|
public synchronized String toStar(BlogStarReq blogStarReq, Model model){
|
|
StarValue starValue = new StarValue();
|
|
starValue.setStar(false);
|
|
List<BlogStar> list = blogStarService.list(new QueryWrapper<BlogStar>()
|
|
.eq("user_id", blogStarReq.getUserId()).eq("topic_id", blogStarReq.getTopicId()));
|
|
if (!CollectionUtils.isEmpty(list)){
|
|
list.forEach(x->{
|
|
blogStarService.removeById(x);
|
|
});
|
|
starValue.setStar(false);
|
|
} else {
|
|
BlogStar blogStar = new BlogStar();
|
|
blogStar.setTopicId(blogStarReq.getTopicId());
|
|
blogStar.setUserId(blogStarReq.getUserId());
|
|
blogStar.setGmtCreate(QuinnUtils.getTime());
|
|
blogStarService.save(blogStar);
|
|
starValue.setStar(true);
|
|
}
|
|
model.addAttribute("starValue",starValue);
|
|
return "blog/read::star_refresh";
|
|
}
|
|
|
|
// 编辑问题
|
|
@GetMapping("/blog/editor/{uid}/{bid}")
|
|
public synchronized String toEditor(@PathVariable("uid") String uid,
|
|
@PathVariable("bid") String bid,Model model){
|
|
|
|
Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
|
|
|
|
if (!blog.getAuthorId().equals(uid)){
|
|
QuinnUtils.print("禁止非法编辑");
|
|
return "redirect:/blog";
|
|
}
|
|
|
|
model.addAttribute("blog",blog);
|
|
|
|
List<BlogCategory> categoryList = blogCategoryService.list(null);
|
|
model.addAttribute("categoryList",categoryList);
|
|
|
|
return "blog/editor";
|
|
}
|
|
|
|
@PostMapping("/blog/editor")
|
|
public String editor(Blog blog){
|
|
Blog queryBlog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", blog.getBid()));
|
|
|
|
queryBlog.setTitle(blog.getTitle());
|
|
queryBlog.setCategoryId(blog.getCategoryId());
|
|
queryBlog.setContent(blog.getContent());
|
|
queryBlog.setGmtUpdate(QuinnUtils.getTime());
|
|
|
|
blogService.updateById(queryBlog);
|
|
|
|
return "redirect:/blog/read/"+blog.getBid();
|
|
}
|
|
|
|
// 删除问题
|
|
@GetMapping("/blog/delete/{uid}/{bid}")
|
|
public String delete(@PathVariable("uid") String uid,
|
|
@PathVariable("bid") String bid){
|
|
|
|
Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
|
|
|
|
if (!blog.getAuthorId().equals(uid)){
|
|
QuinnUtils.print("禁止非法删除");
|
|
return "redirect:/blog";
|
|
}
|
|
|
|
blogService.removeById(blog);
|
|
// 重定向到列表页面
|
|
return "redirect:/blog";
|
|
}
|
|
|
|
// 评论
|
|
@PostMapping("/blog/comment/{bid}")
|
|
public String comment(@PathVariable("bid") String bid, Comment comment){
|
|
// 存储评论
|
|
comment.setCommentId(QuinnUtils.getUuid());
|
|
comment.setTopicCategory(1);
|
|
comment.setGmtCreate(QuinnUtils.getTime());
|
|
commentService.save(comment);
|
|
// 重定向到列表页面
|
|
return "redirect:/blog/read/"+bid;
|
|
}
|
|
|
|
}
|
|
|