implemented get all namespaces via interface
This commit is contained in:
parent
5c4fb7ed73
commit
4213f3b08c
6 changed files with 65 additions and 40 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)*/
|
||||
}
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue