diff --git a/models/limit_config.go b/models/limit_config.go index 154c13ed8..62ff3bfbe 100644 --- a/models/limit_config.go +++ b/models/limit_config.go @@ -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 +} diff --git a/models/task_config.go b/models/task_config.go index 44a3bea32..922273c46 100644 --- a/models/task_config.go +++ b/models/task_config.go @@ -60,7 +60,7 @@ type TaskConfigWithLimit struct { AwardAmount int64 `binding:"Required;MaxSize(256)"` Creator string CreatedUnix timeutil.TimeStamp - Limiters []LimitConfigVO + Limiters []*LimitConfigVO } func getTaskConfig(t *TaskConfig) (*TaskConfig, error) { diff --git a/routers/reward/point/limit.go b/routers/reward/point/limit.go new file mode 100644 index 000000000..a831169f8 --- /dev/null +++ b/routers/reward/point/limit.go @@ -0,0 +1,27 @@ +package point + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/routers/response" + "code.gitea.io/gitea/services/reward/limiter" + "net/http" +) + +func GetPointLimitConfigList(ctx *context.Context) { + r, err := limiter.GetLimitConfigList(models.LimitTypeRewardPoint) + if err != nil { + ctx.JSON(http.StatusOK, response.ServerError(err.Error())) + return + } + ctx.JSON(http.StatusOK, response.SuccessWithData(r)) +} + +func AddPointLimitConfig(ctx *context.Context, config models.LimitConfigVO) { + err := limiter.AddLimitConfig(&config, ctx.User, models.LimitTypeRewardPoint) + if err != nil { + ctx.JSON(http.StatusOK, response.ServerError(err.Error())) + return + } + ctx.JSON(http.StatusOK, response.Success()) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 135f4d702..89416ba16 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -1318,6 +1318,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/reward/point", func() { m.Get("/account", point.GetPointAccount) + m.Get("/limiter/list", point.GetPointLimitConfigList) + m.Post("/limiter/add", bindIgnErr(models.LimitConfigVO{}), point.AddPointLimitConfig) }, reqSignIn) m.Group("/task/config", func() { diff --git a/services/reward/limiter/config.go b/services/reward/limiter/config.go new file mode 100644 index 000000000..12204b2c5 --- /dev/null +++ b/services/reward/limiter/config.go @@ -0,0 +1,41 @@ +package limiter + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/redis/redis_client" + "code.gitea.io/gitea/modules/redis/redis_key" +) + +func GetLimitConfigList(limitType models.LimitType) ([]*models.LimitConfigVO, error) { + r, err := GetLimitersByLimitType(limitType) + if err != nil { + return nil, err + } + result := make([]*models.LimitConfigVO, 0) + for _, v := range r { + result = append(result, v.ToLimitConfigVO()) + } + return result, nil +} + +func AddLimitConfig(config *models.LimitConfigVO, doer *models.User, limitType models.LimitType) error { + r := &models.LimitConfig{ + Tittle: config.Tittle, + RefreshRate: config.RefreshRate, + Scope: config.Scope, + LimitNum: config.LimitNum, + LimitCode: config.LimitCode, + LimitType: limitType.Name(), + CreatorId: doer.ID, + CreatorName: doer.Name, + } + err := models.AddLimitConfig(r) + + if err != nil { + log.Error("add limit config error,config:%v err:%v", config, err) + return err + } + redis_client.Del(redis_key.LimitConfig(limitType)) + return nil +} diff --git a/services/task/task_config.go b/services/task/task_config.go index fe50647e3..0001edc21 100644 --- a/services/task/task_config.go +++ b/services/task/task_config.go @@ -72,18 +72,11 @@ func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { Creator: li.CreatorName, CreatedUnix: li.CreatedUnix, } - lv := make([]models.LimitConfigVO, 0) + lv := make([]*models.LimitConfigVO, 0) for j := 0; j < len(l); j++ { lj := l[j] if lj.LimitCode == li.TaskCode { - lv = append(lv, models.LimitConfigVO{ - Tittle: lj.Tittle, - RefreshRate: lj.RefreshRate, - Scope: lj.Scope, - LimitNum: lj.LimitNum, - Creator: lj.CreatorName, - CreatedUnix: lj.CreatedUnix, - }) + lv = append(lv, lj.ToLimitConfigVO()) } } t.Limiters = lv