From d0282568938ac00549944977b64c6d458e337bf2 Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Tue, 1 Nov 2022 11:32:13 +0100 Subject: [PATCH] Improve sorting --- handlers/application/administration.go | 12 ++------ handlers/application/agenda.go | 9 ++---- handlers/application/beneficiaries.go | 11 ++----- handlers/application/dashboard.go | 3 +- handlers/application/vehicles.go | 4 +++ .../default/web/layouts/vehicles/search.html | 13 ++++---- themes/default/web/public/css/main.css | 30 ++++++------------- utils/sorting/beneficiaries.go | 13 ++++++++ utils/sorting/events.go | 11 +++++++ utils/sorting/groups.go | 15 ++++++++++ 10 files changed, 67 insertions(+), 54 deletions(-) create mode 100644 utils/sorting/beneficiaries.go create mode 100644 utils/sorting/events.go create mode 100644 utils/sorting/groups.go diff --git a/handlers/application/administration.go b/handlers/application/administration.go index 9abb126..b6b85e4 100644 --- a/handlers/application/administration.go +++ b/handlers/application/administration.go @@ -8,9 +8,9 @@ import ( "io" "net/http" "sort" - "strings" "time" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage" accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi" @@ -19,14 +19,6 @@ import ( "google.golang.org/protobuf/types/known/structpb" ) -type GroupsByName []groupstorage.Group - -func (a GroupsByName) Len() int { return len(a) } -func (a GroupsByName) Less(i, j int) bool { - return strings.Compare(a[i].Data["name"].(string), a[j].Data["name"].(string)) < 0 -} -func (a GroupsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) { request := &groupsmanagement.GetGroupsRequest{ @@ -47,7 +39,7 @@ func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Reque groups = append(groups, g) } - sort.Sort(GroupsByName(groups)) + sort.Sort(sorting.GroupsByName(groups)) h.Renderer.Administration(w, r, groups) } diff --git a/handlers/application/agenda.go b/handlers/application/agenda.go index 693cab4..6c20ec5 100644 --- a/handlers/application/agenda.go +++ b/handlers/application/agenda.go @@ -11,6 +11,7 @@ import ( formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi" agendastorage "git.coopgo.io/coopgo-platform/agenda/storage" groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" @@ -21,12 +22,6 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" ) -type EventsByStartdate []agendastorage.Event - -func (e EventsByStartdate) Len() int { return len(e) } -func (e EventsByStartdate) Less(i, j int) bool { return e[i].Startdate.Before(e[j].Startdate) } -func (e EventsByStartdate) Swap(i, j int) { e[i], e[j] = e[j], e[i] } - type EventsForm struct { Name string `json:"name" validate:"required"` Type string `json:"type" validate:"required"` @@ -60,7 +55,7 @@ func (h *ApplicationHandler) AgendaHome(w http.ResponseWriter, r *http.Request) responses = append(responses, e.ToStorageType()) } - sort.Sort(EventsByStartdate(responses)) + sort.Sort(sorting.EventsByStartdate(responses)) groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{ Groupids: groupids, diff --git a/handlers/application/beneficiaries.go b/handlers/application/beneficiaries.go index 6e34a3a..c0dfb3f 100644 --- a/handlers/application/beneficiaries.go +++ b/handlers/application/beneficiaries.go @@ -18,6 +18,7 @@ import ( formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators" "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" profilepictures "git.coopgo.io/coopgo-apps/parcoursmob/utils/profile-pictures" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage" fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi" groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" @@ -40,14 +41,6 @@ type BeneficiariesForm struct { Gender string `json:"gender"` } -type BeneficiariesByName []mobilityaccountsstorage.Account - -func (e BeneficiariesByName) Len() int { return len(e) } -func (e BeneficiariesByName) Less(i, j int) bool { - return e[i].Data["first_name"].(string) < e[j].Data["first_name"].(string) -} -func (e BeneficiariesByName) Swap(i, j int) { e[i], e[j] = e[j], e[i] } - func (h *ApplicationHandler) BeneficiariesList(w http.ResponseWriter, r *http.Request) { accounts, err := h.beneficiaries(r) @@ -57,7 +50,7 @@ func (h *ApplicationHandler) BeneficiariesList(w http.ResponseWriter, r *http.Re return } - sort.Sort(BeneficiariesByName(accounts)) + sort.Sort(sorting.BeneficiariesByName(accounts)) cacheid := uuid.NewString() h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour) diff --git a/handlers/application/dashboard.go b/handlers/application/dashboard.go index 9d1e8b7..8eead7d 100644 --- a/handlers/application/dashboard.go +++ b/handlers/application/dashboard.go @@ -7,6 +7,7 @@ import ( "sort" "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi" agendastorage "git.coopgo.io/coopgo-platform/agenda/storage" "git.coopgo.io/coopgo-platform/groups-management/storage" @@ -69,7 +70,7 @@ func (h *ApplicationHandler) Dashboard(w http.ResponseWriter, r *http.Request) { events = append(events, e.ToStorageType()) } - sort.Sort(EventsByStartdate(events)) + sort.Sort(sorting.EventsByStartdate(events)) h.Renderer.Dashboard(w, r, accounts, count, count_members, events) diff --git a/handlers/application/vehicles.go b/handlers/application/vehicles.go index 92f0a3f..5755614 100644 --- a/handlers/application/vehicles.go +++ b/handlers/application/vehicles.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "net/http" + "sort" "time" "git.coopgo.io/coopgo-apps/parcoursmob/utils/identification" + "git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting" fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi" groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi" "git.coopgo.io/coopgo-platform/groups-management/storage" @@ -72,6 +74,8 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques return } + sort.Sort(sorting.BeneficiariesByName(accounts)) + h.Renderer.VehiclesSearch(w, r, accounts, searched, vehicles, beneficiary, r.FormValue("startdate"), r.FormValue("enddate")) } diff --git a/themes/default/web/layouts/vehicles/search.html b/themes/default/web/layouts/vehicles/search.html index db6d7e6..d0d87c6 100644 --- a/themes/default/web/layouts/vehicles/search.html +++ b/themes/default/web/layouts/vehicles/search.html @@ -23,14 +23,15 @@
-
- +