73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package handlers_packages
|
|
|
|
import (
|
|
"brunel/domain"
|
|
"brunel/fastmap"
|
|
"brunel/packages"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func Packages(c *fiber.Ctx) error {
|
|
pageNum := c.QueryInt("page", 1) // Default to 1 if not provided
|
|
pageSize := c.QueryInt("pageSize", 250) // Default to 250 if not provided
|
|
search := strings.ToLower(c.Query("search"))
|
|
filter := c.Query("filter")
|
|
|
|
// Adjust pageNum to be 0-based for GetPage
|
|
adjustedPageNum := pageNum - 1
|
|
if adjustedPageNum < 0 {
|
|
adjustedPageNum = 0
|
|
}
|
|
|
|
packs := packages.GetPackages()
|
|
|
|
// 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]()
|
|
|
|
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 matchesFilter && matchesSearch {
|
|
finalReturn.Set(k, source)
|
|
}
|
|
}
|
|
|
|
result := finalReturn.GetPage(adjustedPageNum, pageSize)
|
|
|
|
return c.Status(fiber.StatusOK).JSON(result)
|
|
}
|