Gestion des sections
This commit is contained in:
parent
3e984d059c
commit
f12e14a85d
5 changed files with 498 additions and 0 deletions
227
controllers/sections.go
Normal file
227
controllers/sections.go
Normal file
|
|
@ -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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue