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

View File

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