|
|
@@ -1,6 +1,9 @@ |
|
|
|
package models |
|
|
|
|
|
|
|
import "code.gitea.io/gitea/modules/timeutil" |
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/modules/timeutil" |
|
|
|
"xorm.io/builder" |
|
|
|
) |
|
|
|
|
|
|
|
type LimitType string |
|
|
|
|
|
|
@@ -57,10 +60,23 @@ type LimitConfigVO struct { |
|
|
|
RefreshRate string |
|
|
|
Scope string |
|
|
|
LimitNum int64 |
|
|
|
LimitCode string |
|
|
|
Creator string |
|
|
|
CreatedUnix timeutil.TimeStamp |
|
|
|
} |
|
|
|
|
|
|
|
func (l *LimitConfig) ToLimitConfigVO() *LimitConfigVO { |
|
|
|
return &LimitConfigVO{ |
|
|
|
Tittle: l.Tittle, |
|
|
|
RefreshRate: l.RefreshRate, |
|
|
|
Scope: l.Scope, |
|
|
|
LimitNum: l.LimitNum, |
|
|
|
LimitCode: l.LimitCode, |
|
|
|
Creator: l.CreatorName, |
|
|
|
CreatedUnix: l.CreatedUnix, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { |
|
|
|
r := make([]LimitConfig, 0) |
|
|
|
err := x.Where(" limit_type = ?", limitType.Name()).Find(&r) |
|
|
@@ -71,3 +87,36 @@ func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { |
|
|
|
} |
|
|
|
return r, nil |
|
|
|
} |
|
|
|
|
|
|
|
func AddLimitConfig(l *LimitConfig) error { |
|
|
|
sess := x.NewSession() |
|
|
|
defer sess.Close() |
|
|
|
|
|
|
|
//delete old limit config |
|
|
|
cond := builder.NewCond() |
|
|
|
cond = cond.And(builder.Eq{"limit_type": l.LimitType}) |
|
|
|
cond = cond.And(builder.Eq{"scope": l.Scope}) |
|
|
|
if l.LimitCode == "" { |
|
|
|
subCond := builder.NewCond() |
|
|
|
subCond = subCond.Or(builder.IsNull{"limit_code"}) |
|
|
|
subCond = subCond.Or(builder.Eq{"limit_code": ""}) |
|
|
|
cond = cond.And(subCond) |
|
|
|
} else { |
|
|
|
cond = cond.And(builder.Eq{"limit_code": l.LimitCode}) |
|
|
|
} |
|
|
|
_, err := sess.Where(cond).Delete(&LimitConfig{}) |
|
|
|
if err != nil { |
|
|
|
sess.Rollback() |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//add new config |
|
|
|
_, err = sess.Insert(l) |
|
|
|
if err != nil { |
|
|
|
sess.Rollback() |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
sess.Commit() |
|
|
|
return nil |
|
|
|
} |