64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package handlers_packages
|
|
|
|
import (
|
|
"brunel/domain"
|
|
"brunel/packages"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type PackagesResponse struct {
|
|
Total int `json:"total"`
|
|
Packages []domain.SourcePackage `json:"packages"`
|
|
}
|
|
|
|
func Packages(c *fiber.Ctx) error {
|
|
pageNum := c.QueryInt("page", 1)
|
|
pageSize := c.QueryInt("pageSize", 250)
|
|
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()
|
|
|
|
finalReturn := make([]domain.SourcePackage, 0)
|
|
packs.ForEach(func(k string, source domain.SourcePackage) bool {
|
|
matchesFilter := filter == ""
|
|
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
|
|
|
|
source.Packages.ForEach(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)
|
|
})
|
|
|
|
if matchesFilter && matchesSearch {
|
|
finalReturn = append(finalReturn, source)
|
|
}
|
|
return true
|
|
})
|
|
sort.Slice(finalReturn, func(i, j int) bool {
|
|
return finalReturn[i].Name < finalReturn[j].Name
|
|
})
|
|
|
|
result := finalReturn[adjustedPageNum*pageSize : (adjustedPageNum+1)*pageSize]
|
|
|
|
response := PackagesResponse{
|
|
Total: len(finalReturn),
|
|
Packages: result,
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(response)
|
|
}
|