Beispiel #1
0
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()
}