diff --git a/controllers/contacts.go b/controllers/contacts.go index 5f4b8a8..aa15e61 100644 --- a/controllers/contacts.go +++ b/controllers/contacts.go @@ -68,6 +68,9 @@ func Contacts(c *fiber.Ctx) error { params.PageSize = 50 params.PersonType = "contacts" + params.OrderColumn = c.Query("c") + params.OrderDirection = c.Query("o") + var sections []models.Section db.Order("name asc").Find(§ions, "contains_contacts = ? AND id IN ?", true, allowedSections) params.AllowedSections = allowedSections @@ -100,6 +103,8 @@ func Contacts(c *fiber.Ctx) error { "SearchJSON": searchJSON, "Sections": sections, "Fields": fields, + "OrderCol": params.OrderColumn, + "OrderDir": params.OrderDirection, }) } diff --git a/controllers/members.go b/controllers/members.go index c26381a..e1c0405 100644 --- a/controllers/members.go +++ b/controllers/members.go @@ -83,6 +83,9 @@ func Members(c *fiber.Ctx) error { params.PageSize = 50 params.PersonType = "members" + params.OrderColumn = c.Query("c") + params.OrderDirection = c.Query("o") + var sections []models.Section db.Order("name asc").Find(§ions, "contains_members = ? AND id IN ?", true, allowedSections) params.AllowedSections = allowedSections @@ -115,6 +118,8 @@ func Members(c *fiber.Ctx) error { "SearchJSON": searchJSON, "Sections": sections, "Fields": fields, + "OrderCol": params.OrderColumn, + "OrderDir": params.OrderDirection, }) } diff --git a/helpers/database/people.go b/helpers/database/people.go index e5ad054..e9a3f9d 100644 --- a/helpers/database/people.go +++ b/helpers/database/people.go @@ -27,6 +27,8 @@ type PeopleSearchParams struct { PageSize int `json:"-"` PageNumber int `json:"-"` AllowedSections []uint `json:"-"` + OrderColumn string `json:"-"` + OrderDirection string `json:"-"` } type PeopleSearchResults struct { @@ -43,6 +45,27 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) { return results, nil } + if strings.EqualFold(params.OrderDirection, "DESC") { + params.OrderDirection = "DESC" + } else { + params.OrderDirection = "ASC" + } + + switch strings.ToLower(params.OrderColumn) { + case "address": + params.OrderColumn = "people.address1" + case "npa": + params.OrderColumn = "people.postal_code" + case "section": + params.OrderColumn = "people.section_id" + case "created": + params.OrderColumn = "people.created_at" + case "updated": + params.OrderColumn = "people.updated_at" + default: + params.OrderColumn = "CONCAT(people.last_name, people.first_name)" + } + // SQL qeury for results sqlQuery := ` SELECT people.* @@ -353,9 +376,9 @@ func PeopleSearch(params PeopleSearchParams) (PeopleSearchResults, error) { %s %s GROUP BY people.id - ORDER BY CONCAT(people.last_name, people.first_name) ASC + ORDER BY %s %s %s - `, sqlQuery, sqlFieldJoins, sqlFilters, sqlPagination) + `, sqlQuery, sqlFieldJoins, sqlFilters, params.OrderColumn, params.OrderDirection, sqlPagination) sqlResult := db.Raw(sqlQuery, sqlParams...).Preload("Section").Find(&results.Results) if sqlResult.Error != nil { diff --git a/views/people.html b/views/people.html index a6c6d02..6075eb1 100644 --- a/views/people.html +++ b/views/people.html @@ -244,16 +244,124 @@
Inconnu
+ {% else %}
+ {{ Person.CreatedAt|date:"02.01.2006 15:04" }}
+ {% endif %}
Inconnu
+ {% else %}
+ {{ Person.UpdatedAt|date:"02.01.2006 15:04" }}
+ {% endif %}
+