brunel/handlers/packages/packages.go
2024-07-28 19:59:50 +01:00

71 lines
1.8 KiB
Go

package handlers_packages
import (
"brunel/domain"
"brunel/fastmap"
"brunel/packages"
"strings"
"github.com/gofiber/fiber/v2"
)
func Packages(c *fiber.Ctx) error {
pageNum := c.QueryInt("page")
pageSize := c.QueryInt("pageSize")
search := c.Query("search")
filter := c.Query("filter")
if pageNum == 0 {
pageNum = 1
}
if pageSize == 0 {
pageSize = 250
}
packs := packages.GetPackages()
if filter != "" {
finalReturn := fastmap.New[string, domain.SourcePackage]()
filteredPacks := 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) {
filteredPacks.Set(k, source)
return false
}
return true
})
return true
})
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)
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))
}