From 2e20832213f4dfe977df7ccce1397d66531e0c4e Mon Sep 17 00:00:00 2001 From: zebrapurring <> Date: Thu, 12 Dec 2024 23:25:49 +0100 Subject: [PATCH 1/2] fix: sorting for completed and scheduled maintenance entries * Completed entries are sorted by descending completion date * Scheduled entries are sorted by ascending scheduling date * Selecting both entries shall sort them by scheduling and then completion date in descending order --- .../data/repo/repo_maintenance_entry.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/internal/data/repo/repo_maintenance_entry.go b/backend/internal/data/repo/repo_maintenance_entry.go index b3e98349e..368b9f13f 100644 --- a/backend/internal/data/repo/repo_maintenance_entry.go +++ b/backend/internal/data/repo/repo_maintenance_entry.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "entgo.io/ent/dialect/sql" "github.com/google/uuid" "github.com/sysadminsmedia/homebox/backend/internal/data/ent" "github.com/sysadminsmedia/homebox/backend/internal/data/ent/group" @@ -136,6 +137,10 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context, maintenanceentry.DateEQ(time.Time{}), maintenanceentry.DateGT(time.Now()), )) + // Sort scheduled entries by ascending scheduled date + query = query.Order( + maintenanceentry.ByScheduledDate(sql.OrderAsc()), + ) } else if filters.Status == MaintenanceFilterStatusCompleted { query = query.Where( maintenanceentry.Not(maintenanceentry.Or( @@ -143,8 +148,18 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context, maintenanceentry.DateEQ(time.Time{}), maintenanceentry.DateGT(time.Now()), ))) + // Sort completed entries by descending completion date + query = query.Order( + maintenanceentry.ByDate(sql.OrderDesc()), + ) + } else { + // Sort entries by default by scheduled and maintenance date in descending order + query = query.Order( + maintenanceentry.ByScheduledDate(sql.OrderDesc()), + maintenanceentry.ByDate(sql.OrderDesc()), + ) } - entries, err := query.WithItem().Order(maintenanceentry.ByScheduledDate()).All(ctx) + entries, err := query.WithItem().All(ctx) if err != nil { return []MaintenanceEntryWithDetails{}, err From c5ffa0ead90cfb44e76393b45616b750b37fea66 Mon Sep 17 00:00:00 2001 From: zebrapurring <> Date: Fri, 13 Dec 2024 00:00:14 +0100 Subject: [PATCH 2/2] refactor: rewrite if-else as switch-case --- backend/internal/data/repo/repo_maintenance_entry.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/internal/data/repo/repo_maintenance_entry.go b/backend/internal/data/repo/repo_maintenance_entry.go index 368b9f13f..ed3a06c4f 100644 --- a/backend/internal/data/repo/repo_maintenance_entry.go +++ b/backend/internal/data/repo/repo_maintenance_entry.go @@ -131,7 +131,8 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context, item.HasGroupWith(group.IDEQ(groupID)), ), ) - if filters.Status == MaintenanceFilterStatusScheduled { + switch filters.Status { + case MaintenanceFilterStatusScheduled: query = query.Where(maintenanceentry.Or( maintenanceentry.DateIsNil(), maintenanceentry.DateEQ(time.Time{}), @@ -141,7 +142,7 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context, query = query.Order( maintenanceentry.ByScheduledDate(sql.OrderAsc()), ) - } else if filters.Status == MaintenanceFilterStatusCompleted { + case MaintenanceFilterStatusCompleted: query = query.Where( maintenanceentry.Not(maintenanceentry.Or( maintenanceentry.DateIsNil(), @@ -152,7 +153,7 @@ func (r *MaintenanceEntryRepository) GetMaintenanceByItemID(ctx context.Context, query = query.Order( maintenanceentry.ByDate(sql.OrderDesc()), ) - } else { + default: // Sort entries by default by scheduled and maintenance date in descending order query = query.Order( maintenanceentry.ByScheduledDate(sql.OrderDesc()),