From f7c8fa4539f30b4bb50aadb16ea37bbcdf91458a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Wed, 1 Jan 2025 16:15:47 +0100 Subject: [PATCH] Gestion des sections --- controllers/sections.go | 227 ++++++++++++++++++++++++++++++++++++++++ main.go | 9 ++ views/section.html | 64 +++++++++++ views/section_form.html | 119 +++++++++++++++++++++ views/sections.html | 79 ++++++++++++++ 5 files changed, 498 insertions(+) create mode 100644 controllers/sections.go create mode 100644 views/section.html create mode 100644 views/section_form.html create mode 100644 views/sections.html diff --git a/controllers/sections.go b/controllers/sections.go new file mode 100644 index 0000000..577e382 --- /dev/null +++ b/controllers/sections.go @@ -0,0 +1,227 @@ +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.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 err + } + + 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.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 err + } + + 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.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 err + } + + return c.Redirect("/admin/sections") +} diff --git a/main.go b/main.go index 555efb2..3f38826 100644 --- a/main.go +++ b/main.go @@ -112,6 +112,15 @@ func main() { app.Get("/totp/verify", controllers.TotpVerifyPage) app.Post("/totp/verify", controllers.TotpVerifyPage) + // Admin: Sections + app.Get("/admin/sections", controllers.Sections) + app.Get("/admin/sections/:id", controllers.SectionShow) + app.Get("/admin/sections/add", controllers.SectionAdd) + app.Post("/admin/sections/add", controllers.SectionAdd) + app.Get("/admin/sections/:id/edit", controllers.SectionEdit) + app.Post("/admin/sections/:id/edit", controllers.SectionEdit) + app.Post("/admin/sections/:id/delete", controllers.SectionDelete) + // Admin: Lists app.Get("/admin/lists", controllers.Lists) app.Get("/admin/lists/:id", controllers.ListShow) diff --git a/views/section.html b/views/section.html new file mode 100644 index 0000000..5a8ae63 --- /dev/null +++ b/views/section.html @@ -0,0 +1,64 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ +
+ Nom
+ {{ Section.Name }} +
+ +
+ Nom technique
+ {{ Section.ShortName }} +
+ +
+ Section parente
+ {% if Section.ParentSectionID %} + {{ Section.ParentSection.Name }} + {% else %} + N/A + {% endif %} +
+ +
+ Contient des membres
+ {% if Section.ContainsMembers %}Oui{% else %}Non{% endif %} +
+ +
+ Contient des contacts
+ {% if Section.ContainsContacts %}Oui{% else %}Non{% endif %} +
+ +
+ + + Modifier + +
+ +
+
+ +
+{% endblock %} diff --git a/views/section_form.html b/views/section_form.html new file mode 100644 index 0000000..d77ad47 --- /dev/null +++ b/views/section_form.html @@ -0,0 +1,119 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ + {% if Errors %} +
+
    + {% for Error in Errors %} +
  • {{ Error }}
  • + {% endfor %} +
+
+ {% endif %} + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ +
+{% endblock %} diff --git a/views/sections.html b/views/sections.html new file mode 100644 index 0000000..6e5adf9 --- /dev/null +++ b/views/sections.html @@ -0,0 +1,79 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ + {% if Sections %} +
+ + + + + + + + + + + + {% for Section in Sections %} + + + + + + + + {% endfor %} + +
NomNom techniqueSection parenteContient membresContient contacts
+ + {{ Section.Name }} + + + {{ Section.ShortName }} + + {% if Section.ParentSectionID %} + {{ Section.ParentSection.Name }} + {% else %} + N/A + {% endif %} + + {% if Section.ContainsMembers %} + Oui + {% else %} + Non + {% endif %} + + {% if Section.ContainsContacts %} + Oui + {% else %} + Non + {% endif %} +
+
+ {% else %} +
+ Pas de section pour le moment +
+ {% endif %} + + + +
+{% endblock %}