Members: show & add

This commit is contained in:
William Bouzourène 2025-01-13 12:38:19 +01:00
parent efafb845f3
commit 917fe799d3
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
5 changed files with 605 additions and 5 deletions

View file

@ -2,13 +2,29 @@ package controllers
import ( import (
"errors" "errors"
"fmt"
"strconv"
"git.readonly.ch/bouzoure/pop-camarades/helpers" "git.readonly.ch/bouzoure/pop-camarades/helpers"
"git.readonly.ch/bouzoure/pop-camarades/models" "git.readonly.ch/bouzoure/pop-camarades/models"
"github.com/go-playground/validator"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"gorm.io/gorm" "gorm.io/gorm"
) )
type PersonValidation struct {
LastName string `validate:"required,min=1,max=100"`
FirstName string `validate:"required,min=1,max=100"`
Email string `validate:"max=100,email"`
Phone string `validate:"max=100"`
Mobile string `validate:"max=100"`
Address1 string `validate:"max=100"`
Address2 string `validate:"max=100"`
PostalCode string `validate:"min=4,max=4,number"`
City string `validate:"max=100"`
Section string `validate:"required,number"`
}
func Members(c *fiber.Ctx) error { func Members(c *fiber.Ctx) error {
db, err := helpers.GetDatabase() db, err := helpers.GetDatabase()
if err != nil { if err != nil {
@ -32,3 +48,147 @@ func Members(c *fiber.Ctx) error {
"People": people, "People": people,
}) })
} }
func MemberShow(c *fiber.Ctx) error {
id := c.Params("id")
db, err := helpers.GetDatabase()
if err != nil {
return err
}
var person models.Person
result := db.Preload("Section").Find(
&person, "id = ? AND is_member", id, true,
)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil {
return result.Error
}
title := fmt.Sprintf(
"%s %s | Membre",
person.LastName,
person.FirstName,
)
return c.Render("person", fiber.Map{
"PageTitle": title,
"Person": person,
})
}
func MemberAdd(c *fiber.Ctx) error {
var person models.Person
var errors []string
db, err := helpers.GetDatabase()
if err != nil {
return err
}
var sections []models.Section
db.Order("name collate nocase asc").Find(
&sections,
"contains_members = ?",
true,
)
if c.Method() == "POST" {
data := PersonValidation{
LastName: c.FormValue("last_name"),
FirstName: c.FormValue("first_name"),
Email: c.FormValue("email"),
Phone: c.FormValue("phone"),
Mobile: c.FormValue("mobile"),
Address1: c.FormValue("address1"),
Address2: c.FormValue("address2"),
PostalCode: c.FormValue("postal_code"),
City: c.FormValue("city"),
Section: c.FormValue("section"),
}
validate := validator.New()
validErrs := validate.Struct(data)
if validErrs != nil {
for _, validErr := range validErrs.(validator.ValidationErrors) {
switch validErr.Field() {
case "LastName":
errors = append(errors, "Le nom de famille est requis et ne peut pas contenir plus de 100 caractères")
case "FirstName":
errors = append(errors, "Le prénom est requis et ne peut pas contenir plus de 100 caractères")
case "Email":
if len(data.Email) > 0 {
errors = append(errors, "L'adresse email doit être valide")
}
case "Phone":
errors = append(errors, "Le numéro de téléphone fixe est trop long")
case "Mobile":
errors = append(errors, "Le numéro de téléphone mobile est trop long")
case "Address1":
errors = append(errors, "La ligne 1 de l'adresse est trop longue")
case "Address2":
errors = append(errors, "La ligne 2 de l'adresse est trop longue")
case "PostalCode":
if len(data.PostalCode) > 0 {
errors = append(errors, "Le code postal n'est pas valide")
}
case "City":
errors = append(errors, "Le lieu est trop long")
case "Section":
errors = append(errors, "La section n'est pas valide")
}
}
}
person.IsContact = false
person.IsMember = true
person.LastName = data.LastName
person.FirstName = data.FirstName
person.Email = data.Email
person.Phone = data.Phone
person.Mobile = data.Mobile
person.Address1 = data.Address1
person.Address2 = data.Address2
person.PostalCode = data.PostalCode
person.City = data.City
sectionID, err := strconv.ParseUint(data.Section, 10, 0)
if err == nil {
for _, section := range sections {
if section.ID == uint(sectionID) {
person.SectionID = uint(sectionID)
break
}
}
}
if person.SectionID == 0 {
errors = append(errors, "La section est introuvable")
}
if len(errors) == 0 {
result := db.Create(&person)
if result.Error != nil {
return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/members/%d",
person.ID,
))
}
}
}
return c.Render("person_form", fiber.Map{
"PageTitle": "Ajouter un membre",
"Person": person,
"Sections": sections,
"Errors": errors,
})
}

View file

@ -118,6 +118,9 @@ func main() {
// Members // Members
app.Get("/members", controllers.Members) app.Get("/members", controllers.Members)
app.Get("/members/:id<int;min(0)>", controllers.MemberShow)
app.Get("/members/add", controllers.MemberAdd)
app.Post("/members/add", controllers.MemberAdd)
// Contacts // Contacts
app.Get("/contacts", controllers.Contacts) app.Get("/contacts", controllers.Contacts)

View file

@ -34,22 +34,22 @@
<tr> <tr>
<td> <td>
{% if Person.IsMember %} {% if Person.IsMember %}
<a href="/member/{{ Person.ID }}"> <a href="/members/{{ Person.ID }}">
{{ Person.LastName }} {{ Person.LastName }}
</a> </a>
{% else %} {% else %}
<a href="/contact/{{ Person.ID }}"> <a href="/contacts/{{ Person.ID }}">
{{ Person.LastName }} {{ Person.LastName }}
</a> </a>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{% if Person.IsMember %} {% if Person.IsMember %}
<a href="/member/{{ Person.ID }}"> <a href="/members/{{ Person.ID }}">
{{ Person.FirstName }} {{ Person.FirstName }}
</a> </a>
{% else %} {% else %}
<a href="/contact/{{ Person.ID }}"> <a href="/contacts/{{ Person.ID }}">
{{ Person.FirstName }} {{ Person.FirstName }}
</a> </a>
{% endif %} {% endif %}
@ -59,7 +59,7 @@
</td> </td>
<td> <td>
{% if Person.SectionID %} {% if Person.SectionID %}
{{ Section.Section.Name }} {{ Person.Section.Name }}
{% endif %} {% endif %}
</td> </td>
</tr> </tr>

221
views/person.html Normal file
View file

@ -0,0 +1,221 @@
{% 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>
{% if Person.IsMember %}
<li class="breadcrumb-item"><a href="/members">Membres</a></li>
{% else %}
<li class="breadcrumb-item"><a href="/contacts">Contacts</a></li>
{% endif %}
<li class="breadcrumb-item active">
{{ Person.LastName }} {{ Person.FirstName }}
</li>
</ol>
</nav>
<hr>
</div>
<div class="row mb-3">
<div class="col-md-2">
Nom de famille
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.LastName }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-3">
<div class="col-md-2">
Prénom
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.FirstName }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-3">
<div class="col-md-2">
Email
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.Email }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-3">
<div class="col-md-2">
Téléphone fixe
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.Phone }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-5">
<div class="col-md-2">
Téléphone mobile
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.Mobile }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-2">
<div class="col-md-2">
Adresse
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.Address1 }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-2">
<div class="col-md-10 offset-md-2">
<input
type="text"
class="form-control"
value="{{ Person.Address2 }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-5">
<div class="col-md-3 col-lg-2 offset-md-2 mb-2 mb-md-0">
<input
type="text"
class="form-control"
value="{{ Person.PostalCode }}"
disabled
readonly
>
</div>
<div class="col-md-7 col-lg-8">
<input
type="text"
class="form-control"
value="{{ Person.City }}"
disabled
readonly
>
</div>
</div>
<div class="row mb-3">
<div class="col-md-2">
Section
</div>
<div class="col-md-10">
<input
type="text"
class="form-control"
value="{{ Person.Section.Name }}"
disabled
readonly
>
</div>
</div>
<div class="my-4">
{% if Person.IsMember %}
<div class="mb-3">
<a class="btn btn-md btn-primary" href="/members/{{ Person.ID }}/edit">
<i class="bi-pencil-square"></i>
Modifier
</a>
<form
action="/members/{{ Person.ID }}/convert"
method="post"
class="d-inline p-0"
>
<button class="btn btn-md btn-primary areyousure" type="submit">
<i class="bi-arrow-repeat"></i>
Convertir en contact
</button>
</form>
</div>
<div>
<form
action="/members/{{ Person.ID }}/archive"
method="post"
class="d-inline p-0"
>
<button class="btn btn-md btn-warning areyousure" type="submit">
<i class="bi-archive"></i>
Archiver
</button>
</form>
<form
action="/members/{{ Person.ID }}/purge"
method="post"
class="d-inline p-0"
>
<button class="btn btn-md btn-danger areyousure" type="submit">
<i class="bi-trash3"></i>
Supprimer
</button>
</form>
</div>
{% else %}
<a class="btn btn-md btn-primary" href="/contacts/{{ Person.ID }}/edit">
<i class="bi-pencil-square"></i>
Modifier
</a>
<form
action="/contacts/{{ Person.ID }}/delete"
method="post"
class="d-inline p-0"
>
<button class="btn btn-md btn-danger areyousure" type="submit">
<i class="bi-trash3"></i>
Supprimer
</button>
</form>
{% endif %}
</div>
</div>
{% endblock %}

216
views/person_form.html Normal file
View file

@ -0,0 +1,216 @@
{% 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>
{% if Person.IsMember %}
<li class="breadcrumb-item"><a href="/members">Membres</a></li>
{% else %}
<li class="breadcrumb-item"><a href="/contacts">Contacts</a></li>
{% endif %}
{% if Person.ID %}
{% if Person.IsMember %}
<li class="breadcrumb-item">
<a href="/admin/members/{{ Person.ID }}">
{{ Person.LastName }} {{ Person.FirstName }}
</a>
</li>
{% else %}
<li class="breadcrumb-item">
<a href="/admin/contacts/{{ Person.ID }}">
{{ Person.LastName }} {{ Person.FirstName }}
</a>
</li>
{% endif %}
<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="row mb-3">
<label for="last_name" class="form-label col-md-2">
Nom de famille
</label>
<div class="col-md-10">
<input
id="name"
class="form-control"
type="text"
name="last_name"
required
value="{{ Person.LastName }}"
autofocus
>
</div>
</div>
<div class="row mb-3">
<label for="first_name" class="form-label col-md-2">
Prénom
</label>
<div class="col-md-10">
<input
id="first_name"
class="form-control"
type="text"
name="first_name"
required
value="{{ Person.FirstName }}"
>
</div>
</div>
<div class="row mb-3">
<label for="email" class="form-label col-md-2">
Email
</label>
<div class="col-md-10">
<input
id="email"
class="form-control"
type="email"
name="email"
value="{{ Person.Email }}"
>
</div>
</div>
<div class="row mb-3">
<label for="phone" class="form-label col-md-2">
Téléphone fixe
</label>
<div class="col-md-10">
<input
id="phone"
class="form-control"
type="text"
name="phone"
value="{{ Person.Phone }}"
>
</div>
</div>
<div class="row mb-5">
<label for="mobile" class="form-label col-md-2">
Téléphone mobile
</label>
<div class="col-md-10">
<input
id="mobile"
class="form-control"
type="text"
name="mobile"
value="{{ Person.Mobile }}"
>
</div>
</div>
<div class="row mb-2">
<label for="address1" class="form-label col-md-2">
Adresse
</label>
<div class="col-md-10">
<input
id="address1"
class="form-control"
type="text"
name="address1"
value="{{ Person.Address1 }}"
placeholder="Ligne 1"
>
</div>
</div>
<div class="row mb-2">
<div class="col-md-10 offset-md-2">
<input
id="address2"
class="form-control"
type="text"
name="address2"
value="{{ Person.Address2 }}"
placeholder="Ligne 2"
>
</div>
</div>
<div class="row mb-5">
<div class="col-md-3 col-lg-2 offset-md-2 mb-2 mb-md-0">
<input
id="postal_code"
class="form-control"
type="text"
name="postal_code"
placeholder="Code postal"
pattern="[0-9]{4}"
>
</div>
<div class="col-md-7 col-lg-8">
<input
id="city"
class="form-control"
type="text"
name="city"
value="{{ Person.City }}"
placeholder="Lieu"
>
</div>
</div>
<div class="row mb-3">
<label for="section" class="form-label col-md-2">
Section
</label>
<div class="col-md-10">
<select
class="form-control"
name="section"
id="section"
required
>
{% for Section in Sections %}
<option
value="{{ Section.ID }}"
{% if Section.ID == Person.Section %}
selected
{% endif %}
>
{{ Section.Name }}
</option>
{% endfor %}
</select>
</div>
</div>
<div class="my-4">
<button class="btn btn-primary" type="submit">
<i class="me-1 bi-floppy"></i>
Enregistrer
</button>
</div>
</form>
</div>
{% endblock %}