Order columns in people list

This commit is contained in:
William Bouzourène 2025-07-11 11:56:47 +02:00
parent 9dda517b03
commit 56a2d30189
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
4 changed files with 171 additions and 22 deletions

View file

@ -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 {