Implement lib to generate fake data for testing
This commit is contained in:
parent
801efeac7e
commit
e05fba69e7
4 changed files with 111 additions and 0 deletions
103
controllers/debug.go
Normal file
103
controllers/debug.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
|
||||
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
||||
"git.readonly.ch/bouzoure/pop-camarades/models"
|
||||
"github.com/brianvoe/gofakeit/v7"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func DebugFakeMembers(c *fiber.Ctx) error {
|
||||
number, _ := c.ParamsInt("number")
|
||||
|
||||
db, err := helpers.GetDatabase()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var sections []models.Section
|
||||
result := db.Find(§ions, "contains_members = ?", true)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected < 1 {
|
||||
return fiber.NewError(400, "cannot create members because no sections")
|
||||
}
|
||||
|
||||
for i := 0; i < number; i++ {
|
||||
section := sections[rand.IntN(len(sections))]
|
||||
address := gofakeit.Address()
|
||||
address2 := fmt.Sprintf("C/O %s", gofakeit.Name())
|
||||
|
||||
person := models.Person{
|
||||
IsMember: true,
|
||||
FirstName: gofakeit.FirstName(),
|
||||
LastName: gofakeit.LastName(),
|
||||
Email: gofakeit.Email(),
|
||||
Phone: gofakeit.Phone(),
|
||||
Mobile: gofakeit.Phone(),
|
||||
Address1: address.Street,
|
||||
Address2: address2,
|
||||
PostalCode: address.Zip[:4],
|
||||
City: address.City,
|
||||
SectionID: section.ID,
|
||||
}
|
||||
|
||||
db.Create(&person)
|
||||
}
|
||||
|
||||
return c.SendString(fmt.Sprintf(
|
||||
"Created %d member(s)", number,
|
||||
))
|
||||
}
|
||||
|
||||
func DebugFakeContacts(c *fiber.Ctx) error {
|
||||
number, _ := c.ParamsInt("number")
|
||||
|
||||
db, err := helpers.GetDatabase()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var sections []models.Section
|
||||
result := db.Find(§ions, "contains_contacts = ?", true)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected < 1 {
|
||||
return fiber.NewError(400, "cannot create contacts because no sections")
|
||||
}
|
||||
|
||||
for i := 0; i < number; i++ {
|
||||
section := sections[rand.IntN(len(sections))]
|
||||
address := gofakeit.Address()
|
||||
address2 := fmt.Sprintf("C/O %s", gofakeit.Name())
|
||||
|
||||
person := models.Person{
|
||||
IsContact: true,
|
||||
FirstName: gofakeit.FirstName(),
|
||||
LastName: gofakeit.LastName(),
|
||||
Email: gofakeit.Email(),
|
||||
Phone: gofakeit.Phone(),
|
||||
Mobile: gofakeit.Phone(),
|
||||
Address1: address.Street,
|
||||
Address2: address2,
|
||||
PostalCode: address.Zip[:4],
|
||||
City: address.City,
|
||||
SectionID: section.ID,
|
||||
}
|
||||
|
||||
db.Create(&person)
|
||||
}
|
||||
|
||||
return c.SendString(fmt.Sprintf(
|
||||
"Created %d contact(s)", number,
|
||||
))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue