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")
|
||||||
|
}
|
||||||
9
main.go
9
main.go
|
|
@ -112,6 +112,15 @@ func main() {
|
||||||
app.Get("/totp/verify", controllers.TotpVerifyPage)
|
app.Get("/totp/verify", controllers.TotpVerifyPage)
|
||||||
app.Post("/totp/verify", controllers.TotpVerifyPage)
|
app.Post("/totp/verify", controllers.TotpVerifyPage)
|
||||||
|
|
||||||
|
// Admin: Sections
|
||||||
|
app.Get("/admin/sections", controllers.Sections)
|
||||||
|
app.Get("/admin/sections/:id<int;min(0)>", controllers.SectionShow)
|
||||||
|
app.Get("/admin/sections/add", controllers.SectionAdd)
|
||||||
|
app.Post("/admin/sections/add", controllers.SectionAdd)
|
||||||
|
app.Get("/admin/sections/:id<int;min(0)>/edit", controllers.SectionEdit)
|
||||||
|
app.Post("/admin/sections/:id<int;min(0)>/edit", controllers.SectionEdit)
|
||||||
|
app.Post("/admin/sections/:id<int;min(0)>/delete", controllers.SectionDelete)
|
||||||
|
|
||||||
// Admin: Lists
|
// Admin: Lists
|
||||||
app.Get("/admin/lists", controllers.Lists)
|
app.Get("/admin/lists", controllers.Lists)
|
||||||
app.Get("/admin/lists/:id<int;min(0)>", controllers.ListShow)
|
app.Get("/admin/lists/:id<int;min(0)>", controllers.ListShow)
|
||||||
|
|
|
||||||
64
views/section.html
Normal file
64
views/section.html
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
{% extends "layouts/main.html" %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="mb-4">
|
||||||
|
<nav>
|
||||||
|
<ol class="breadcrumb">
|
||||||
|
<li class="breadcrumb-item"><a href="/">Accueil</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="/admin">Administration</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="/admin/sections">Sections</a></li>
|
||||||
|
<li class="breadcrumb-item active">{{ Section.Name }}</li>
|
||||||
|
</ol>
|
||||||
|
</nav>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<b>Nom</b><br>
|
||||||
|
{{ Section.Name }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<b>Nom technique</b><br>
|
||||||
|
{{ Section.ShortName }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<b>Section parente</b><br>
|
||||||
|
{% if Section.ParentSectionID %}
|
||||||
|
{{ Section.ParentSection.Name }}
|
||||||
|
{% else %}
|
||||||
|
N/A
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<b>Contient des membres</b><br>
|
||||||
|
{% if Section.ContainsMembers %}Oui{% else %}Non{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<b>Contient des contacts</b><br>
|
||||||
|
{% if Section.ContainsContacts %}Oui{% else %}Non{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-3">
|
||||||
|
<a class="btn btn-md btn-primary" href="/admin/sections/{{ Section.ID }}/edit">
|
||||||
|
<i class="feather" data-feather="edit-2"></i>
|
||||||
|
Modifier
|
||||||
|
</a>
|
||||||
|
<form
|
||||||
|
action="/admin/sections/{{ Section.ID }}/delete"
|
||||||
|
method="post"
|
||||||
|
class="d-inline p-0"
|
||||||
|
>
|
||||||
|
<button class="btn btn-md btn-danger areyousure" type="submit">
|
||||||
|
<i class="feather" data-feather="trash-2"></i>
|
||||||
|
Supprimer
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
119
views/section_form.html
Normal file
119
views/section_form.html
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
{% extends "layouts/main.html" %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="mb-4">
|
||||||
|
<nav>
|
||||||
|
<ol class="breadcrumb">
|
||||||
|
<li class="breadcrumb-item"><a href="/">Accueil</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="/admin">Administration</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="/admin/sections">Sections</a></li>
|
||||||
|
|
||||||
|
{% if Section.ID %}
|
||||||
|
<li class="breadcrumb-item"><a href="/admin/sections/{{ Section.ID }}">{{ Section.Name }}</a></li>
|
||||||
|
<li class="breadcrumb-item active">Modifier</li>
|
||||||
|
{% else %}
|
||||||
|
<li class="breadcrumb-item active">Ajouter</li>
|
||||||
|
{% endif %}
|
||||||
|
</ol>
|
||||||
|
</nav>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if Errors %}
|
||||||
|
<div class="alert alert-danger">
|
||||||
|
<ul class="m-0">
|
||||||
|
{% for Error in Errors %}
|
||||||
|
<li>{{ Error }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<form id="section" method="post">
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name" class="form-label">
|
||||||
|
Nom
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="name"
|
||||||
|
class="form-control"
|
||||||
|
type="text"
|
||||||
|
name="name"
|
||||||
|
required
|
||||||
|
value="{{ Section.Name }}"
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="short_name" class="form-label">
|
||||||
|
Nom technique
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="short_name"
|
||||||
|
class="form-control"
|
||||||
|
type="text"
|
||||||
|
name="short_name"
|
||||||
|
required
|
||||||
|
value="{{ Section.ShortName }}"
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="parent_section" class="form-label">
|
||||||
|
Section parente
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="parent_section"
|
||||||
|
class="form-control"
|
||||||
|
name="parent_section"
|
||||||
|
>
|
||||||
|
<option value="0">--- Pas de section parente ---</option>
|
||||||
|
{% for ParentSection in Sections %}
|
||||||
|
<option
|
||||||
|
value="{{ ParentSection.ID }}"
|
||||||
|
{% if Section.ParentSectionID == ParentSection.ID %}selected{% endif %}
|
||||||
|
>
|
||||||
|
{{ ParentSection.Name }}
|
||||||
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
class="form-check-input me-2"
|
||||||
|
id="contains_members"
|
||||||
|
name="contains_members"
|
||||||
|
{% if Section.ContainsMembers %}checked{% endif %}
|
||||||
|
>
|
||||||
|
<label for="contains_members" class="form-label">
|
||||||
|
Contient des membres
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
class="form-check-input me-2"
|
||||||
|
id="contains_contacts"
|
||||||
|
name="contains_contacts"
|
||||||
|
{% if Section.ContainsContacts %}checked{% endif %}
|
||||||
|
>
|
||||||
|
<label for="contains_contacts" class="form-label">
|
||||||
|
Contient des contacts
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-3">
|
||||||
|
<button class="btn btn-primary" type="submit">
|
||||||
|
<i class="me-1" data-feather="save"></i>
|
||||||
|
Enregistrer
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
79
views/sections.html
Normal file
79
views/sections.html
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
{% extends "layouts/main.html" %}
|
||||||
|
|
||||||
|
{% block main %}
|
||||||
|
<div class="container mt-4">
|
||||||
|
<div class="mb-4">
|
||||||
|
<nav>
|
||||||
|
<ol class="breadcrumb">
|
||||||
|
<li class="breadcrumb-item"><a href="/">Accueil</a></li>
|
||||||
|
<li class="breadcrumb-item"><a href="/admin">Administration</a></li>
|
||||||
|
<li class="breadcrumb-item active">Sections</li>
|
||||||
|
</ol>
|
||||||
|
</nav>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if Sections %}
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Nom</th>
|
||||||
|
<th>Nom technique</th>
|
||||||
|
<th>Section parente</th>
|
||||||
|
<th>Contient membres</th>
|
||||||
|
<th>Contient contacts</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for Section in Sections %}
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<a href="/admin/sections/{{ Section.ID }}">
|
||||||
|
{{ Section.Name }}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ Section.ShortName }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{% if Section.ParentSectionID %}
|
||||||
|
{{ Section.ParentSection.Name }}
|
||||||
|
{% else %}
|
||||||
|
N/A
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{% if Section.ContainsMembers %}
|
||||||
|
Oui
|
||||||
|
{% else %}
|
||||||
|
Non
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{% if Section.ContainsContacts %}
|
||||||
|
Oui
|
||||||
|
{% else %}
|
||||||
|
Non
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="my-4">
|
||||||
|
Pas de section pour le moment
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<div class="mt-3">
|
||||||
|
<a class="btn btn-md btn-primary" href="/admin/sections/add">
|
||||||
|
<i class="feather" data-feather="plus"></i>
|
||||||
|
Ajouter
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue