diff --git a/models/host.go b/models/host.go index 1571221..559d7b4 100644 --- a/models/host.go +++ b/models/host.go @@ -33,6 +33,12 @@ func (host *Host) Delete(id int) (int64, error) { return Db.Id(id).Delete(host) } +func (host *Host) Find(id int) error { + _, err := Db.Id(id).Get(host) + + return err +} + func (host *Host) NameExists(name string) (bool, error) { count, err := Db.Where("name = ?", name).Count(host); diff --git a/routers/host/host.go b/routers/host/host.go index 18ff992..4148d16 100644 --- a/routers/host/host.go +++ b/routers/host/host.go @@ -21,7 +21,19 @@ func Index(ctx *macaron.Context) { func Create(ctx *macaron.Context) { ctx.Data["Title"] = "添加主机" - ctx.HTML(200, "host/create") + ctx.HTML(200, "host/host_form") +} + +func Edit(ctx *macaron.Context) { + ctx.Data["Title"] = "编辑主机" + hostModel := new(models.Host) + id := ctx.ParamsInt(":id") + err := hostModel.Find(id) + if err != nil { + logger.Errorf("获取主机详情失败#主机id-%d", id) + } + ctx.Data["Host"] = hostModel + ctx.HTML(200, "host/host_form") } type HostForm struct { diff --git a/routers/routers.go b/routers/routers.go index 6adaf1a..3ef62e1 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -36,7 +36,7 @@ func Register(m *macaron.Macaron) { // 50x错误 m.InternalServerError(func(ctx *macaron.Context) { if isGetRequest(ctx) && !isAjaxRequest(ctx) { - ctx.Data["Title"] = "500 - SERVER INTERNAL ERROR" + ctx.Data["Title"] = "500 - INTERNAL SERVER ERROR" ctx.HTML(500, "error/500") } else { json := utils.JsonResponse{} @@ -71,6 +71,7 @@ func Register(m *macaron.Macaron) { // 主机 m.Group("/host", func() { m.Get("/create", host.Create) + m.Get("/Edit", host.Edit) m.Post("/store", binding.Bind(host.HostForm{}), host.Store) m.Get("", host.Index) m.Post("/remove/:id", host.Remove) @@ -112,7 +113,6 @@ func RegisterMiddleware(m *macaron.Macaron) { // 设置模板共享变量 m.Use(func(ctx *macaron.Context) { ctx.Data["URI"] = ctx.Req.RequestURI - ctx.Data["StandardTimeFormat"] = "2006-01-02 15:03:04" }) } diff --git a/routers/task/task.go b/routers/task/task.go index c0d9f03..524e748 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -31,7 +31,7 @@ func Create(ctx *macaron.Context) { ctx.Data["Hosts"] = hosts ctx.Data["FirstHostName"] = hosts[0].Name ctx.Data["FirstHostId"] = hosts[0].Id - ctx.HTML(200, "task/create") + ctx.HTML(200, "task/task_form") } type TaskForm struct { @@ -88,13 +88,10 @@ func Store(ctx *macaron.Context, form TaskForm) string { // 删除任务 func Remove(ctx *macaron.Context) string { - id, err := strconv.Atoi(ctx.Params(":id")) + id := ctx.ParamsInt(":id") json := utils.JsonResponse{} - if err != nil { - return json.CommonFailure("参数错误", err) - } taskModel := new(models.Task) - _, err = taskModel.Delete(id) + _, err := taskModel.Delete(id) if err != nil { return json.CommonFailure(utils.FailureContent, err) } @@ -116,13 +113,10 @@ func Disable(ctx *macaron.Context) string { // 改变任务状态 func changeStatus(ctx *macaron.Context, status models.Status) string { - id, err := strconv.Atoi(ctx.Params(":id")) + id := ctx.ParamsInt(":id") json := utils.JsonResponse{} - if err != nil { - return json.CommonFailure("参数错误", err) - } taskModel := new(models.Task) - _, err = taskModel.Update(id, models.CommonMap{ + _, err := taskModel.Update(id, models.CommonMap{ "Status": status, }) if err != nil { diff --git a/templates/host/create.html b/templates/host/host_form.html similarity index 100% rename from templates/host/create.html rename to templates/host/host_form.html diff --git a/templates/task/create.html b/templates/task/task_form.html similarity index 100% rename from templates/task/create.html rename to templates/task/task_form.html