feat: update duration
This commit is contained in:
@@ -8,9 +8,7 @@ import (
|
||||
|
||||
"quyun/v2/app/commands"
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/api"
|
||||
"quyun/v2/app/http/super"
|
||||
"quyun/v2/app/http/web"
|
||||
"quyun/v2/app/jobs"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/app/tenancy"
|
||||
@@ -40,10 +38,6 @@ func defaultProviders() container.Providers {
|
||||
jwt.DefaultProvider(),
|
||||
job.DefaultProvider(),
|
||||
database.DefaultProvider(),
|
||||
{Provider: services.Provide},
|
||||
{Provider: api.Provide},
|
||||
{Provider: super.Provide},
|
||||
{Provider: web.Provide},
|
||||
}...)
|
||||
}
|
||||
|
||||
@@ -56,6 +50,10 @@ func Command() atom.Option {
|
||||
defaultProviders().
|
||||
With(
|
||||
jobs.Provide,
|
||||
services.Provide,
|
||||
super.Provide,
|
||||
// {Provider: api.Provide},
|
||||
// {Provider: web.Provide},
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
)
|
||||
@@ -18,3 +21,16 @@ type TenantItem struct {
|
||||
UserCount 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
|
||||
}
|
||||
|
||||
@@ -5,13 +5,12 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/super/dto"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
. "go.ipao.vip/atom/fen"
|
||||
"quyun/v2/app/http/super/dto"
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
@@ -52,6 +51,12 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
r.tenant.list,
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
@@ -17,3 +18,16 @@ type tenant struct{}
|
||||
func (*tenant) list(ctx fiber.Ctx, filter *dto.TenantFilter) (*requests.Pager, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
@@ -165,3 +167,30 @@ func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,11 +2,19 @@
|
||||
|
||||
|
||||
|
||||
## Login
|
||||
### Login
|
||||
POST {{ host }}/super/v1/auth/login
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username":"test-user",
|
||||
"password":"test-password"
|
||||
}
|
||||
|
||||
### update tenant expire
|
||||
PATCH {{ host }}/super/v1/tenants/2
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"duration": 7
|
||||
}
|
||||
Reference in New Issue
Block a user