From 1cdbc1cbb695b633a2e6f9c08f77df77c4f97e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Tue, 7 Jan 2025 15:47:29 +0100 Subject: [PATCH] Sections: prevent parent/child loop & force unqiue name/shortname --- controllers/sections.go | 49 ++++++++++++++++++++++++++++++++++------- views/section_form.html | 19 +++++++++------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/controllers/sections.go b/controllers/sections.go index 50362b6..2c857c6 100644 --- a/controllers/sections.go +++ b/controllers/sections.go @@ -69,6 +69,11 @@ func SectionAdd(c *fiber.Ctx) error { return err } + var sections []models.Section + db.Order("name collate nocase asc").Find( + §ions, "id <> ? AND parent_section_id = 0", section.ID, + ) + if c.Method() == "POST" { name := c.FormValue("name") shortName := c.FormValue("short_name") @@ -84,13 +89,32 @@ func SectionAdd(c *fiber.Ctx) error { errors = append(errors, "Le nom technique doit contentir entre 1 et 100 caractères") } + var checkName []models.Section + db.Find(&checkName, "name = ?", name) + + if len(checkName) > 0 { + errors = append(errors, "Le nom doit être unique") + } + + var checkShortName []models.Section + db.Find(&checkShortName, "short_name = ?", shortName) + + if len(checkShortName) > 0 { + errors = append(errors, "Le nom technique doit être unique") + } + section.Name = name section.ShortName = shortName section.ParentSectionID = 0 parentSectionID, err := strconv.ParseUint(parentSection, 10, 0) if err == nil { - section.ParentSectionID = uint(parentSectionID) + for _, parentSection := range sections { + if parentSection.ID == uint(parentSectionID) { + section.ParentSectionID = uint(parentSectionID) + break + } + } } section.ContainsMembers = false @@ -116,9 +140,6 @@ func SectionAdd(c *fiber.Ctx) error { } } - 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, @@ -146,11 +167,20 @@ func SectionEdit(c *fiber.Ctx) error { return result.Error } + var childSections []models.Section + db.Find(&childSections, "parent_section_id = ?", section.ID) + isParent := (len(childSections) > 0) + title := fmt.Sprintf( "%s | Modifier section", section.Name, ) + var sections []models.Section + db.Order("name collate nocase asc").Find( + §ions, "id <> ? AND parent_section_id = 0", section.ID, + ) + var errors []string if c.Method() == "POST" { name := c.FormValue("name") @@ -173,7 +203,12 @@ func SectionEdit(c *fiber.Ctx) error { section.ParentSectionID = 0 parentSectionID, err := strconv.ParseUint(parentSection, 10, 0) if err == nil { - section.ParentSectionID = uint(parentSectionID) + for _, parentSection := range sections { + if parentSection.ID == uint(parentSectionID) { + section.ParentSectionID = uint(parentSectionID) + break + } + } } section.ContainsMembers = false @@ -199,13 +234,11 @@ func SectionEdit(c *fiber.Ctx) error { } } - 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, + "IsParent": isParent, "Errors": errors, }) } diff --git a/views/section_form.html b/views/section_form.html index e493e5b..7c1bbe3 100644 --- a/views/section_form.html +++ b/views/section_form.html @@ -68,16 +68,19 @@ id="parent_section" class="form-control" name="parent_section" + {% if IsParent %}disabled{% endif %} > - {% for ParentSection in Sections %} - - {% endfor %} + {% if !IsParent %} + {% for ParentSection in Sections %} + + {% endfor %} + {% endif %}