package api import ( "database/sql" "fmt" "log" "math/rand" "github.com/brianvoe/gofakeit/v6" ) func fakeData() { tx, err := db.Begin() if err != nil { log.Fatal(err) } fakeUsers(tx) fakeSupplier(tx) fakeMarket(tx) tx.Commit() } func fakeUsers(tx *sql.Tx) { for i := 0; i < 10; i++ { username := gofakeit.Username() password := gofakeit.Password(true, true, true, true, true, 1) encryptedPassword := EncryptPassword(password) _, err := tx.Exec(`insert into users (username, password) values ($1, $2)`, username, encryptedPassword) if err != nil { tx.Rollback() log.Fatal(err) } log.Println("fake users", username, password) } } func fakeSupplier(tx *sql.Tx) { for i := 0; i < 10; i++ { username := gofakeit.Username() password := EncryptPassword(gofakeit.Password(true, true, true, true, true, 1)) _, err := tx.Exec(`insert into users (username, password, role) values ($1, $2, 2)`, username, password) if err != nil { tx.Rollback() log.Fatal(err) } log.Println("fake supplier", username, password) } } func fakeMarket(tx *sql.Tx) { for i := 0; i < 10; i++ { addr := gofakeit.Address() name := addr.State description := addr.Address location := fmt.Sprintf("(%f, %f)", addr.Latitude, addr.Longitude) row := tx.QueryRow(`insert into market (name, description, location) values ($1, $2, $3) returning id`, name, description, location) var mid int64 err := row.Scan(&mid) if err != nil { tx.Rollback() log.Fatal(err) } log.Println("fake market", name, description, location) fakeProduct(tx, int64(mid)) } } func fakeProduct(tx *sql.Tx, mid int64) { for i := 0; i < 10; i++ { name := gofakeit.BeerName() description := gofakeit.BeerStyle() quantity := rand.Intn(390) price := gofakeit.Price(39, 390) _, err := tx.Exec(`insert into goods (name, description, supplier_id, market_id, quantity, price) values ($1, $2, $3, $4, $5, $6)`, name, description, 1, mid, quantity, price) if err != nil { tx.Rollback() log.Fatal(err) } log.Println("fake goods", name, description, quantity, price) } }