add search
This commit is contained in:
@@ -112,24 +112,23 @@ func (repo *DefaultRepository[T]) GetById(id string) (T, error) {
|
||||
return obj, 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.
|
||||
// Returns a slice of all elememts in the repo that have a
|
||||
// ISearchCriteria.Weight greater than 0 sort by that weight. 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 {
|
||||
filtered := make([]T, 0)
|
||||
for _, elem := range all {
|
||||
if c.Weight(elem) > 0 { filtered = append(filtered, elem) }
|
||||
}
|
||||
|
||||
slices.SortFunc(filtered, 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
|
||||
}
|
||||
if wa > wb { return -1 }
|
||||
return 1
|
||||
})
|
||||
|
||||
return all, nil
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user