diff --git a/models/lists.go b/models/lists.go index 240bf113..19ca6dda 100644 --- a/models/lists.go +++ b/models/lists.go @@ -51,7 +51,7 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) { return lists, err } -// ReadAll gets all List a user has access to +// ReadAll gets all lists a user has access to func (l *List) ReadAll(user *User) (interface{}, error) { lists := Lists{} fullUser, _, err := GetUserByID(user.ID) diff --git a/models/namespaces.go b/models/namespaces.go index 59ca578d..be326513 100644 --- a/models/namespaces.go +++ b/models/namespaces.go @@ -105,3 +105,51 @@ func GetNamespaceByID(id int64) (namespace Namespace, err error) { return namespace, err } + +// ReadAll gets all namespaces a user has access to +func (n *Namespace) ReadAll(doer *User) (interface{}, error) { + + all := []*Namespace{} + + // TODO respect individual rights + err := x.Select("namespaces.*"). + Table("namespaces"). + Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id"). + Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id"). + Where("team_members.user_id = ?", doer.ID). + Or("namespaces.owner_id = ?", doer.ID). + GroupBy("namespaces.id"). + Find(&all) + + if err != nil { + return all, err + } + + // Get all users + users := []*User{} + err = x.Select("users.*"). + Table("namespaces"). + Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id"). + Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id"). + Join("INNER", "users", "users.id = namespaces.owner_id"). + Where("team_members.user_id = ?", doer.ID). + Or("namespaces.owner_id = ?", doer.ID). + GroupBy("users.id"). + Find(&users) + + if err != nil { + return all, err + } + + // Put user objects in our namespace list + for i, n := range all { + for _, u := range users { + if n.OwnerID == u.ID { + all[i].Owner = *u + break + } + } + } + + return all, nil +} diff --git a/models/namespaces_add_update.go b/models/namespaces_add_update.go index 67e7ba99..07efdfe5 100644 --- a/models/namespaces_add_update.go +++ b/models/namespaces_add_update.go @@ -30,29 +30,3 @@ func CreateOrUpdateNamespace(namespace *Namespace) (err error) { return } - -// GetAllNamespacesByUserID does what it says -func GetAllNamespacesByUserID(userID int64) (namespaces []Namespace, err error) { - - // First, get all namespaces which that user owns - err = x.Where("owner_id = ?", userID).Find(&namespaces) - if err != nil { - return namespaces, err - } - - // Get all namespaces of teams that user is part of - /*err = x.Table("namespaces"). - Join("INNER", ). - Find(namespaces)*/ - - // Get user objects - // I couldn't come up with a more performant way to do this... - for in, n := range namespaces { - namespaces[in].Owner, _, err = GetUserByID(n.OwnerID) - if err != nil { - return nil, err - } - } - - return -} diff --git a/routes/api/v1/namespaces_list.go b/routes/api/v1/namespaces_list.go index c641f14a..c13c7bc3 100644 --- a/routes/api/v1/namespaces_list.go +++ b/routes/api/v1/namespaces_list.go @@ -1,7 +1,6 @@ package v1 import ( - "git.kolaente.de/konrad/list/models" "github.com/labstack/echo" "net/http" ) @@ -21,15 +20,18 @@ func GetAllNamespacesByCurrentUser(c echo.Context) error { // "500": // "$ref": "#/responses/Message" - user, err := models.GetCurrentUser(c) - if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get the current user."}) - } + return echo.NewHTTPError(http.StatusNotImplemented) + /* - namespaces, err := models.GetAllNamespacesByUserID(user.ID) - if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"Could not get namespaces."}) - } + user, err := models.GetCurrentUser(c) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get the current user."}) + } - return c.JSON(http.StatusOK, namespaces) + namespaces, err := models.GetAllNamespacesByUserID(user.ID) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"Could not get namespaces."}) + } + + return c.JSON(http.StatusOK, namespaces)*/ } diff --git a/routes/crud/read_all.go b/routes/crud/read_all.go index 188c6961..770db644 100644 --- a/routes/crud/read_all.go +++ b/routes/crud/read_all.go @@ -1,7 +1,6 @@ package crud import ( - "fmt" "git.kolaente.de/konrad/list/models" "github.com/labstack/echo" "net/http" @@ -16,7 +15,6 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { lists, err := c.CObject.ReadAll(¤tUser) if err != nil { - fmt.Println(err) return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."}) } diff --git a/routes/routes.go b/routes/routes.go index 6f37d308..d8b56b59 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -103,7 +103,10 @@ func RegisterRoutes(e *echo.Echo) { a.DELETE("/items/:id", itemHandler.DeleteWeb) a.POST("/items/:id", itemHandler.UpdateWeb) - a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser) + namespaceHandler := &crud.WebHandler{ + CObject: &models.Namespace{}, + } + a.GET("/namespaces", namespaceHandler.ReadAllWeb) a.PUT("/namespaces", apiv1.AddNamespace) a.GET("/namespaces/:id", apiv1.ShowNamespace) a.POST("/namespaces/:id", apiv1.UpdateNamespace)