vikunja-api/vendor/code.vikunja.io/web/handler/read_all.go

113 lines
3.8 KiB
Go
Raw Permalink Normal View History

2018-12-02 19:30:42 +01:00
// Copyright (c) 2018 Vikunja and contributors.
2018-11-26 21:17:33 +01:00
//
// This program is free software: you can redistribute it and/or modify
2018-12-02 19:30:42 +01:00
// it under the terms of the GNU Lesser General Public License as published by
2018-11-26 21:17:33 +01:00
// 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
2018-12-02 19:30:42 +01:00
// GNU Lesser General Public License for more details.
2018-11-26 21:17:33 +01:00
//
2018-12-02 19:30:42 +01:00
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-11-26 21:17:33 +01:00
2018-12-01 00:26:56 +01:00
package handler
2018-07-11 01:45:11 +02:00
import (
"fmt"
2019-05-07 21:42:24 +02:00
"github.com/labstack/echo/v4"
2019-10-23 23:11:40 +02:00
"math"
"net/http"
2018-11-09 11:30:17 +01:00
"strconv"
2018-07-11 01:45:11 +02:00
)
// 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()
2019-03-24 10:13:40 +01:00
currentAuth, err := config.AuthProvider.AuthObject(ctx)
2018-07-11 01:45:11 +02:00
if err != nil {
2018-07-11 14:27:16 +02:00
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
2018-07-11 01:45:11 +02:00
}
// Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))
}
2018-11-09 11:30:17 +01:00
// Pagination
page := ctx.QueryParam("page")
if page == "" {
page = "1"
}
pageNumber, err := strconv.Atoi(page)
if err != nil {
2019-03-24 10:13:40 +01:00
config.LoggingProvider.Error(err.Error())
2018-11-09 11:30:17 +01:00
return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.")
}
if pageNumber < 0 {
2019-10-23 23:11:40 +02:00
return echo.NewHTTPError(http.StatusBadRequest, "Page number cannot be negative.")
}
// Items per page
var perPageNumber int
perPage := ctx.QueryParam("per_page")
// If we dont have an "items per page" parameter, we want to use the default.
// To prevent Atoi from failing, we check this here.
if perPage != "" {
perPageNumber, err = strconv.Atoi(perPage)
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, "Per page amount cannot be negative.")
}
if perPageNumber > config.MaxItemsPerPage {
perPageNumber = config.MaxItemsPerPage
2018-11-09 11:30:17 +01:00
}
// Search
search := ctx.QueryParam("s")
2019-10-23 23:11:40 +02:00
result, resultCount, numberOfItems, err := currentStruct.ReadAll(currentAuth, search, pageNumber, perPageNumber)
2018-07-11 01:45:11 +02:00
if err != nil {
2018-12-01 00:26:56 +01:00
return HandleHTTPError(err, ctx)
2018-07-11 01:45:11 +02:00
}
2019-10-23 23:11:40 +02:00
// Calculate the number of pages from the number of items
// We always round up, because if we don't have a number of items which is exactly dividable by the number of items per page,
// we would get a result that is one page off.
var numberOfPages = math.Ceil(float64(numberOfItems) / float64(perPageNumber))
// If we return all results, we only have one page
if pageNumber < 0 {
numberOfPages = 1
}
// If we don't have results, we don't have a page
if resultCount == 0 {
numberOfPages = 0
}
ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatFloat(numberOfPages, 'f', 0, 64))
ctx.Response().Header().Set("x-pagination-result-count", strconv.FormatInt(int64(resultCount), 10))
ctx.Response().Header().Set("Access-Control-Expose-Headers", "x-pagination-total-pages, x-pagination-result-count")
err = ctx.JSON(http.StatusOK, result)
if err != nil {
return HandleHTTPError(err, ctx)
}
return err
}