Esempio n. 1
0
func (c *MockContentController) Enable() {
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	o := orm.NewOrm()

	var content models.MockContent
	content.Id = id
	err := o.Read(&content)
	if err != nil {
		c.ShowError(err.Error())
		return
	}

	o.QueryTable("mock_content").Filter("rule_id", content.RuleId).Update(orm.Params{"enable": "0"})
	content.Enable = true
	_, err = o.Update(&content)
	if err != nil {
		c.ShowError(err.Error())
		return
	}

	refreshRuleCache()
	c.ShowSuccess(nil)
}
Esempio n. 2
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()
}