package cn.ftebox.controller;

import cn.ftebox.entity.Blog;
import cn.ftebox.service.BlogService;
import cn.ftebox.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BlogController {
    @Resource
    private BlogService blogService;
    @Resource
    private CommentService commentService;

    @RequestMapping({"/article_datali.do"})
    public ModelAndView details(@RequestParam(value = "id", required = false)Integer id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        // 根据id查询博客信息
        Blog blog = blogService.findById(id);
        mav.addObject("blog",blog);
        // 点击量+1
        blog.setClickHit(blog.getClickHit()+1);
        blogService.update(blog);

        // 查询评论
        Map<String,Object> map = new HashMap<>();
        map.put("blogId",blog.getId());
        map.put("state",1);
        mav.addObject("commentList",commentService.list(map));
        return mav;
    }
}
