From df7224103d656e1d9210777d03de2df41ccaebee Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Mon, 11 Jul 2022 15:14:57 +0800 Subject: [PATCH] #2225 add point limit config api --- models/limit_config.go | 7 +++++ routers/reward/point/limit.go | 13 ++++---- routers/routes/routes.go | 3 +- services/reward/limiter/config.go | 50 +++++++++++++++++++++++++++---- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/models/limit_config.go b/models/limit_config.go index 284edec6f..7bc350c06 100644 --- a/models/limit_config.go +++ b/models/limit_config.go @@ -66,6 +66,13 @@ type LimitConfig struct { DeletedAt timeutil.TimeStamp `xorm:"deleted"` } +type LimitConfigQueryOpts struct { + RefreshRate string + Scope LimitScope + LimitCode string + LimitType LimitType +} + type LimitConfigVO struct { ID int64 Tittle string diff --git a/routers/reward/point/limit.go b/routers/reward/point/limit.go index 6c5ec5827..184a244f9 100644 --- a/routers/reward/point/limit.go +++ b/routers/reward/point/limit.go @@ -8,23 +8,26 @@ import ( "net/http" ) -func GetPointLimitConfigList(ctx *context.Context) { - r, err := limiter.GetLimitConfigList(models.LimitTypeRewardPoint) +func GetSingleDailyPointLimitConfig(ctx *context.Context) { + r, err := limiter.GetSingleDailyPointLimitConfig() if err != nil { ctx.JSON(http.StatusOK, response.ServerError(err.Error())) return } - ctx.JSON(http.StatusOK, response.SuccessWithData(r)) + resultMap := make(map[string]int64, 0) + resultMap["LimitNum"] = r.LimitNum + ctx.JSON(http.StatusOK, response.SuccessWithData(resultMap)) } -func AddPointLimitConfig(ctx *context.Context, config models.LimitConfigVO) { - err := limiter.AddLimitConfig(&config, ctx.User, models.LimitTypeRewardPoint) +func SetSingleDailyPointLimitConfig(ctx *context.Context, config models.LimitConfigVO) { + err := limiter.SetSingleDailyPointLimitConfig(config.LimitNum, ctx.User) if err != nil { ctx.JSON(http.StatusOK, response.ServerError(err.Error())) return } ctx.JSON(http.StatusOK, response.Success()) } + func DeletePointLimitConfig(ctx *context.Context) { id := ctx.QueryInt64("id") err := limiter.DeleteLimitConfig(id, ctx.User) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 2fc56f7f0..e4b2f06ca 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -605,8 +605,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/config/recommend_org", bindIgnErr(operation.OrgInfos{}), operation.UpdateRecommendOrganizations) m.Group("/reward/point", func() { - m.Get("/limiter/list", point.GetPointLimitConfigList) - m.Post("/limiter/add", bindIgnErr(models.LimitConfigVO{}), point.AddPointLimitConfig) + m.Combo("/limiter/single-daily").Get(point.GetSingleDailyPointLimitConfig).Post(bindIgnErr(models.LimitConfigVO{}), point.SetSingleDailyPointLimitConfig) m.Post("/limiter/delete", point.DeletePointLimitConfig) m.Get("/account/search", point.SearchPointAccount) m.Post("/account/operate", binding.Bind(models.AdminRewardOperateReq{}), point.OperatePointAccountBalance) diff --git a/services/reward/limiter/config.go b/services/reward/limiter/config.go index 8de00d178..d90cf0aac 100644 --- a/services/reward/limiter/config.go +++ b/services/reward/limiter/config.go @@ -7,13 +7,53 @@ import ( "code.gitea.io/gitea/modules/redis/redis_key" ) -func GetLimitConfigList(limitType models.LimitType) ([]*models.LimitConfigVO, error) { - r, err := GetLimitersByLimitType(limitType) +func GetSingleDailyPointLimitConfig() (*models.LimitConfigVO, error) { + r, err := GetLimitConfigList(models.LimitConfigQueryOpts{ + RefreshRate: models.PeriodDaily, + Scope: models.LimitScopeSingleUser, + LimitCode: "", + LimitType: models.LimitTypeRewardPoint, + }) if err != nil { return nil, err } + if r == nil || len(r) == 0 { + return nil, nil + } + return r[0], nil +} + +func SetSingleDailyPointLimitConfig(limitNum int64, doer *models.User) error { + l := &models.LimitConfigVO{ + RefreshRate: models.PeriodDaily, + Scope: models.LimitScopeSingleUser.Name(), + LimitCode: "", + LimitType: models.LimitTypeRewardPoint.Name(), + LimitNum: limitNum, + } + return AddLimitConfig(l, doer) +} + +func GetLimitConfigList(opts models.LimitConfigQueryOpts) ([]*models.LimitConfigVO, error) { + r, err := GetLimitersByLimitType(opts.LimitType) + if err != nil { + log.Error("GetLimitConfigList error when getting limiters by limit type.err=%v", err) + return nil, err + } result := make([]*models.LimitConfigVO, 0) for _, v := range r { + if opts.LimitCode != "" && opts.LimitCode != v.LimitCode { + continue + } + if opts.Scope != "" && opts.Scope.Name() != v.Scope { + continue + } + if opts.RefreshRate != "" && opts.RefreshRate != v.RefreshRate { + continue + } + if opts.LimitType != "" && opts.LimitType.Name() != v.LimitType { + continue + } result = append(result, v.ToLimitConfigVO()) } return result, nil @@ -22,14 +62,14 @@ func GetLimitConfigById(id int64) (*models.LimitConfig, error) { return models.GetLimitConfigById(id) } -func AddLimitConfig(config *models.LimitConfigVO, doer *models.User, limitType models.LimitType) error { +func AddLimitConfig(config *models.LimitConfigVO, doer *models.User) error { r := &models.LimitConfig{ Tittle: config.Tittle, RefreshRate: config.RefreshRate, Scope: config.Scope, LimitNum: config.LimitNum, LimitCode: config.LimitCode, - LimitType: limitType.Name(), + LimitType: config.LimitType, CreatorId: doer.ID, CreatorName: doer.Name, } @@ -39,7 +79,7 @@ func AddLimitConfig(config *models.LimitConfigVO, doer *models.User, limitType m log.Error("add limit config error,config:%v err:%v", config, err) return err } - redis_client.Del(redis_key.LimitConfig(limitType.Name())) + redis_client.Del(redis_key.LimitConfig(config.LimitType)) return nil }