Refactored getting all lists for a namespace (#100)

This commit is contained in:
konrad 2019-09-20 16:16:31 +00:00 committed by Gitea
parent 8d5a2685c4
commit 694fc62495
2 changed files with 8 additions and 20 deletions

View file

@ -158,8 +158,7 @@ Sorry for some of them being in German, I'll tranlate them at some point.
* [x] Middleware to have configurable rate-limiting per user
* [ ] Colors for lists and namespaces -> Up to the frontend to implement these
* [ ] Reminders via mail
* [ ] Be able to "really" delete the account -> delete all lists and move ownership for others
* [ ] Deprecate /namespaces/{id}/lists in favour of namespace.ReadOne() <-- should also return the lists
* [ ] Be able to "really" delete the account -> delete all lists and move ownership for others
* [ ] All `ReadAll` methods should return the number of items per page, the number of items on this page, the total pages and the items
-> Check if there's a way to do that efficently. Maybe only implementing it in the web handler.
* [ ] Move lists between namespaces -> Extra endpoint

View file

@ -18,6 +18,7 @@ package v1
import (
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/web/handler"
"github.com/labstack/echo/v4"
"net/http"
"strconv"
@ -41,32 +42,23 @@ func GetListsByNamespaceID(c echo.Context) error {
// Get our namespace
namespace, err := getNamespace(c)
if err != nil {
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."})
}
return c.JSON(http.StatusInternalServerError, models.Message{"An error occurred."})
return handler.HandleHTTPError(err, c)
}
// Get the lists
doer, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occurred."})
return handler.HandleHTTPError(err, c)
}
lists, err := models.GetListsByNamespaceID(namespace.ID, doer)
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 occurred."})
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, lists)
}
func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
func getNamespace(c echo.Context) (namespace *models.Namespace, err error) {
// Check if we have our ID
id := c.Param("namespace")
// Make int
@ -76,7 +68,7 @@ func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
}
if namespaceID == -1 {
namespace = models.PseudoNamespace
namespace = &models.PseudoNamespace
return
}
@ -91,10 +83,7 @@ func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
return namespace, err
}
if !canRead {
return
return nil, echo.ErrForbidden
}
// Get the namespace
err = namespace.ReadOne()
return
}