package cn.feast.coding.controller.links;

import javax.servlet.http.HttpServletRequest;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.links.Link;
import cn.feast.coding.service.links.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/link")
public class LinkController extends BaseController {
    @Autowired
    private LinkService linkService;

    @RequestMapping(value = "/list")
    public String list(Link link, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request) {
        PageInfo<Link> pageInfo = linkService.queryPage(link, pageNum(curPage), pageSize(request, pageSize));
        model.put("pages", pageInfo);
        model.put("link", link);
        return "links/list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "links/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Link link, ModelMap model) {
        link.setTimes(0);
        linkService.insert(link);
        model.put("flag", 1);
        return "links/add";
    }


    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Link link, ModelMap model) {
        link = linkService.selectByPrimaryKey(link.getId());
        model.put("link", link);
        return "links/edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Link link, HttpServletRequest request, ModelMap model) {
        linkService.update(link);
        model.put("flag", 1);
        return "links/edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(Link link) {
        linkService.deleteByPrimaryKey(link.getId());
    }

}
