chore: stabilize lint and verify builds
This commit is contained in:
@@ -2,11 +2,13 @@ package seed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands"
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/fields"
|
||||
"quyun/v2/database/models"
|
||||
@@ -45,8 +47,8 @@ type Service struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
||||
func Serve(_ *cobra.Command, _ []string) error {
|
||||
err := container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
||||
models.SetDefault(svc.DB)
|
||||
fmt.Println("Cleaning existing data...")
|
||||
|
||||
@@ -137,10 +139,14 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// 2. Tenant
|
||||
tenantCodeSuffix, err := randomIntString(1000)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate tenant code: %w", err)
|
||||
}
|
||||
tenant := &models.Tenant{
|
||||
UserID: creator.ID,
|
||||
Name: "梅派艺术工作室",
|
||||
Code: "meipai_" + cast.ToString(rand.Intn(1000)),
|
||||
Code: "meipai_" + tenantCodeSuffix,
|
||||
UUID: types.UUID(uuid.New()),
|
||||
Status: consts.TenantStatusVerified,
|
||||
}
|
||||
@@ -189,6 +195,15 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
price = 990
|
||||
} // 9.90
|
||||
|
||||
viewsValue, err := randomIntWithLimit(10000)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate views: %w", err)
|
||||
}
|
||||
likesValue, err := randomIntWithLimit(1000)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate likes: %w", err)
|
||||
}
|
||||
|
||||
c := &models.Content{
|
||||
TenantID: tenant.ID,
|
||||
UserID: creator.ID,
|
||||
@@ -197,8 +212,8 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
Genre: "京剧",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
Views: int32(rand.Intn(10000)),
|
||||
Likes: int32(rand.Intn(1000)),
|
||||
Views: int32(viewsValue),
|
||||
Likes: int32(likesValue),
|
||||
}
|
||||
if err := models.ContentQuery.WithContext(ctx).Create(c); err == nil {
|
||||
seededContents = append(seededContents, c)
|
||||
@@ -413,10 +428,14 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
DecidedOperatorUserID: creator.ID,
|
||||
DecidedReason: "符合要求",
|
||||
})
|
||||
inviteSuffix, err := randomIntWithLimit(100000)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate invite code: %w", err)
|
||||
}
|
||||
models.TenantInviteQuery.WithContext(ctx).Create(&models.TenantInvite{
|
||||
TenantID: tenant.ID,
|
||||
UserID: creator.ID,
|
||||
Code: "invite" + cast.ToString(rand.Intn(100000)),
|
||||
Code: "invite" + cast.ToString(inviteSuffix),
|
||||
Status: "active",
|
||||
MaxUses: 5,
|
||||
UsedCount: 0,
|
||||
@@ -459,6 +478,7 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
})
|
||||
|
||||
// 8. System config
|
||||
|
||||
models.SystemConfigQuery.WithContext(ctx).Create(&models.SystemConfig{
|
||||
ConfigKey: "site_name",
|
||||
Value: types.JSON([]byte(`{"value":"曲韵平台"}`)),
|
||||
@@ -577,6 +597,34 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
fmt.Println("Seed done.")
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errorx.ErrOperationFailed.WithCause(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func randomIntString(limitValue int64) (string, error) {
|
||||
value, err := randomIntWithLimit(limitValue)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return cast.ToString(value), nil
|
||||
}
|
||||
|
||||
func randomIntWithLimit(limitValue int64) (int64, error) {
|
||||
if limitValue <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
limit := big.NewInt(limitValue)
|
||||
value, err := rand.Int(rand.Reader, limit)
|
||||
if err != nil {
|
||||
return 0, errorx.ErrOperationFailed.WithCause(err)
|
||||
}
|
||||
|
||||
return value.Int64(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user