From f13596e6ac6fae4475e9a0c4c76f66005ddc806a Mon Sep 17 00:00:00 2001 From: pluja Date: Tue, 13 Feb 2024 10:42:10 +0100 Subject: [PATCH] sort attributes --- src/database/pocketbase.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/database/pocketbase.go b/src/database/pocketbase.go index 6951d19..0b58817 100644 --- a/src/database/pocketbase.go +++ b/src/database/pocketbase.go @@ -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) {