brunel/handlers/packages/packages.go

62 lines
1.5 KiB
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package handlers_packages
import (
"brunel/domain"
"brunel/fastmap"
"brunel/packages"
"strings"
"github.com/gofiber/fiber/v2"
)
2024-07-29 17:39:11 +02:00
type PackagesResponse struct {
Total int `json:"total"`
Packages *fastmap.Fastmap[string, domain.SourcePackage] `json:"packages"`
}
2024-07-28 20:59:50 +02:00
func Packages(c *fiber.Ctx) error {
2024-07-29 17:39:11 +02:00
pageNum := c.QueryInt("page", 1)
pageSize := c.QueryInt("pageSize", 250)
2024-07-29 01:46:05 +02:00
search := strings.ToLower(c.Query("search"))
2024-07-28 20:59:50 +02:00
filter := c.Query("filter")
2024-07-29 01:46:05 +02:00
// Adjust pageNum to be 0-based for GetPage
adjustedPageNum := pageNum - 1
if adjustedPageNum < 0 {
adjustedPageNum = 0
2024-07-28 20:59:50 +02:00
}
2024-07-29 01:46:05 +02:00
2024-07-28 20:59:50 +02:00
packs := packages.GetPackages()
2024-07-29 01:46:05 +02:00
finalReturn := fastmap.New[string, domain.SourcePackage]()
2024-07-30 02:24:18 +02:00
packs.ForEach(func(k string, source domain.SourcePackage) bool {
2024-07-29 01:46:05 +02:00
matchesFilter := filter == ""
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
2024-07-30 02:24:18 +02:00
source.Packages.ForEach(func(key string, value domain.PackageInfo) bool {
2024-07-29 01:46:05 +02:00
if !matchesFilter && value.Status == domain.PackageStatus(filter) {
matchesFilter = true
}
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
matchesSearch = true
}
return !(matchesFilter && matchesSearch)
2024-07-28 20:59:50 +02:00
})
2024-07-29 01:46:05 +02:00
if matchesFilter && matchesSearch {
finalReturn.Set(k, source)
2024-07-28 20:59:50 +02:00
}
return true
})
2024-07-30 03:02:05 +02:00
finalReturn.StableSortByKey()
2024-07-29 01:46:05 +02:00
result := finalReturn.GetPage(adjustedPageNum, pageSize)
2024-07-29 17:39:11 +02:00
response := PackagesResponse{
Total: finalReturn.Len(),
Packages: result,
}
return c.Status(fiber.StatusOK).JSON(response)
2024-07-28 20:59:50 +02:00
}