Map to slice

This commit is contained in:
ferreo 2024-07-30 02:07:55 +01:00
parent 57f1d0d6af
commit f4487c0040
2 changed files with 22 additions and 17 deletions

View File

@ -3,7 +3,7 @@ package handlers_build
import ( import (
"brunel/buildqueue" "brunel/buildqueue"
"brunel/domain" "brunel/domain"
"brunel/fastmap" "sort"
"strings" "strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -11,7 +11,7 @@ import (
type QueueResponse struct { type QueueResponse struct {
Total int `json:"total"` Total int `json:"total"`
Packages *fastmap.Fastmap[string, domain.BuildQueueItem] `json:"packages"` Packages []domain.BuildQueueItem `json:"packages"`
} }
func Queue(c *fiber.Ctx) error { func Queue(c *fiber.Ctx) error {
@ -27,7 +27,7 @@ func Queue(c *fiber.Ctx) error {
} }
packs := buildqueue.GetQueue() packs := buildqueue.GetQueue()
finalReturn := fastmap.New[string, domain.BuildQueueItem]() finalReturn := make([]domain.BuildQueueItem, 0)
packs.ForEach(func(k string, source domain.BuildQueueItem) bool { packs.ForEach(func(k string, source domain.BuildQueueItem) bool {
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search) matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
@ -40,16 +40,19 @@ func Queue(c *fiber.Ctx) error {
}) })
if matchesFilter && matchesSearch { if matchesFilter && matchesSearch {
finalReturn.Set(k, source) finalReturn = append(finalReturn, source)
} }
return true return true
}) })
finalReturn.StableSortByKey() sort.Slice(finalReturn, func(i, j int) bool {
result := finalReturn.GetPage(adjustedPageNum, pageSize) return finalReturn[i].Source.Name < finalReturn[j].Source.Name
})
result := finalReturn[adjustedPageNum*pageSize : (adjustedPageNum+1)*pageSize]
response := QueueResponse{ response := QueueResponse{
Total: finalReturn.Len(), Total: len(finalReturn),
Packages: result, Packages: result,
} }

View File

@ -2,8 +2,8 @@ package handlers_packages
import ( import (
"brunel/domain" "brunel/domain"
"brunel/fastmap"
"brunel/packages" "brunel/packages"
"sort"
"strings" "strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -11,7 +11,7 @@ import (
type PackagesResponse struct { type PackagesResponse struct {
Total int `json:"total"` Total int `json:"total"`
Packages *fastmap.Fastmap[string, domain.SourcePackage] `json:"packages"` Packages []domain.SourcePackage `json:"packages"`
} }
func Packages(c *fiber.Ctx) error { func Packages(c *fiber.Ctx) error {
@ -28,8 +28,7 @@ func Packages(c *fiber.Ctx) error {
packs := packages.GetPackages() packs := packages.GetPackages()
finalReturn := fastmap.New[string, domain.SourcePackage]() finalReturn := make([]domain.SourcePackage, 0)
packs.ForEach(func(k string, source domain.SourcePackage) bool { packs.ForEach(func(k string, source domain.SourcePackage) bool {
matchesFilter := filter == "" matchesFilter := filter == ""
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search) matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
@ -45,15 +44,18 @@ func Packages(c *fiber.Ctx) error {
}) })
if matchesFilter && matchesSearch { if matchesFilter && matchesSearch {
finalReturn.Set(k, source) finalReturn = append(finalReturn, source)
} }
return true return true
}) })
finalReturn.StableSortByKey() sort.Slice(finalReturn, func(i, j int) bool {
result := finalReturn.GetPage(adjustedPageNum, pageSize) return finalReturn[i].Name < finalReturn[j].Name
})
result := finalReturn[adjustedPageNum*pageSize : (adjustedPageNum+1)*pageSize]
response := PackagesResponse{ response := PackagesResponse{
Total: finalReturn.Len(), Total: len(finalReturn),
Packages: result, Packages: result,
} }