Fix to search logic

This commit is contained in:
ferreo 2024-07-29 00:46:05 +01:00
parent e1c49febd0
commit dd2e4bfab5

View File

@ -4,67 +4,69 @@ import (
"brunel/domain" "brunel/domain"
"brunel/fastmap" "brunel/fastmap"
"brunel/packages" "brunel/packages"
"sort"
"strings" "strings"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func Packages(c *fiber.Ctx) error { func Packages(c *fiber.Ctx) error {
pageNum := c.QueryInt("page") pageNum := c.QueryInt("page", 1) // Default to 1 if not provided
pageSize := c.QueryInt("pageSize") pageSize := c.QueryInt("pageSize", 250) // Default to 250 if not provided
search := c.Query("search") search := strings.ToLower(c.Query("search"))
filter := c.Query("filter") filter := c.Query("filter")
if pageNum == 0 {
pageNum = 1 // Adjust pageNum to be 0-based for GetPage
} adjustedPageNum := pageNum - 1
if pageSize == 0 { if adjustedPageNum < 0 {
pageSize = 250 adjustedPageNum = 0
} }
packs := packages.GetPackages() packs := packages.GetPackages()
if filter != "" { // Convert map to slice for sorting
finalReturn := fastmap.New[string, domain.SourcePackage]() packSlice := make([]struct {
filteredPacks := fastmap.New[string, domain.SourcePackage]() Key string
packs.Iter(func(k string, source domain.SourcePackage) bool { Value domain.SourcePackage
source.Packages.Iter(func(key string, value domain.PackageInfo) bool { }, 0, packs.Len())
if value.Status == domain.PackageStatus(filter) {
filteredPacks.Set(k, source) packs.Iter(func(k string, v domain.SourcePackage) bool {
return false packSlice = append(packSlice, struct {
} Key string
return true Value domain.SourcePackage
}) }{k, v})
return true return true
})
// Stable sort the slice
sort.SliceStable(packSlice, func(i, j int) bool {
return packSlice[i].Key < packSlice[j].Key
})
finalReturn := fastmap.New[string, domain.SourcePackage]()
for _, item := range packSlice {
k, source := item.Key, item.Value
matchesFilter := filter == ""
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
source.Packages.Iter(func(key string, value domain.PackageInfo) bool {
if !matchesFilter && value.Status == domain.PackageStatus(filter) {
matchesFilter = true
}
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
matchesSearch = true
}
return !(matchesFilter && matchesSearch) // stop iterating if we've found matches for both
}) })
if search != "" {
filteredPacks.Iter(func(k string, source domain.SourcePackage) bool { if matchesFilter && matchesSearch {
source.Packages.Iter(func(key string, value domain.PackageInfo) bool { finalReturn.Set(k, source)
if strings.Contains(key, search) {
finalReturn.Set(k, source)
return false
}
return true
})
return true
})
} else {
finalReturn = filteredPacks
} }
return c.Status(fiber.StatusOK).JSON(finalReturn.GetPage(pageNum, pageSize))
}
if search != "" {
finalReturn := fastmap.New[string, domain.SourcePackage]()
packs.Iter(func(k string, source domain.SourcePackage) bool {
source.Packages.Iter(func(key string, value domain.PackageInfo) bool {
if value.Status == domain.PackageStatus(filter) {
finalReturn.Set(k, source)
return false
}
return true
})
return true
})
return c.Status(fiber.StatusOK).JSON(finalReturn.GetPage(pageNum, pageSize))
} }
return c.Status(fiber.StatusOK).JSON(packs.GetPage(pageNum, pageSize)) result := finalReturn.GetPage(adjustedPageNum, pageSize)
return c.Status(fiber.StatusOK).JSON(result)
} }