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) if !matchesFilter && source.Status == domain.PackageStatus(filter) { matchesFilter = true } if !matchesSearch && strings.Contains(strings.ToLower(k), search) { matchesSearch = true } if matchesFilter && matchesSearch { finalReturn = append(finalReturn, source) } return true }) sort.Slice(finalReturn, func(i, j int) bool { return finalReturn[i].Name < finalReturn[j].Name }) var result []domain.SourcePackage startIndex := adjustedPageNum * pageSize endIndex := (adjustedPageNum + 1) * pageSize if startIndex >= len(finalReturn) { result = []domain.SourcePackage{} } else { if endIndex > len(finalReturn) { endIndex = len(finalReturn) } result = finalReturn[startIndex:endIndex] } response := PackagesResponse{ Total: len(finalReturn), Packages: result, } return c.Status(fiber.StatusOK).JSON(response) }