implemented get all namespaces via interface

This commit is contained in:
konrad 2018-07-11 13:00:00 +02:00 committed by kolaente
parent 5c4fb7ed73
commit 4213f3b08c
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 65 additions and 40 deletions

View file

@ -51,7 +51,7 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
return lists, err 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) { func (l *List) ReadAll(user *User) (interface{}, error) {
lists := Lists{} lists := Lists{}
fullUser, _, err := GetUserByID(user.ID) fullUser, _, err := GetUserByID(user.ID)

View file

@ -105,3 +105,51 @@ func GetNamespaceByID(id int64) (namespace Namespace, err error) {
return namespace, err 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
}

View file

@ -30,29 +30,3 @@ func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
return 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
}

View file

@ -1,7 +1,6 @@
package v1 package v1
import ( import (
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
) )
@ -21,6 +20,9 @@ func GetAllNamespacesByCurrentUser(c echo.Context) error {
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
return echo.NewHTTPError(http.StatusNotImplemented)
/*
user, err := models.GetCurrentUser(c) user, err := models.GetCurrentUser(c)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get the current user."}) return c.JSON(http.StatusInternalServerError, models.Message{"Could not get the current user."})
@ -31,5 +33,5 @@ func GetAllNamespacesByCurrentUser(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get namespaces."}) return c.JSON(http.StatusInternalServerError, models.Message{"Could not get namespaces."})
} }
return c.JSON(http.StatusOK, namespaces) return c.JSON(http.StatusOK, namespaces)*/
} }

View file

@ -1,7 +1,6 @@
package crud package crud
import ( import (
"fmt"
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
@ -16,7 +15,6 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
lists, err := c.CObject.ReadAll(&currentUser) lists, err := c.CObject.ReadAll(&currentUser)
if err != nil { if err != nil {
fmt.Println(err)
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."}) return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."})
} }

View file

@ -103,7 +103,10 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/items/:id", itemHandler.DeleteWeb) a.DELETE("/items/:id", itemHandler.DeleteWeb)
a.POST("/items/:id", itemHandler.UpdateWeb) 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.PUT("/namespaces", apiv1.AddNamespace)
a.GET("/namespaces/:id", apiv1.ShowNamespace) a.GET("/namespaces/:id", apiv1.ShowNamespace)
a.POST("/namespaces/:id", apiv1.UpdateNamespace) a.POST("/namespaces/:id", apiv1.UpdateNamespace)