List handles GET /packages.
(w http.ResponseWriter, r *http.Request)
| 20 | |
| 21 | // List handles GET /packages. |
| 22 | func (h *PackagesHandler) List(w http.ResponseWriter, r *http.Request) { |
| 23 | q := r.URL.Query() |
| 24 | page, _ := strconv.Atoi(q.Get("page")) |
| 25 | if page <= 0 { |
| 26 | page = 1 |
| 27 | } |
| 28 | limit, _ := strconv.Atoi(q.Get("limit")) |
| 29 | if limit <= 0 { |
| 30 | limit = 50 |
| 31 | } |
| 32 | if limit > 10000 { |
| 33 | limit = 10000 |
| 34 | } |
| 35 | search := q.Get("search") |
| 36 | if len(search) > 200 { |
| 37 | search = search[:200] |
| 38 | } |
| 39 | params := store.ListParams{ |
| 40 | Page: page, |
| 41 | Limit: limit, |
| 42 | Search: search, |
| 43 | Category: q.Get("category"), |
| 44 | NeedsUpdate: q.Get("needsUpdate"), |
| 45 | IsSecurityUpdate: q.Get("isSecurityUpdate"), |
| 46 | Host: q.Get("host"), |
| 47 | Repository: q.Get("repository"), |
| 48 | } |
| 49 | pkgs, total, err := h.packages.List(r.Context(), params) |
| 50 | if err != nil { |
| 51 | Error(w, http.StatusInternalServerError, "Failed to load packages") |
| 52 | return |
| 53 | } |
| 54 | pages := (total + params.Limit - 1) / params.Limit |
| 55 | if pages < 1 { |
| 56 | pages = 1 |
| 57 | } |
| 58 | JSON(w, http.StatusOK, map[string]interface{}{ |
| 59 | "packages": pkgs, |
| 60 | "pagination": map[string]interface{}{ |
| 61 | "page": params.Page, |
| 62 | "limit": params.Limit, |
| 63 | "total": total, |
| 64 | "pages": pages, |
| 65 | }, |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | // GetByID handles GET /packages/:packageId. |
| 70 | func (h *PackagesHandler) GetByID(w http.ResponseWriter, r *http.Request) { |