func (c *ProjectController) Post() { var result ApiResult var project models.Project id, _ := c.GetInt("id") name := c.GetString("name") description := c.GetString("description") if name == "" { result = ApiResult{false, "Name is required", nil} c.Data["json"] = &result c.ServeJSON() return } o := orm.NewOrm() project.Id = id project.Name = name project.Description = description created, _, err := o.ReadOrCreate(&project, "Id") if err != nil { result = ApiResult{false, err.Error(), err} c.Data["json"] = &result c.ServeJSON() return } if created { result = ApiResult{true, "", project} } else { project.Name = name project.Description = description _, err := o.Update(&project) if err != nil { result = ApiResult{false, err.Error(), nil} c.Data["json"] = &result c.ServeJSON() return } result = ApiResult{true, "", project} } c.Data["json"] = &result c.ServeJSON() }
func (c *ProjectController) Get() { var result ApiResult var project models.Project id, _ := strconv.Atoi(c.Ctx.Input.Param(":id")) o := orm.NewOrm() project.Id = id err := o.Read(&project) if err == orm.ErrNoRows || err == orm.ErrMissPK { result = ApiResult{false, "Can't find data.", nil} } else { result = ApiResult{true, "", project} } c.Data["json"] = &result c.ServeJSON() }
func (c *ProjectController) Toggle() { var project models.Project id, _ := strconv.Atoi(c.Ctx.Input.Param(":id")) project.Id = id o := orm.NewOrm() err := o.Read(&project) if err == orm.ErrNoRows || err == orm.ErrMissPK { c.ShowError("Can't find data.") return } project.Enable = !project.Enable _, err = o.Update(&project) if err == orm.ErrNoRows || err == orm.ErrMissPK { c.ShowError(err.Error()) return } refreshRuleCache() c.ShowSuccess(nil) }