47 lines
885 B
Go
47 lines
885 B
Go
package fakers
|
|
|
|
import (
|
|
"github.com/bxcodec/faker/v3"
|
|
"github.com/google/uuid"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"math"
|
|
"math/rand"
|
|
"moretcgshop/app/models"
|
|
"time"
|
|
)
|
|
|
|
func ProductFaker(db *gorm.DB) *models.Product {
|
|
user := UserFaker(db)
|
|
err := db.Create(&user).Error
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
name := faker.Name()
|
|
return &models.Product{
|
|
ID: uuid.New().String(),
|
|
|
|
Name: name,
|
|
Price: decimal.NewFromFloat(fakePrice()),
|
|
|
|
ShortDescription: faker.Paragraph(),
|
|
Description: faker.Paragraph(),
|
|
|
|
CreatedAt: time.Time{},
|
|
UpdatedAt: time.Time{},
|
|
}
|
|
}
|
|
|
|
func fakePrice() float64 {
|
|
return precision(rand.Float64()*math.Pow10(rand.Intn(8)), rand.Intn(2)+1)
|
|
}
|
|
|
|
// precision | a helper function to set precision of price
|
|
func precision(val float64, pre int) float64 {
|
|
div := math.Pow10(pre)
|
|
return float64(int64(val*div)) / div
|
|
}
|