feat: update duration

This commit is contained in:
2025-12-16 16:12:55 +08:00
parent 0531a72ae6
commit 0e303e8a5c
6 changed files with 79 additions and 9 deletions

View File

@@ -8,9 +8,7 @@ import (
"quyun/v2/app/commands" "quyun/v2/app/commands"
"quyun/v2/app/errorx" "quyun/v2/app/errorx"
"quyun/v2/app/http/api"
"quyun/v2/app/http/super" "quyun/v2/app/http/super"
"quyun/v2/app/http/web"
"quyun/v2/app/jobs" "quyun/v2/app/jobs"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/app/tenancy" "quyun/v2/app/tenancy"
@@ -40,10 +38,6 @@ func defaultProviders() container.Providers {
jwt.DefaultProvider(), jwt.DefaultProvider(),
job.DefaultProvider(), job.DefaultProvider(),
database.DefaultProvider(), database.DefaultProvider(),
{Provider: services.Provide},
{Provider: api.Provide},
{Provider: super.Provide},
{Provider: web.Provide},
}...) }...)
} }
@@ -56,6 +50,10 @@ func Command() atom.Option {
defaultProviders(). defaultProviders().
With( With(
jobs.Provide, jobs.Provide,
services.Provide,
super.Provide,
// {Provider: api.Provide},
// {Provider: web.Provide},
), ),
), ),
) )

View File

@@ -1,6 +1,9 @@
package dto package dto
import ( import (
"errors"
"time"
"quyun/v2/app/requests" "quyun/v2/app/requests"
"quyun/v2/database/models" "quyun/v2/database/models"
) )
@@ -18,3 +21,16 @@ type TenantItem struct {
UserCount int64 UserCount int64
UserBalance int64 UserBalance int64
} }
type TenantExpireUpdateForm struct {
Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
}
// Duration
func (form *TenantExpireUpdateForm) ParseDuration() (time.Duration, error) {
duration := time.Duration(form.Duration) * 24 * time.Hour
if duration == 0 {
return 0, errors.New("invalid parsed duration")
}
return duration, nil
}

View File

@@ -5,13 +5,12 @@
package super package super
import ( import (
"quyun/v2/app/http/super/dto"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
_ "go.ipao.vip/atom" _ "go.ipao.vip/atom"
_ "go.ipao.vip/atom/contracts" _ "go.ipao.vip/atom/contracts"
. "go.ipao.vip/atom/fen" . "go.ipao.vip/atom/fen"
"quyun/v2/app/http/super/dto"
) )
// Routes implements the HttpRoute contract and provides route registration // Routes implements the HttpRoute contract and provides route registration
@@ -52,6 +51,12 @@ func (r *Routes) Register(router fiber.Router) {
r.tenant.list, r.tenant.list,
Query[dto.TenantFilter]("filter"), Query[dto.TenantFilter]("filter"),
)) ))
r.log.Debugf("Registering route: Patch /super/v1/tenants/:tenantID -> tenant.updateExpire")
router.Patch("/super/v1/tenants/:tenantID", Func2(
r.tenant.updateExpire,
PathParam[int64]("tenantID"),
Body[dto.TenantExpireUpdateForm]("form"),
))
r.log.Info("Successfully registered all routes") r.log.Info("Successfully registered all routes")
} }

View File

@@ -1,6 +1,7 @@
package super package super
import ( import (
"quyun/v2/app/errorx"
"quyun/v2/app/http/super/dto" "quyun/v2/app/http/super/dto"
"quyun/v2/app/requests" "quyun/v2/app/requests"
"quyun/v2/app/services" "quyun/v2/app/services"
@@ -17,3 +18,16 @@ type tenant struct{}
func (*tenant) list(ctx fiber.Ctx, filter *dto.TenantFilter) (*requests.Pager, error) { func (*tenant) list(ctx fiber.Ctx, filter *dto.TenantFilter) (*requests.Pager, error) {
return services.Tenant.Pager(ctx, filter) return services.Tenant.Pager(ctx, filter)
} }
// list
// @Router /super/v1/tenants/:tenantID [patch]
// @Bind tenantID path
// @Bind form body
func (*tenant) updateExpire(ctx fiber.Ctx, tenantID int64, form *dto.TenantExpireUpdateForm) error {
duration, err := form.ParseDuration()
if err != nil {
return errorx.Wrap(err).WithMsg("时间解析出错")
}
return services.Tenant.AddExpireDuration(ctx, tenantID, duration)
}

View File

@@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"time"
"quyun/v2/app/http/super/dto" "quyun/v2/app/http/super/dto"
"quyun/v2/app/requests" "quyun/v2/app/requests"
@@ -11,6 +12,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/sirupsen/logrus"
"go.ipao.vip/gen" "go.ipao.vip/gen"
) )
@@ -165,3 +167,30 @@ func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64
} }
return result, nil return result, nil
} }
// FindByID
func (t *tenant) FindByID(ctx context.Context, id int64) (*models.Tenant, error) {
tbl, query := models.TenantQuery.QueryContext(ctx)
m, err := query.Where(tbl.ID.Eq(id)).First()
if err != nil {
return nil, errors.Wrapf(err, "find by id failed, id: %d", id)
}
return m, nil
}
// AddExpireDuration
func (t *tenant) AddExpireDuration(ctx context.Context, tenantID int64, duration time.Duration) error {
logrus.WithField("tenant_id", tenantID).WithField("duration", duration).Info("add expire duration")
m, err := t.FindByID(ctx, tenantID)
if err != nil {
return err
}
if m.ExpiredAt.Before(time.Now()) {
m.ExpiredAt = time.Now().Add(duration)
} else {
m.ExpiredAt = m.ExpiredAt.Add(duration)
}
return m.Save(ctx)
}

View File

@@ -2,7 +2,7 @@
## Login ### Login
POST {{ host }}/super/v1/auth/login POST {{ host }}/super/v1/auth/login
Content-Type: application/json Content-Type: application/json
@@ -10,3 +10,11 @@ Content-Type: application/json
"username":"test-user", "username":"test-user",
"password":"test-password" "password":"test-password"
} }
### update tenant expire
PATCH {{ host }}/super/v1/tenants/2
Content-Type: application/json
{
"duration": 7
}