// Copyright (c) 2018 Vikunja and contributors. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . package handler import ( "github.com/labstack/echo/v4" "net/http" "strconv" ) // ReadAllWeb is the webhandler to get all objects of a type func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { // Get our model currentStruct := c.EmptyStruct() currentAuth, err := config.AuthProvider.AuthObject(ctx) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") } // Get the object & bind params to struct if err := ctx.Bind(currentStruct); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.") } // Pagination page := ctx.QueryParam("page") if page == "" { page = "1" } pageNumber, err := strconv.ParseInt(page, 10, 64) if err != nil { config.LoggingProvider.Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.") } if pageNumber < 0 { return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.") } // Items per page perPage := ctx.QueryParam("per_page") perPageNumber, err := strconv.ParseInt(perPage, 10, 64) if err != nil { config.LoggingProvider.Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Bad per page amount requested.") } // Set default page count if perPageNumber == 0 { perPageNumber = config.MaxItemsPerPage } if perPageNumber < 1 { return echo.NewHTTPError(http.StatusBadRequest, "Bad per page amount requested.") } if perPageNumber > config.MaxItemsPerPage { perPageNumber = config.MaxItemsPerPage } // Search search := ctx.QueryParam("s") result, resultCount, numberOfPages, err := currentStruct.ReadAll(currentAuth, search, pageNumber, perPageNumber) if err != nil { return HandleHTTPError(err, ctx) } ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatInt(numberOfPages, 10)) ctx.Response().Header().Set("x-pagination-result-count", strconv.FormatInt(resultCount, 10)) ctx.Response().Header().Set("Access-Control-Expose-Headers", "x-pagination-total-pages, x-pagination-result-count") return ctx.JSON(http.StatusOK, result) }