Method to fix fields order

This commit is contained in:
William Bouzourène 2025-03-24 15:30:15 +01:00
parent 03b1f92886
commit 9c585d6751
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
2 changed files with 54 additions and 0 deletions

View file

@ -101,3 +101,56 @@ func DebugFakeContacts(c *fiber.Ctx) error {
"Created %d contact(s)", number, "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 collate nocase 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 collate nocase asc",
).Find(
&contactFields, "person_type = ?", "contact",
)
if result2.Error != nil {
return result.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,
))
}

View file

@ -231,6 +231,7 @@ func main() {
if config.Debug { if config.Debug {
app.Get("/debug/fake-members/:number<int;min(1)>", controllers.DebugFakeMembers) app.Get("/debug/fake-members/:number<int;min(1)>", controllers.DebugFakeMembers)
app.Get("/debug/fake-contacts/:number<int;min(1)>", controllers.DebugFakeContacts) app.Get("/debug/fake-contacts/:number<int;min(1)>", controllers.DebugFakeContacts)
app.Get("/debug/fix-fields-order", controllers.DebugFixFieldsOrder)
} }
log.Info( log.Info(