From c54cad7738a95f732511585344398d1c4926740b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Mon, 30 Dec 2024 16:18:40 +0100 Subject: [PATCH] Lists: list & add --- controllers/lists.go | 68 ++++++++++++++++++++++++++++++++++++++++++-- helpers/database.go | 2 ++ models/lists.go | 1 + static/main.css | 8 ++++++ views/list_form.html | 56 ++++++++++++++++++++++++++++++++++++ views/lists.html | 41 ++++++++++++++++++++++++++ 6 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 views/list_form.html create mode 100644 views/lists.html diff --git a/controllers/lists.go b/controllers/lists.go index 0dfbd8e..c7fcb4c 100644 --- a/controllers/lists.go +++ b/controllers/lists.go @@ -1,9 +1,32 @@ 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 { - 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 { @@ -11,7 +34,46 @@ func ListShow(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 { diff --git a/helpers/database.go b/helpers/database.go index 3e423a6..cd4591c 100644 --- a/helpers/database.go +++ b/helpers/database.go @@ -42,6 +42,8 @@ func connectDatabase() (*gorm.DB, error) { &models.Person{}, &models.ManageSection{}, &models.ManageParentSection{}, + &models.List{}, + &models.ListItem{}, ) if err != nil { // TODO: Handle exception diff --git a/models/lists.go b/models/lists.go index 2f2b252..e46e330 100644 --- a/models/lists.go +++ b/models/lists.go @@ -12,5 +12,6 @@ type ListItem struct { gorm.Model Value string Default string + ListID uint List List } diff --git a/static/main.css b/static/main.css index 85bc7f6..dc02ea5 100644 --- a/static/main.css +++ b/static/main.css @@ -33,4 +33,12 @@ img#header-logo { text-align: center; font-size: 12px; padding-top: 4px; +} + +a { + text-decoration: none; +} + +.item-click { + cursor: pointer; } \ No newline at end of file diff --git a/views/list_form.html b/views/list_form.html new file mode 100644 index 0000000..3678d43 --- /dev/null +++ b/views/list_form.html @@ -0,0 +1,56 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+

Ajouter un liste

+
+ + {% if Errors %} +
+
    + {% for Error in Errors %} +
  • {{ Error }}
  • + {% endfor %} +
+
+ {% endif %} + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ +
+{% endblock %} diff --git a/views/lists.html b/views/lists.html new file mode 100644 index 0000000..541217c --- /dev/null +++ b/views/lists.html @@ -0,0 +1,41 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+

Listes

+
+ + {% if Lists %} +
+ + + {% for List in Lists %} + + + + {% endfor %} + +
+ + {{ List.Name }} + +
+
+ {% else %} +
+ Pas de liste pour le moment +
+ {% endif %} + + + +
+{% endblock %}