Sections: prevent parent/child loop & force unqiue name/shortname

This commit is contained in:
William Bouzourène 2025-01-07 15:47:29 +01:00
parent 360ed9acd4
commit 1cdbc1cbb6
2 changed files with 52 additions and 16 deletions

View file

@ -69,6 +69,11 @@ func SectionAdd(c *fiber.Ctx) error {
return err
}
var sections []models.Section
db.Order("name collate nocase asc").Find(
&sections, "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 {
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(&sections)
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(
&sections, "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 {
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(&sections, "id <> ?", section.ID)
return c.Render("section_form", fiber.Map{
"PageTitle": title,
"Section": section,
"Sections": sections,
"IsParent": isParent,
"Errors": errors,
})
}

View file

@ -68,8 +68,10 @@
id="parent_section"
class="form-control"
name="parent_section"
{% if IsParent %}disabled{% endif %}
>
<option value="0">--- Pas de section parente ---</option>
{% if !IsParent %}
{% for ParentSection in Sections %}
<option
value="{{ ParentSection.ID }}"
@ -78,6 +80,7 @@
{{ ParentSection.Name }}
</option>
{% endfor %}
{% endif %}
</select>
</div>