| @@ -1,6 +1,9 @@ | |||||
| package models | package models | ||||
| import "code.gitea.io/gitea/modules/timeutil" | |||||
| import ( | |||||
| "code.gitea.io/gitea/modules/timeutil" | |||||
| "xorm.io/builder" | |||||
| ) | |||||
| type LimitType string | type LimitType string | ||||
| @@ -57,10 +60,23 @@ type LimitConfigVO struct { | |||||
| RefreshRate string | RefreshRate string | ||||
| Scope string | Scope string | ||||
| LimitNum int64 | LimitNum int64 | ||||
| LimitCode string | |||||
| Creator string | Creator string | ||||
| CreatedUnix timeutil.TimeStamp | 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) { | func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { | ||||
| r := make([]LimitConfig, 0) | r := make([]LimitConfig, 0) | ||||
| err := x.Where(" limit_type = ?", limitType.Name()).Find(&r) | err := x.Where(" limit_type = ?", limitType.Name()).Find(&r) | ||||
| @@ -71,3 +87,36 @@ func GetLimitConfigByLimitType(limitType LimitType) ([]LimitConfig, error) { | |||||
| } | } | ||||
| return r, nil | 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 | |||||
| } | |||||
| @@ -60,7 +60,7 @@ type TaskConfigWithLimit struct { | |||||
| AwardAmount int64 `binding:"Required;MaxSize(256)"` | AwardAmount int64 `binding:"Required;MaxSize(256)"` | ||||
| Creator string | Creator string | ||||
| CreatedUnix timeutil.TimeStamp | CreatedUnix timeutil.TimeStamp | ||||
| Limiters []LimitConfigVO | |||||
| Limiters []*LimitConfigVO | |||||
| } | } | ||||
| func getTaskConfig(t *TaskConfig) (*TaskConfig, error) { | func getTaskConfig(t *TaskConfig) (*TaskConfig, error) { | ||||
| @@ -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()) | |||||
| } | |||||
| @@ -1318,6 +1318,8 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Group("/reward/point", func() { | m.Group("/reward/point", func() { | ||||
| m.Get("/account", point.GetPointAccount) | m.Get("/account", point.GetPointAccount) | ||||
| m.Get("/limiter/list", point.GetPointLimitConfigList) | |||||
| m.Post("/limiter/add", bindIgnErr(models.LimitConfigVO{}), point.AddPointLimitConfig) | |||||
| }, reqSignIn) | }, reqSignIn) | ||||
| m.Group("/task/config", func() { | m.Group("/task/config", func() { | ||||
| @@ -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 | |||||
| } | |||||
| @@ -72,18 +72,11 @@ func GetTaskConfigWithLimitList() ([]*models.TaskConfigWithLimit, error) { | |||||
| Creator: li.CreatorName, | Creator: li.CreatorName, | ||||
| CreatedUnix: li.CreatedUnix, | CreatedUnix: li.CreatedUnix, | ||||
| } | } | ||||
| lv := make([]models.LimitConfigVO, 0) | |||||
| lv := make([]*models.LimitConfigVO, 0) | |||||
| for j := 0; j < len(l); j++ { | for j := 0; j < len(l); j++ { | ||||
| lj := l[j] | lj := l[j] | ||||
| if lj.LimitCode == li.TaskCode { | 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 | t.Limiters = lv | ||||