Refactored getting all lists for a namespace (#100)
This commit is contained in:
parent
8d5a2685c4
commit
694fc62495
2 changed files with 8 additions and 20 deletions
|
@ -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
|
* [x] Middleware to have configurable rate-limiting per user
|
||||||
* [ ] Colors for lists and namespaces -> Up to the frontend to implement these
|
* [ ] Colors for lists and namespaces -> Up to the frontend to implement these
|
||||||
* [ ] Reminders via mail
|
* [ ] Reminders via mail
|
||||||
* [ ] Be able to "really" delete the account -> delete all lists and move ownership for others
|
* [ ] 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
|
|
||||||
* [ ] All `ReadAll` methods should return the number of items per page, the number of items on this page, the total pages and the items
|
* [ ] 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.
|
-> Check if there's a way to do that efficently. Maybe only implementing it in the web handler.
|
||||||
* [ ] Move lists between namespaces -> Extra endpoint
|
* [ ] Move lists between namespaces -> Extra endpoint
|
||||||
|
|
|
@ -18,6 +18,7 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/models"
|
"code.vikunja.io/api/pkg/models"
|
||||||
|
"code.vikunja.io/web/handler"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -41,32 +42,23 @@ func GetListsByNamespaceID(c echo.Context) error {
|
||||||
// Get our namespace
|
// Get our namespace
|
||||||
namespace, err := getNamespace(c)
|
namespace, err := getNamespace(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrNamespaceDoesNotExist(err) {
|
return handler.HandleHTTPError(err, c)
|
||||||
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."})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the lists
|
// Get the lists
|
||||||
doer, err := models.GetCurrentUser(c)
|
doer, err := models.GetCurrentUser(c)
|
||||||
if err != nil {
|
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)
|
lists, err := models.GetListsByNamespaceID(namespace.ID, doer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrNamespaceDoesNotExist(err) {
|
return handler.HandleHTTPError(err, c)
|
||||||
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."})
|
|
||||||
}
|
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occurred."})
|
|
||||||
}
|
}
|
||||||
return c.JSON(http.StatusOK, lists)
|
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
|
// Check if we have our ID
|
||||||
id := c.Param("namespace")
|
id := c.Param("namespace")
|
||||||
// Make int
|
// Make int
|
||||||
|
@ -76,7 +68,7 @@ func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if namespaceID == -1 {
|
if namespaceID == -1 {
|
||||||
namespace = models.PseudoNamespace
|
namespace = &models.PseudoNamespace
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,10 +83,7 @@ func getNamespace(c echo.Context) (namespace models.Namespace, err error) {
|
||||||
return namespace, err
|
return namespace, err
|
||||||
}
|
}
|
||||||
if !canRead {
|
if !canRead {
|
||||||
return
|
return nil, echo.ErrForbidden
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the namespace
|
|
||||||
err = namespace.ReadOne()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue