pop-camarades/controllers/debug.go

156 lines
3.1 KiB
Go

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(&sections, "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(&sections, "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,
))
}
func DebugFixFieldsOrder(c *fiber.Ctx) error {
db, err := helpers.GetDatabase()
if err != nil {
return err
}
var memberFields []models.Field
result := db.Order(
"name asc",
).Find(
&memberFields, "person_type = ?", "member",
)
if result.Error != nil {
return result.Error
}
if result.RowsAffected > 0 {
position := 1
for _, field := range memberFields {
field.Position = position
db.Save(&field)
position++
}
}
var contactFields []models.Field
result2 := db.Order(
"name asc",
).Find(
&contactFields, "person_type = ?", "contact",
)
if result2.Error != nil {
return result2.Error
}
if result2.RowsAffected > 0 {
position := 1
for _, field := range contactFields {
field.Position = position
db.Save(&field)
position++
}
}
return c.SendString(fmt.Sprintf(
"Fixed fields -> members: %d, contacts: %d",
result.RowsAffected,
result2.RowsAffected,
))
}