2018-07-03 08:26:28 +02:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-07-25 16:24:46 +02:00
|
|
|
"code.vikunja.io/api/models"
|
2018-07-03 08:26:28 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
2018-07-12 23:36:55 +02:00
|
|
|
"strconv"
|
2018-07-03 08:26:28 +02:00
|
|
|
)
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// GetListsByNamespaceID is the web handler to delete a namespace
|
2018-07-03 08:26:28 +02:00
|
|
|
func GetListsByNamespaceID(c echo.Context) error {
|
2018-07-03 08:48:28 +02:00
|
|
|
// swagger:operation GET /namespaces/{namespaceID}/lists namespaces getListsByNamespace
|
2018-07-03 08:26:28 +02:00
|
|
|
// ---
|
|
|
|
// summary: gets all lists belonging to that namespace
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: namespaceID
|
|
|
|
// in: path
|
|
|
|
// description: ID of the namespace
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Namespace"
|
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
// "500":
|
|
|
|
// "$ref": "#/responses/Message"
|
|
|
|
|
2018-07-04 08:56:52 +02:00
|
|
|
// Get our namespace
|
|
|
|
namespace, err := getNamespace(c)
|
2018-07-03 08:26:28 +02:00
|
|
|
if err != nil {
|
2018-07-04 08:56:52 +02:00
|
|
|
if models.IsErrNamespaceDoesNotExist(err) {
|
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."})
|
|
|
|
}
|
|
|
|
if models.IsErrUserDoesNotHaveAccessToNamespace(err) {
|
|
|
|
return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."})
|
|
|
|
}
|
2018-07-03 08:26:28 +02:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the lists
|
2018-07-04 08:56:52 +02:00
|
|
|
lists, err := models.GetListsByNamespaceID(namespace.ID)
|
2018-07-03 08:26:28 +02:00
|
|
|
if err != nil {
|
|
|
|
if models.IsErrNamespaceDoesNotExist(err) {
|
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."})
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, lists)
|
2018-07-03 08:48:28 +02:00
|
|
|
}
|
2018-07-12 23:36:55 +02:00
|
|
|
|
|
|
|
func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
|
|
|
|
// Check if we have our ID
|
2018-09-06 18:55:58 +02:00
|
|
|
id := c.Param("namespace")
|
2018-07-12 23:36:55 +02:00
|
|
|
// Make int
|
|
|
|
namespaceID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the namespace
|
|
|
|
namespace, err = models.GetNamespaceByID(namespaceID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user has acces to that namespace
|
|
|
|
user, err := models.GetCurrentUser(c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !namespace.CanRead(&user) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|