Order columns in people list
This commit is contained in:
parent
9dda517b03
commit
56a2d30189
4 changed files with 171 additions and 22 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue