From 360ed9acd4e347252af1fc945055bada698dcbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Mon, 6 Jan 2025 17:05:03 +0100 Subject: [PATCH] Manage optionnal fields --- controllers/fields.go | 202 ++++++++++++++++++++++++++++++++++++++++++ helpers/database.go | 2 + main.go | 9 ++ models/fields.go | 40 +++++++++ views/field.html | 68 ++++++++++++++ views/field_form.html | 130 +++++++++++++++++++++++++++ views/fields.html | 67 ++++++++++++++ 7 files changed, 518 insertions(+) create mode 100644 controllers/fields.go create mode 100644 models/fields.go create mode 100644 views/field.html create mode 100644 views/field_form.html create mode 100644 views/fields.html diff --git a/controllers/fields.go b/controllers/fields.go new file mode 100644 index 0000000..3a3bdfb --- /dev/null +++ b/controllers/fields.go @@ -0,0 +1,202 @@ +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 Fields(c *fiber.Ctx) error { + db, err := helpers.GetDatabase() + if err != nil { + return err + } + + var fields []models.Field + result := db.Order("name collate nocase asc").Find(&fields) + + if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { + return err + } + + return c.Render("fields", fiber.Map{ + "PageTitle": "Champs supplémentaires", + "Fields": fields, + "PersonTypes": models.PersonTypes, + "FieldTypes": models.FieldTypes, + }) +} + +func FieldShow(c *fiber.Ctx) error { + id := c.Params("id") + + db, err := helpers.GetDatabase() + if err != nil { + return err + } + + var field models.Field + result := db.Preload("List").Find(&field, "id = ?", id) + + 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 | Champs supplémentaires", + field.Name, + ) + + return c.Render("field", fiber.Map{ + "PageTitle": title, + "Field": field, + "PersonTypes": models.PersonTypes, + "FieldTypes": models.FieldTypes, + }) +} + +func FieldAdd(c *fiber.Ctx) error { + var field models.Field + var errors []string + + db, err := helpers.GetDatabase() + if err != nil { + return err + } + + var lists []models.List + db.Order("name collate nocase asc").Find(&lists) + + if c.Method() == "POST" { + field.Name = c.FormValue("name") + if len(field.Name) > 100 || len(field.Name) < 1 { + errors = append(errors, "Le nom doit contentir entre 1 et 100 caractères") + } + + for key := range models.PersonTypes { + if c.FormValue("person_type") == key { + field.PersonType = key + } + } + + if len(field.PersonType) < 1 { + errors = append(errors, "Population incorrecte") + } + + for key := range models.FieldTypes { + if c.FormValue("field_type") == key { + field.FieldType = key + } + } + + if len(field.FieldType) < 1 { + errors = append(errors, "Type de champ incorrect") + } + + if field.FieldType == "list" { + listID, err := strconv.ParseUint(c.FormValue("list"), 10, 0) + if err != nil || listID < 1 { + errors = append(errors, "Liste incorrecte") + } else { + field.ListID = uint(listID) + } + } + + if len(errors) == 0 { + result := db.Create(&field) + if result.Error != nil { + return result.Error + } else { + c.Redirect(fmt.Sprintf( + "/admin/fields/%d", + field.ID, + )) + } + } + } + + return c.Render("field_form", fiber.Map{ + "PageTitle": "Ajouter un champ", + "Field": field, + "Lists": lists, + "Errors": errors, + "PersonTypes": models.PersonTypes, + "FieldTypes": models.FieldTypes, + }) +} + +func FieldEdit(c *fiber.Ctx) error { + id := c.Params("id") + + db, err := helpers.GetDatabase() + if err != nil { + return err + } + + var field models.Field + result := db.Find(&field, "id = ?", id) + + 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 | Modifier un champ", + field.Name, + ) + + var errors []string + if c.Method() == "POST" { + field.Name = c.FormValue("name") + if len(field.Name) > 100 || len(field.Name) < 1 { + errors = append(errors, "Le nom doit contentir entre 1 et 100 caractères") + } + + if len(errors) == 0 { + result := db.Save(&field) + if result.Error != nil { + return result.Error + } else { + c.Redirect(fmt.Sprintf( + "/admin/fields/%d", + field.ID, + )) + } + } + } + + return c.Render("field_form", fiber.Map{ + "PageTitle": title, + "Field": field, + "Errors": errors, + }) +} + +func FieldDelete(c *fiber.Ctx) error { + id := c.Params("id") + + db, err := helpers.GetDatabase() + if err != nil { + return err + } + + result := db.Delete(&models.Field{}, id) + if result.Error != nil { + return result.Error + } + + return c.Redirect("/admin/fields") +} diff --git a/helpers/database.go b/helpers/database.go index f1c8876..4783ce5 100644 --- a/helpers/database.go +++ b/helpers/database.go @@ -43,6 +43,8 @@ func connectDatabase() (*gorm.DB, error) { &models.Person{}, &models.List{}, &models.ListItem{}, + &models.Field{}, + &models.FieldValue{}, ) if err != nil { // TODO: Handle exception diff --git a/main.go b/main.go index 90e88df..904d861 100644 --- a/main.go +++ b/main.go @@ -144,6 +144,15 @@ func main() { app.Post("/admin/lists/:id/items/:itemid", controllers.ListItemEdit) app.Post("/admin/lists/:id/items/:itemid/delete", controllers.ListItemDelete) + // Admin: Fields + app.Get("/admin/fields", controllers.Fields) + app.Get("/admin/fields/:id", controllers.FieldShow) + app.Get("/admin/fields/add", controllers.FieldAdd) + app.Post("/admin/fields/add", controllers.FieldAdd) + app.Get("/admin/fields/:id/edit", controllers.FieldEdit) + app.Post("/admin/fields/:id/edit", controllers.FieldEdit) + app.Post("/admin/fields/:id/delete", controllers.FieldDelete) + // Admin: Users app.Get("/admin/users", controllers.Users) app.Get("/admin/users/:id", controllers.UserShow) diff --git a/models/fields.go b/models/fields.go new file mode 100644 index 0000000..6f643ff --- /dev/null +++ b/models/fields.go @@ -0,0 +1,40 @@ +package models + +import ( + "database/sql" + + "gorm.io/gorm" +) + +type Field struct { + gorm.Model + Name string + PersonType string + FieldType string + ListID uint + List List +} + +type FieldValue struct { + gorm.Model + FieldID uint + Field Field + ValueString sql.NullString + ValueInt sql.NullInt64 + ValueDate sql.NullTime + ListItemID uint + ListItem ListItem +} + +var PersonTypes = map[string]string{ + "member": "Membre", + "contact": "Contact", +} + +var FieldTypes = map[string]string{ + "text": "Texte", + "longtext": "Texte multiligne", + "number": "Nombre", + "date": "Date", + "list": "Liste", +} diff --git a/views/field.html b/views/field.html new file mode 100644 index 0000000..c0dd7ea --- /dev/null +++ b/views/field.html @@ -0,0 +1,68 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ +
+ Nom du champ
+ {{ Field.Name }} +
+ +
+ Population
+ {% for Key, Value in PersonTypes %} + {% if Key == Field.PersonType %} + {{ Value }} + {% endif %} + {% endfor %} +
+ +
+ Type de champ
+ {% for Key, Value in FieldTypes %} + {% if Key == Field.FieldType %} + {{ Value }} + {% endif %} + {% endfor %} +
+ + {% if Field.ListID %} + + + {% endif %} + +
+ + + Modifier + +
+ +
+
+ +
+{% endblock %} diff --git a/views/field_form.html b/views/field_form.html new file mode 100644 index 0000000..f1d58d0 --- /dev/null +++ b/views/field_form.html @@ -0,0 +1,130 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ + {% if Errors %} +
+
    + {% for Error in Errors %} +
  • {{ Error }}
  • + {% endfor %} +
+
+ {% endif %} + +
+ +
+ + +
+ + {% if !Field.ID %} +
+ + +
+ +
+ + +
+ +
+ + +
+ {% endif %} + +
+ +
+
+ +
+{% endblock %} + +{% block javascript %} + +{% endblock %} \ No newline at end of file diff --git a/views/fields.html b/views/fields.html new file mode 100644 index 0000000..f1cfdd9 --- /dev/null +++ b/views/fields.html @@ -0,0 +1,67 @@ +{% extends "layouts/main.html" %} + +{% block main %} +
+
+ +
+
+ + {% if Fields %} +
+ + + + + + + + + + {% for Field in Fields %} + + + + + + {% endfor %} + +
NomPopulationType de champ
+ + {{ Field.Name }} + + + {% for Key, Value in PersonTypes %} + {% if Key == Field.PersonType %} + {{ Value }} + {% endif %} + {% endfor %} + + {% for Key, Value in FieldTypes %} + {% if Key == Field.FieldType %} + {{ Value }} + {% endif %} + {% endfor %} +
+
+ {% else %} +
+ Pas de champs pour le moment +
+ {% endif %} + + + +
+{% endblock %}