Sections: prevent parent/child loop & force unqiue name/shortname
This commit is contained in:
parent
360ed9acd4
commit
1cdbc1cbb6
2 changed files with 52 additions and 16 deletions
|
|
@ -69,6 +69,11 @@ func SectionAdd(c *fiber.Ctx) error {
|
||||||
return err
|
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" {
|
if c.Method() == "POST" {
|
||||||
name := c.FormValue("name")
|
name := c.FormValue("name")
|
||||||
shortName := c.FormValue("short_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")
|
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.Name = name
|
||||||
section.ShortName = shortName
|
section.ShortName = shortName
|
||||||
|
|
||||||
section.ParentSectionID = 0
|
section.ParentSectionID = 0
|
||||||
parentSectionID, err := strconv.ParseUint(parentSection, 10, 0)
|
parentSectionID, err := strconv.ParseUint(parentSection, 10, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
section.ParentSectionID = uint(parentSectionID)
|
for _, parentSection := range sections {
|
||||||
|
if parentSection.ID == uint(parentSectionID) {
|
||||||
|
section.ParentSectionID = uint(parentSectionID)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
section.ContainsMembers = false
|
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{
|
return c.Render("section_form", fiber.Map{
|
||||||
"PageTitle": "Ajouter une section",
|
"PageTitle": "Ajouter une section",
|
||||||
"Section": section,
|
"Section": section,
|
||||||
|
|
@ -146,11 +167,20 @@ func SectionEdit(c *fiber.Ctx) error {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var childSections []models.Section
|
||||||
|
db.Find(&childSections, "parent_section_id = ?", section.ID)
|
||||||
|
isParent := (len(childSections) > 0)
|
||||||
|
|
||||||
title := fmt.Sprintf(
|
title := fmt.Sprintf(
|
||||||
"%s | Modifier section",
|
"%s | Modifier section",
|
||||||
section.Name,
|
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
|
var errors []string
|
||||||
if c.Method() == "POST" {
|
if c.Method() == "POST" {
|
||||||
name := c.FormValue("name")
|
name := c.FormValue("name")
|
||||||
|
|
@ -173,7 +203,12 @@ func SectionEdit(c *fiber.Ctx) error {
|
||||||
section.ParentSectionID = 0
|
section.ParentSectionID = 0
|
||||||
parentSectionID, err := strconv.ParseUint(parentSection, 10, 0)
|
parentSectionID, err := strconv.ParseUint(parentSection, 10, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
section.ParentSectionID = uint(parentSectionID)
|
for _, parentSection := range sections {
|
||||||
|
if parentSection.ID == uint(parentSectionID) {
|
||||||
|
section.ParentSectionID = uint(parentSectionID)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
section.ContainsMembers = false
|
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{
|
return c.Render("section_form", fiber.Map{
|
||||||
"PageTitle": title,
|
"PageTitle": title,
|
||||||
"Section": section,
|
"Section": section,
|
||||||
"Sections": sections,
|
"Sections": sections,
|
||||||
|
"IsParent": isParent,
|
||||||
"Errors": errors,
|
"Errors": errors,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,16 +68,19 @@
|
||||||
id="parent_section"
|
id="parent_section"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
name="parent_section"
|
name="parent_section"
|
||||||
|
{% if IsParent %}disabled{% endif %}
|
||||||
>
|
>
|
||||||
<option value="0">--- Pas de section parente ---</option>
|
<option value="0">--- Pas de section parente ---</option>
|
||||||
{% for ParentSection in Sections %}
|
{% if !IsParent %}
|
||||||
<option
|
{% for ParentSection in Sections %}
|
||||||
value="{{ ParentSection.ID }}"
|
<option
|
||||||
{% if Section.ParentSectionID == ParentSection.ID %}selected{% endif %}
|
value="{{ ParentSection.ID }}"
|
||||||
>
|
{% if Section.ParentSectionID == ParentSection.ID %}selected{% endif %}
|
||||||
{{ ParentSection.Name }}
|
>
|
||||||
</option>
|
{{ ParentSection.Name }}
|
||||||
{% endfor %}
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue