Lists: list & add

This commit is contained in:
William Bouzourène 2024-12-30 16:18:40 +01:00
parent 0159e8d528
commit 6acd737b99
6 changed files with 173 additions and 3 deletions

View file

@ -1,9 +1,32 @@
package controllers package controllers
import "github.com/gofiber/fiber/v2" import (
"errors"
"fmt"
"git.readonly.ch/bouzoure/popvaud-people/helpers"
"git.readonly.ch/bouzoure/popvaud-people/models"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
func Lists(c *fiber.Ctx) error { func Lists(c *fiber.Ctx) error {
return c.SendString("Lists") db, err := helpers.GetDatabase()
if err != nil {
return err
}
var lists []models.List
result := db.Find(&lists)
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return err
}
return c.Render("lists", fiber.Map{
"PageTitle": "Listes",
"Lists": lists,
})
} }
func ListShow(c *fiber.Ctx) error { func ListShow(c *fiber.Ctx) error {
@ -11,7 +34,46 @@ func ListShow(c *fiber.Ctx) error {
} }
func ListAdd(c *fiber.Ctx) error { func ListAdd(c *fiber.Ctx) error {
return c.SendString("ListAdd") var list models.List
var errors []string
db, err := helpers.GetDatabase()
if err != nil {
return err
}
if c.Method() == "POST" {
name := c.FormValue("name")
multi := c.FormValue("multi")
if len(name) > 100 || len(name) < 1 {
errors = append(errors, "Le nom doit avoir entre 1 et 100 caractères")
}
list.Name = name
list.Multi = false
if multi == "on" {
list.Multi = true
}
if len(errors) == 0 {
result := db.Create(&list)
if result.Error != nil {
return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
}
}
}
return c.Render("list_form", fiber.Map{
"PageTitle": "Ajouter une liste",
"List": list,
"Errors": errors,
})
} }
func ListEdit(c *fiber.Ctx) error { func ListEdit(c *fiber.Ctx) error {

View file

@ -42,6 +42,8 @@ func connectDatabase() (*gorm.DB, error) {
&models.Person{}, &models.Person{},
&models.ManageSection{}, &models.ManageSection{},
&models.ManageParentSection{}, &models.ManageParentSection{},
&models.List{},
&models.ListItem{},
) )
if err != nil { if err != nil {
// TODO: Handle exception // TODO: Handle exception

View file

@ -12,5 +12,6 @@ type ListItem struct {
gorm.Model gorm.Model
Value string Value string
Default string Default string
ListID uint
List List List List
} }

View file

@ -34,3 +34,11 @@ img#header-logo {
font-size: 12px; font-size: 12px;
padding-top: 4px; padding-top: 4px;
} }
a {
text-decoration: none;
}
.item-click {
cursor: pointer;
}

56
views/list_form.html Normal file
View file

@ -0,0 +1,56 @@
{% extends "layouts/main.html" %}
{% block main %}
<div class="container mt-4">
<h1>Ajouter un liste</h1>
<hr>
{% if Errors %}
<div class="alert alert-danger">
<ul class="m-0">
{% for Error in Errors %}
<li>{{ Error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<form id="list" 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="{{ List.Name }}"
>
</div>
<div class="mb-3">
<input
type="checkbox"
class="form-check-input me-2"
id="multi"
name="multi"
{% if List.Multi %}checked{% endif %}
>
<label for="multi" class="form-label">
Liste à choix multiples
</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 %}

41
views/lists.html Normal file
View file

@ -0,0 +1,41 @@
{% extends "layouts/main.html" %}
{% block main %}
<div class="container mt-4">
<h1>Listes</h1>
<hr>
{% if Lists %}
<div class="table-responsive">
<table class="table table-borderless table-hover">
<tbody>
{% for List in Lists %}
<tr>
<td
class="item-click"
onclick="window.location.href='/admin/lists/{{ List.ID }}'"
>
<a href="/admin/lists/{{ List.ID }}">
{{ List.Name }}
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="my-4">
Pas de liste pour le moment
</div>
{% endif %}
<div class="mt-3">
<a class="btn btn-md btn-primary" href="/admin/lists/add">
<i class="feather" data-feather="plus"></i>
Ajouter
</a>
</div>
</div>
{% endblock %}