api/pkg/routes/api/v1/list_by_namespace.go

91 lines
2.6 KiB
Go
Raw Normal View History

// Vikunja is a todo-list application to facilitate your life.
2020-01-09 17:33:22 +00:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-11-26 20:17:33 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-11-26 20:17:33 +00:00
//
// 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 General Public License for more details.
2018-11-26 20:17:33 +00:00
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-11-26 20:17:33 +00:00
package v1
import (
2018-10-31 12:42:38 +00:00
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web/handler"
2019-05-07 19:42:24 +00:00
"github.com/labstack/echo/v4"
"net/http"
2018-07-12 21:36:55 +00:00
"strconv"
)
2018-07-10 12:02:23 +00:00
// GetListsByNamespaceID is the web handler to delete a namespace
// TODO: depricate this in favour of namespace.ReadOne() <-- should also return the lists
// @Summary Get all lists in a namespace
// @Description Returns all lists inside of a namespace.
// @tags namespace
// @Accept json
// @Produce json
// @Param id path int true "Namespace ID"
2019-01-03 22:22:06 +00:00
// @Security JWTKeyAuth
// @Success 200 {array} models.List "The lists."
// @Failure 403 {object} models.Message "No access to that namespace."
// @Failure 404 {object} models.Message "The namespace does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /namespaces/{id}/lists [get]
func GetListsByNamespaceID(c echo.Context) error {
// Get our namespace
namespace, err := getNamespace(c)
if err != nil {
return handler.HandleHTTPError(err, c)
}
// Get the lists
doer, err := user.GetCurrentUser(c)
if err != nil {
return handler.HandleHTTPError(err, c)
}
lists, err := models.GetListsByNamespaceID(namespace.ID, doer)
if err != nil {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, lists)
2018-07-03 06:48:28 +00:00
}
2018-07-12 21:36:55 +00:00
func getNamespace(c echo.Context) (namespace *models.Namespace, err error) {
2018-07-12 21:36:55 +00:00
// Check if we have our ID
id := c.Param("namespace")
2018-07-12 21:36:55 +00:00
// Make int
namespaceID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return
}
if namespaceID == -1 {
namespace = &models.PseudoNamespace
return
}
2018-07-12 21:36:55 +00:00
// Check if the user has acces to that namespace
user, err := user.GetCurrentUser(c)
2018-07-12 21:36:55 +00:00
if err != nil {
return
}
2019-10-16 20:52:29 +00:00
namespace = &models.Namespace{ID: namespaceID}
2019-03-24 12:35:50 +00:00
canRead, err := namespace.CanRead(user)
if err != nil {
return namespace, err
}
if !canRead {
return nil, echo.ErrForbidden
2018-07-12 21:36:55 +00:00
}
return
}