@@ -430,7 +430,7 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, pm groups.PageMeta)
430
430
orderClause = fmt .Sprintf ("ORDER BY %s %s, g.id %s" , orderBy , dir , dir )
431
431
}
432
432
433
- q := fmt .Sprintf (`SELECT DISTINCT g.id, g.domain_id, tags, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
433
+ q := fmt .Sprintf (`SELECT g.id, g.domain_id, tags, COALESCE(g.parent_id, '') AS parent_id, g.name, g.description,
434
434
g.metadata, g.created_at, g.updated_at, g.updated_by, g.status FROM groups g %s %s LIMIT :limit OFFSET :offset;` , query , orderClause )
435
435
436
436
dbPageMeta , err := toDBGroupPageMeta (pm )
@@ -454,7 +454,7 @@ func (repo groupRepository) RetrieveAll(ctx context.Context, pm groups.PageMeta)
454
454
455
455
cq := fmt .Sprintf (` SELECT COUNT(*) AS total_count
456
456
FROM (
457
- SELECT DISTINCT g.id, g.domain_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.tags, g.description,
457
+ SELECT g.id, g.domain_id, COALESCE(g.parent_id, '') AS parent_id, g.name, g.tags, g.description,
458
458
g.metadata, g.created_at, g.updated_at, g.updated_by, g.status FROM groups g %s
459
459
) AS subquery;
460
460
` , query )
@@ -849,37 +849,54 @@ func (repo groupRepository) RetrieveUserGroups(ctx context.Context, domainID, us
849
849
850
850
func (repo groupRepository ) retrieveGroups (ctx context.Context , domainID , userID , query string , pm groups.PageMeta ) (groups.Page , error ) {
851
851
baseQuery := repo .userGroupsBaseQuery (domainID , userID )
852
+
853
+ orderClause := ""
854
+ var orderBy string
855
+ switch pm .Order {
856
+ case "name" :
857
+ orderBy = "g.name"
858
+ case "created_at" :
859
+ orderBy = "g.created_at"
860
+ case "updated_at" , "" :
861
+ orderBy = "COALESCE(g.updated_at, g.created_at)"
862
+ }
863
+
864
+ if orderBy != "" {
865
+ dir := pm .Dir
866
+ if dir != api .AscDir && dir != api .DescDir {
867
+ dir = api .DescDir
868
+ }
869
+ orderClause = fmt .Sprintf ("ORDER BY %s %s, g.id %s" , orderBy , dir , dir )
870
+ }
871
+
852
872
q := fmt .Sprintf (`%s
853
- SELECT
854
- g.id,
855
- g.name,
856
- g.domain_id,
857
- COALESCE(g.parent_id, '') AS parent_id,
858
- g.description,
859
- g.tags,
860
- g.metadata,
861
- g.created_at,
862
- g.updated_at,
863
- g.updated_by,
864
- g.status,
865
- g.path as path,
866
- g.role_id,
867
- g.role_name,
868
- g.actions,
869
- g.access_type,
870
- g.access_provider_id,
871
- g.access_provider_role_id,
872
- g.access_provider_role_name,
873
- g.access_provider_role_actions
874
- FROM
875
- final_groups g
876
- %s
877
- ORDER BY
878
- g.created_at
879
- LIMIT :limit
880
- OFFSET :offset;
881
- ` ,
882
- baseQuery , query )
873
+ SELECT
874
+ g.id,
875
+ g.name,
876
+ g.domain_id,
877
+ COALESCE(g.parent_id, '') AS parent_id,
878
+ g.description,
879
+ g.tags,
880
+ g.metadata,
881
+ g.created_at,
882
+ g.updated_at,
883
+ g.updated_by,
884
+ g.status,
885
+ g.path as path,
886
+ g.role_id,
887
+ g.role_name,
888
+ g.actions,
889
+ g.access_type,
890
+ g.access_provider_id,
891
+ g.access_provider_role_id,
892
+ g.access_provider_role_name,
893
+ g.access_provider_role_actions
894
+ FROM final_groups g
895
+ %s
896
+ %s
897
+ LIMIT :limit OFFSET :offset;` ,
898
+ baseQuery , query , orderClause )
899
+
883
900
dbPageMeta , err := toDBGroupPageMeta (pm )
884
901
if err != nil {
885
902
return groups.Page {}, errors .Wrap (repoerr .ErrFailedToRetrieveAllGroups , err )
@@ -900,34 +917,13 @@ func (repo groupRepository) retrieveGroups(ctx context.Context, domainID, userID
900
917
}
901
918
902
919
cq := fmt .Sprintf (`%s
903
- SELECT COUNT(*) AS total_count
904
- FROM (
905
- SELECT
906
- g.id,
907
- g.name,
908
- g.domain_id,
909
- COALESCE(g.parent_id, '') AS parent_id,
910
- g.description,
911
- g.tags,
912
- g.metadata,
913
- g.created_at,
914
- g.updated_at,
915
- g.updated_by,
916
- g.status,
917
- g.path as path,
918
- g.role_id,
919
- g.role_name,
920
- g.actions,
921
- g.access_type,
922
- g.access_provider_id,
923
- g.access_provider_role_id,
924
- g.access_provider_role_name,
925
- g.access_provider_role_actions
926
- FROM
927
- final_groups g
928
- %s
929
- ) AS subquery;
930
- ` , baseQuery , query )
920
+ SELECT COUNT(*) AS total_count
921
+ FROM (
922
+ SELECT g.id
923
+ FROM final_groups g
924
+ %s
925
+ ) AS subquery;` ,
926
+ baseQuery , query )
931
927
932
928
total , err := postgres .Total (ctx , repo .db , cq , dbPageMeta )
933
929
if err != nil {
0 commit comments