pop-camarades/controllers/sections.go

260 lines
5.5 KiB
Go

package controllers
import (
"errors"
"fmt"
"strconv"
"git.readonly.ch/bouzoure/pop-camarades/helpers"
"git.readonly.ch/bouzoure/pop-camarades/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.Order("name collate nocase asc").Preload("ParentSection").Find(&sections)
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(&section, "id = ?", id)
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
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
}
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")
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")
}
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
if containsMembers == "on" {
section.ContainsMembers = true
}
section.ContainsContacts = false
if containsContacts == "on" {
section.ContainsContacts = true
}
if len(errors) == 0 {
result := db.Create(&section)
if result.Error != nil {
return result.Error
}
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
}
}
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(&section, "id = ?", id)
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
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")
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 {
for _, parentSection := range sections {
if parentSection.ID == uint(parentSectionID) {
section.ParentSectionID = uint(parentSectionID)
break
}
}
}
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(&section)
if result.Error != nil {
return result.Error
}
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
}
}
return c.Render("section_form", fiber.Map{
"PageTitle": title,
"Section": section,
"Sections": sections,
"IsParent": isParent,
"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 result.Error
}
return c.Redirect("/admin/sections")
}