package com.wang.springboot.controller;
import com.wang.springboot.pojo.Type;
import com.wang.springboot.service.BlogService;
import com.wang.springboot.service.TagService;
import com.wang.springboot.service.TypeService;
import com.wang.springboot.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* Created by 王一宁 on 2017/10/23.
*/
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private BlogService blogService;
@Autowired
private TagService tagService;
/**/
@GetMapping("/types")
public String types(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
HttpServletRequest request,
Model model) {
// 1.点击分类,拿到的分类id
String id = request.getParameter("id");
long id2 = Long.parseLong(id);
// 2.把分类id放入到session
request.getSession().setAttribute("id", id);
// 3.拿到所有的分类
List<Type> types = typeService.listTypeTop(10000);
/*if (id == -1) {
id = types.get(0).getId();
}*/
// 4.封装查询对象
// 或者自己实现根据id查询所有就好。
BlogQuery blogQuery = new BlogQuery();
blogQuery.setTypeId(id2);
//5.放入前台页面的实体
model.addAttribute("types", types);
model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
return "list";
}
/*分页发送请求的地址*/
@GetMapping("/pages")
public String pages(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
HttpServletRequest request,
Model model) {
// 1.拿到所有的分类
List<Type> types = typeService.listTypeTop(10000);
// 2.取出session存放的分类id
String id2 = (String) request.getSession().getAttribute("id");
Long id = Long.parseLong(id2);
// 3.查询对象,BlogQuery封装分类的ID,根据id查询
// 或者自己实现根据id查询所有就好。
BlogQuery blogQuery = new BlogQuery();
blogQuery.setTypeId(id);
model.addAttribute("types", types);
model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
return "list";
}
}