add sorting; implement sort criteria in default repo ; adding html header

This commit is contained in:
2025-01-01 23:28:12 +01:00
parent db91ebeece
commit fe9ac81281
6 changed files with 43 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package data
import (
"encoding/json"
"errors"
"slices"
"strings"
)
@@ -111,7 +112,24 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
return obj, nil
}
func (repo *DefaultRepository[T]) GetByCriteria(c ISearchCriteria[T]) ([]T, error) {
// TODO
return nil, nil
// Returns a slice of all elememts in the repo sort by the given an
// ISearchCriteria. Throws an error when the elememts cannot be retrieved from
// the repo.
func (repo *DefaultRepository[T]) GetByCriteria(c ISortCriteria[T]) ([]T, error) {
all, err := repo.GetAll()
if err != nil { return nil, err }
slices.SortFunc(all, func(a, b T) int {
wa, wb := c.Weight(a), c.Weight(b)
switch {
case wa > wb:
return 1
case wa < wb:
return -1
default:
return 0
}
})
return all, nil
}

View File

@@ -9,5 +9,5 @@ type IRepository[T IIdentifiable] interface {
Delete(t T) error
GetAll() ([]T, error)
GetById(id string) (T, error)
GetByCriteria(c ISearchCriteria[T]) ([]T, error)
GetByCriteria(c ISortCriteria[T]) ([]T, error)
}

View File

@@ -1,6 +1,6 @@
package data
// TODO docstring
type ISearchCriteria[T any] interface {
Matches(t T) bool
type ISortCriteria[T any] interface {
Weight(t T) int
}