package cn.feast.coding.controller.generator;


import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.generator.Table;
import cn.feast.coding.service.generator.TableService;
import com.github.pagehelper.PageInfo;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/table")
public class TableController extends BaseController {
    @Autowired
    private TableService tableService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Table table, Integer curPage, Integer pageSize,  ModelMap model, HttpServletRequest request) {
        PageInfo<Table> pageInfo = tableService.queryPage(table, pageNum(curPage), pageSize(request, pageSize));
        model.put("pages", pageInfo);
        model.put("table", table);
        return "table/table_list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Table table, HttpServletRequest request, ModelMap model) {
        tableService.save(table);
        model.put("flag", 1);
        return "table/table_add";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Table table, ModelMap model) {
        table = tableService.queryById(table.getId());
        model.put("table", table);
        return "table/table_edit";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Table table, HttpServletRequest request, ModelMap model) {
        tableService.update(table);
        model.put("flag", 1);
        return "table/table_edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void delete(Table table) {
        tableService.deleteById(table.getId());
    }
}
