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
packSlice := make([]struct {
Key string
Value domain.SourcePackage
}, 0, packs.Len())
packs.Iter(func(k string, v domain.SourcePackage) bool {
packSlice = append(packSlice, struct {
Key string
Value domain.SourcePackage
}{k, v})
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]() finalReturn := fastmap.New[string, domain.SourcePackage]()
filteredPacks := fastmap.New[string, domain.SourcePackage]()
packs.Iter(func(k string, source domain.SourcePackage) bool { 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 { source.Packages.Iter(func(key string, value domain.PackageInfo) bool {
if value.Status == domain.PackageStatus(filter) { if !matchesFilter && value.Status == domain.PackageStatus(filter) {
filteredPacks.Set(k, source) matchesFilter = true
return false
} }
return true if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
matchesSearch = true
}
return !(matchesFilter && matchesSearch) // stop iterating if we've found matches for both
}) })
return true
}) if matchesFilter && matchesSearch {
if search != "" {
filteredPacks.Iter(func(k string, source domain.SourcePackage) bool {
source.Packages.Iter(func(key string, value domain.PackageInfo) bool {
if strings.Contains(key, search) {
finalReturn.Set(k, source) 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)
} }