package controllers import ( "errors" "fmt" "strconv" "git.readonly.ch/bouzoure/popvaud-people/helpers" "git.readonly.ch/bouzoure/popvaud-people/models" "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) func Sections(c *fiber.Ctx) error { db, err := helpers.GetDatabase() if err != nil { return err } var sections []models.Section result := db.Order("name collate nocase asc").Preload("ParentSection").Find(§ions) if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { return err } return c.Render("sections", fiber.Map{ "PageTitle": "Sections", "Sections": sections, }) } func SectionShow(c *fiber.Ctx) error { id := c.Params("id") db, err := helpers.GetDatabase() if err != nil { return err } var section models.Section result := db.Preload("ParentSection").Find(§ion, "id = ?", id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, "Not found") } if result.Error != nil { return result.Error } title := fmt.Sprintf( "%s | Sections", section.Name, ) return c.Render("section", fiber.Map{ "PageTitle": title, "Section": section, }) } func SectionAdd(c *fiber.Ctx) error { var section models.Section var errors []string db, err := helpers.GetDatabase() if err != nil { return err } if c.Method() == "POST" { name := c.FormValue("name") shortName := c.FormValue("short_name") parentSection := c.FormValue("parent_section") containsMembers := c.FormValue("contains_members") containsContacts := c.FormValue("contains_contacts") if len(name) > 100 || len(name) < 1 { errors = append(errors, "Le nom doit contentir entre 1 et 100 caractères") } if len(shortName) > 100 || len(shortName) < 1 { errors = append(errors, "Le nom technique doit contentir entre 1 et 100 caractères") } section.Name = name section.ShortName = shortName section.ParentSectionID = 0 parentSectionID, err := strconv.ParseUint(parentSection, 10, 0) if err == nil { section.ParentSectionID = uint(parentSectionID) } section.ContainsMembers = false if containsMembers == "on" { section.ContainsMembers = true } section.ContainsContacts = false if containsContacts == "on" { section.ContainsContacts = true } if len(errors) == 0 { result := db.Create(§ion) if result.Error != nil { return result.Error } else { c.Redirect(fmt.Sprintf( "/admin/sections/%d", section.ID, )) } } } var sections []models.Section db.Order("name collate nocase asc").Find(§ions) return c.Render("section_form", fiber.Map{ "PageTitle": "Ajouter une section", "Section": section, "Sections": sections, "Errors": errors, }) } func SectionEdit(c *fiber.Ctx) error { id := c.Params("id") db, err := helpers.GetDatabase() if err != nil { return err } var section models.Section result := db.Find(§ion, "id = ?", id) if errors.Is(result.Error, gorm.ErrRecordNotFound) { return fiber.NewError(fiber.StatusNotFound, "Not found") } if result.Error != nil { return result.Error } title := fmt.Sprintf( "%s | Modifier section", section.Name, ) var errors []string if c.Method() == "POST" { name := c.FormValue("name") shortName := c.FormValue("short_name") parentSection := c.FormValue("parent_section") containsMembers := c.FormValue("contains_members") containsContacts := c.FormValue("contains_contacts") if len(name) > 100 || len(name) < 1 { errors = append(errors, "Le nom doit contentir entre 1 et 100 caractères") } if len(shortName) > 100 || len(shortName) < 1 { errors = append(errors, "Le nom technique doit contentir entre 1 et 100 caractères") } section.Name = name section.ShortName = shortName section.ParentSectionID = 0 parentSectionID, err := strconv.ParseUint(parentSection, 10, 0) if err == nil { section.ParentSectionID = uint(parentSectionID) } section.ContainsMembers = false if containsMembers == "on" { section.ContainsMembers = true } section.ContainsContacts = false if containsContacts == "on" { section.ContainsContacts = true } if len(errors) == 0 { result := db.Save(§ion) if result.Error != nil { return result.Error } else { c.Redirect(fmt.Sprintf( "/admin/sections/%d", section.ID, )) } } } var sections []models.Section db.Order("name collate nocase asc").Find(§ions, "id <> ?", section.ID) return c.Render("section_form", fiber.Map{ "PageTitle": title, "Section": section, "Sections": sections, "Errors": errors, }) } func SectionDelete(c *fiber.Ctx) error { id := c.Params("id") db, err := helpers.GetDatabase() if err != nil { return err } result := db.Delete(&models.Section{}, id) if result.Error != nil { return result.Error } return c.Redirect("/admin/sections") }