sort attributes

This commit is contained in:
pluja 2024-02-13 10:42:10 +01:00
parent ecf7c38911
commit f13596e6ac

View File

@ -3,6 +3,7 @@ package database
import (
"fmt"
"os"
"sort"
"github.com/pluja/pocketbase"
)
@ -64,14 +65,25 @@ func (p *PbClient) GetServiceByNameOrUrl(id string) (*Service, error) {
Filters: fmt.Sprintf("((name='%v') || (urls~'%v'))", id, id),
}
// TODO: Sort attributes by rating
response, err := collection.List(params)
if err != nil {
return nil, err
}
return &response.Items[0], nil
service := response.Items[0]
// Sort attributes by rating
sort.SliceStable(service.Expand["attributes"], func(i, j int) bool {
ratingOrder := map[string]int{
"bad": 4,
"warn": 3,
"good": 2,
"info": 1,
}
return ratingOrder[service.Expand["attributes"][i].Rating] > ratingOrder[service.Expand["attributes"][j].Rating]
})
return &service, nil
}
func (p *PbClient) GetServiceById(id string) (*Service, error) {