Beispiel #1
0
func (widget WidgetController) ManyRemovePost() revel.Result {
	revel.TRACE.Printf("POST >> widget.many.remove ...")

	var relation, belong, many string
	var belong_id int64
	var manies []int64

	widget.Params.Bind(&relation, "relation")
	widget.Params.Bind(&belong, "belong")
	widget.Params.Bind(&many, "many")
	widget.Params.Bind(&belong_id, "belong_id")
	widget.Params.Bind(&manies, "manies")

	revel.INFO.Printf("POST >> widget.many.remove ... manies:%v", manies)

	obj := models.DefaultTableObject(relation)
	tx := db.Begin()
	if relation != many {
		conditon := fmt.Sprintf("%s_id = ? and %s_id = ?", symbol.Singular(many), symbol.Singular(belong))	
		for _, many_id := range manies {
			if err := tx.Where(conditon, many_id, belong_id).Delete(obj).Error; err != nil {
				tx.Rollback()
				return widget.RenderJson(WidgetResponse{Code: 401, Message: err.Error()})
			}
		}
	} else {
		if err := tx.Where(manies).Delete(obj).Error; err != nil {
			tx.Rollback()
			return widget.RenderJson(WidgetResponse{Code: 401, Message: err.Error()})
		}
	}
	tx.Commit()

	return widget.RenderJson(WidgetResponse{Code: 0, Message: "ok"})
}
Beispiel #2
0
func (widget WidgetController) ManyCreatePost() revel.Result {
	revel.TRACE.Printf("POST >> widget.many.create ...")

	var relation, belong, many string
	var belong_id int64
	var manies []int64

	widget.Params.Bind(&relation, "relation")
	widget.Params.Bind(&belong, "belong")
	widget.Params.Bind(&many, "many")
	widget.Params.Bind(&belong_id, "belong_id")
	widget.Params.Bind(&manies, "manies")

	revel.INFO.Printf("POST >> widget.many.create ... manies:%v", manies)
	tx := db.Begin()
	if relation != many {
		for _, many_id := range manies {
			sql := fmt.Sprintf("INSERT INTO %s(%s_id, %s_id) VALUES(?, ?)", 
				   relation, symbol.Singular(belong), symbol.Singular(many))
			if err := tx.Exec(sql, belong_id, many_id).Error; err != nil {
				tx.Rollback()
				return widget.RenderJson(WidgetResponse{Code: 401, Message: err.Error()})
			}
		}
	}
	tx.Commit()

	return widget.RenderJson(WidgetResponse{Code: 0, Message: "ok"})
}