Skip to content

Commit

Permalink
fix: sorting for completed and scheduled maintenance entries (#384)
Browse files Browse the repository at this point in the history
* 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

* refactor: rewrite if-else as switch-case

---------

Co-authored-by: zebrapurring <>
  • Loading branch information
zebrapurring authored Dec 13, 2024
1 parent 90adeb0 commit 83c57ed
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions backend/internal/data/repo/repo_maintenance_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -130,21 +131,36 @@ 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{}),
maintenanceentry.DateGT(time.Now()),
))
} else if filters.Status == MaintenanceFilterStatusCompleted {
// Sort scheduled entries by ascending scheduled date
query = query.Order(
maintenanceentry.ByScheduledDate(sql.OrderAsc()),
)
case MaintenanceFilterStatusCompleted:
query = query.Where(
maintenanceentry.Not(maintenanceentry.Or(
maintenanceentry.DateIsNil(),
maintenanceentry.DateEQ(time.Time{}),
maintenanceentry.DateGT(time.Now()),
)))
// Sort completed entries by descending completion date
query = query.Order(
maintenanceentry.ByDate(sql.OrderDesc()),
)
default:
// 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
Expand Down

0 comments on commit 83c57ed

Please sign in to comment.