refactor quota settings

This commit is contained in:
woodsaj
2015-09-11 01:47:33 +08:00
parent 555cbeffa5
commit 852f9bd277
9 changed files with 59 additions and 43 deletions

View File

@ -256,7 +256,7 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) {
func LimitQuota(target m.QuotaTarget) macaron.Handler {
return func(c *Context) {
limitReached, err := m.QuotaReached(c.OrgId, target)
limitReached, err := QuotaReached(c.OrgId, target)
if err != nil {
c.JsonApiErr(500, "failed to get quota", err)
return
@ -267,3 +267,20 @@ func LimitQuota(target m.QuotaTarget) macaron.Handler {
}
}
}
func QuotaReached(org_id int64, target m.QuotaTarget) (bool, error) {
if !setting.Quota.Enabled {
return false, nil
}
if !target.IsValid() {
return true, m.ErrInvalidQuotaTarget
}
query := m.GetQuotaByTargetQuery{OrgId: org_id, Target: target}
if err := bus.Dispatch(&query); err != nil {
return true, err
}
if query.Result.Used >= query.Result.Limit {
return true, nil
}
return false, nil
}