Browse Source

#2225

add point limit config api
tags/v1.22.9.2^2
chenyifan01 3 years ago
parent
commit
df7224103d
4 changed files with 61 additions and 12 deletions
  1. +7
    -0
      models/limit_config.go
  2. +8
    -5
      routers/reward/point/limit.go
  3. +1
    -2
      routers/routes/routes.go
  4. +45
    -5
      services/reward/limiter/config.go

+ 7
- 0
models/limit_config.go View File

@@ -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


+ 8
- 5
routers/reward/point/limit.go View File

@@ -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)


+ 1
- 2
routers/routes/routes.go View File

@@ -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)


+ 45
- 5
services/reward/limiter/config.go View File

@@ -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
}



Loading…
Cancel
Save