add sorting; implement sort criteria in default repo ; adding html header
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user