feat: add tenant commands
This commit is contained in:
@@ -208,3 +208,44 @@ func (svc *Service) GetTenantIDBySlug(ctx context.Context, slug string) (int64,
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// CreateTenant
|
||||
func (svc *Service) CreateTenant(ctx context.Context, name, slug string) error {
|
||||
log := svc.log.WithField("method", "CreateTenant")
|
||||
|
||||
expireAt := time.Now().Add(time.Hour * 24 * 366)
|
||||
// 仅保留天数
|
||||
expireAt = time.Date(expireAt.Year(), expireAt.Month(), expireAt.Day(), 0, 0, 0, 0, expireAt.Location())
|
||||
|
||||
tbl := table.Tenants
|
||||
stmt := tbl.
|
||||
INSERT(tbl.Name, tbl.Slug, tbl.ExpireAt).
|
||||
VALUES(String(name), String(slug), TimestampT(expireAt)).
|
||||
ON_CONFLICT(tbl.Slug).
|
||||
DO_NOTHING()
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrapf(err, "create tenant: %s(%s)", name, slug)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetTenantExpireAtBySlug
|
||||
func (svc *Service) SetTenantExpireAtBySlug(ctx context.Context, slug string, expire time.Time) error {
|
||||
log := svc.log.WithField("method", "SetTenantExpireAtBySlug")
|
||||
|
||||
tbl := table.Tenants
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.ExpireAt).
|
||||
SET(TimestampT(expire)).
|
||||
WHERE(tbl.Slug.EQ(String(slug)))
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrapf(err, "renew tenant: %s expire at %s", slug, expire)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user