Example #1
0
func (c *MockContentController) Post() {
	var result ApiResult
	var content models.MockContent

	id, _ := c.GetInt("id")
	ruleId, _ := c.GetInt("ruleId")
	delaySeconds, _ := c.GetInt("delaySeconds")
	tag := c.GetString("tag")
	cont := c.GetString("content")

	o := orm.NewOrm()
	content.Id = id
	content.RuleId = ruleId
	content.DelaySeconds = delaySeconds
	content.Tag = tag
	content.Content = cont

	if content.Tag == "" {
		result = ApiResult{false, "Tag is required", nil}
		c.Data["json"] = &result
		c.ServeJSON()
		return
	}

	if content.RuleId <= 0 {
		result = ApiResult{false, "Please spefic a rule.", nil}
		c.Data["json"] = &result
		c.ServeJSON()
		return
	}

	created, _, err := o.ReadOrCreate(&content, "Id")
	if err != nil {
		result = ApiResult{false, err.Error(), err}
		c.Data["json"] = &result
		c.ServeJSON()
		return
	}

	if created {
		result = ApiResult{true, "", content}
	} else {
		content.RuleId = ruleId
		content.DelaySeconds = delaySeconds
		content.Tag = tag
		content.Content = cont
		_, err := o.Update(&content)
		if err != nil {
			result = ApiResult{false, err.Error(), nil}
			c.Data["json"] = &result
			c.ServeJSON()
			return
		}

		result = ApiResult{true, "", content}

	}

	refreshRuleCache()
	c.Data["json"] = &result
	c.ServeJSON()
}