Exemple #1
0
// @Title delete
// @Description delete
// @Param	id	path string	true "the objectid you want to get"
// @Success 200
// @Failure 403 body is empty
// @router /delete/:id([0-9]+) [delete]
func (this *ManageController) Delete() {
	// convert the string value to an int
	articleId, _ := strconv.Atoi(this.Ctx.Input.Param(":id"))

	o := orm.NewOrm()
	o.Using("default")

	article := models.Article{}

	// Check if the article exists first
	if exist := o.QueryTable(article.TableName()).Filter("Id", articleId).Exist(); exist {
		if _, err := o.Delete(&models.Article{Id: articleId}); err == nil {
			this.Data["json"] = "Record Deleted. "
		} else {
			this.Data["json"] = "Record couldn't be deleted. Reason: " + err.Error()
		}
	} else {
		this.Data["json"] = "Record Doesn't exist."
	}

	this.ServeJSON()
}
Exemple #2
0
// @Title update
// @Description update
// @Param	id	path string	true "the objectid you want to get"
// @Success 200
// @Failure 403 body is empty
// @router /update/:id([0-9]+) [put]
func (this *ManageController) Update() {
	o := orm.NewOrm()
	o.Using("default")

	// convert the string value to an int
	if articleId, err := strconv.Atoi(this.Ctx.Input.Param(":id")); err == nil {
		article := models.Article{Id: articleId}
		// attempt to load the record from the database
		if o.Read(&article) == nil {
			// set the Client and Url properties to arbitrary values
			article.Client = "Sitepoint"
			article.Url = "http://www.google.com"
			if _, err := o.Update(&article); err == nil {
				this.Data["json"] = "Record Was Updated."
			}
		} else {
			this.Data["json"] = "Record Was NOT Updated. Couldn't find article matching id"
		}
	} else {
		this.Data["json"] = "Record Was NOT Updated. Couldn't convert id from a string to a number. " + err.Error()
	}

	this.ServeJSON()
}