diff --git a/models/error.go b/models/error.go index afbc1551..78200423 100644 --- a/models/error.go +++ b/models/error.go @@ -207,7 +207,6 @@ func (err ErrNeedToBeItemOwner) Error() string { return fmt.Sprintf("You need to be item owner to do that [ItemID: %d, UserID: %d]", err.ItemID, err.UserID) } - // ================= // Namespace errors // ================= @@ -230,7 +229,7 @@ func (err ErrNamespaceDoesNotExist) Error() string { // ErrNeedToBeNamespaceOwner represents an error, where the user is not the owner of that namespace (used i.e. when deleting a namespace) type ErrNeedToBeNamespaceOwner struct { NamespaceID int64 - UserID int64 + UserID int64 } // IsErrNamespaceDoesNotExist checks if an error is a ErrNamespaceDoesNotExist. @@ -241,4 +240,4 @@ func IsErrNeedToBeNamespaceOwner(err error) bool { func (err ErrNeedToBeNamespaceOwner) Error() string { return fmt.Sprintf("You need to be namespace owner to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID) -} \ No newline at end of file +} diff --git a/models/namespaces.go b/models/namespaces.go index fd59247b..f1141354 100644 --- a/models/namespaces.go +++ b/models/namespaces.go @@ -44,7 +44,6 @@ func (user *User) IsNamespaceAdmin(namespace *Namespace) (ok bool, err error) { // Check if that user is in a team which has admin rights to that namespace - return } @@ -56,7 +55,6 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (has bool, err error) // Check if the user is in a team which has access to the namespace - return } @@ -68,7 +66,7 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) { } if !exists { - return namespace, ErrNamespaceDoesNotExist{ID:id} + return namespace, ErrNamespaceDoesNotExist{ID: id} } // Get the namespace Owner @@ -79,28 +77,3 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) { return namespace, err } - -// CreateOrUpdateNamespace does what it says -func CreateOrUpdateNamespace(namespace *Namespace) (err error) { - // Check if the User exists - _, _, err = GetUserByID(namespace.Owner.ID) - if err != nil { - return - } - - namespace.OwnerID = namespace.Owner.ID - - if namespace.ID == 0 { - _, err = x.Insert(namespace) - if err != nil { - return - } - } else { - _, err = x.ID(namespace.ID).Update(namespace) - if err != nil { - return - } - } - - return -} diff --git a/models/namespaces_add_update.go b/models/namespaces_add_update.go new file mode 100644 index 00000000..2640e7f9 --- /dev/null +++ b/models/namespaces_add_update.go @@ -0,0 +1 @@ +package models diff --git a/models/namespaces_list.go b/models/namespaces_list.go new file mode 100644 index 00000000..fbde00e0 --- /dev/null +++ b/models/namespaces_list.go @@ -0,0 +1,46 @@ +package models + +// CreateOrUpdateNamespace does what it says +func CreateOrUpdateNamespace(namespace *Namespace) (err error) { + // Check if the User exists + _, _, err = GetUserByID(namespace.Owner.ID) + if err != nil { + return + } + + namespace.OwnerID = namespace.Owner.ID + + if namespace.ID == 0 { + _, err = x.Insert(namespace) + if err != nil { + return + } + } else { + _, err = x.ID(namespace.ID).Update(namespace) + if err != nil { + 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)*/ + + + + + return +} \ No newline at end of file diff --git a/models/teams.go b/models/teams.go index e47d2ce0..d644f751 100644 --- a/models/teams.go +++ b/models/teams.go @@ -64,7 +64,6 @@ func (TeamList) TableName() string { return "team_list" } - func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { err = x.Table("teams"). Join("INNER", "team_namespaces", "teams.id = team_id"). @@ -72,4 +71,4 @@ func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { Find(teams) return -} \ No newline at end of file +} diff --git a/models/user_add_update.go b/models/user_add_update.go index 54592511..12027dd7 100644 --- a/models/user_add_update.go +++ b/models/user_add_update.go @@ -51,7 +51,7 @@ func CreateUser(user User) (newUser User, err error) { } // Create the user's namespace - err = CreateOrUpdateNamespace(&Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner:newUserOut}) + err = CreateOrUpdateNamespace(&Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner: newUserOut}) if err != nil { return User{}, err } diff --git a/routes/api/v1/namespaces_list.go b/routes/api/v1/namespaces_list.go new file mode 100644 index 00000000..eff70208 --- /dev/null +++ b/routes/api/v1/namespaces_list.go @@ -0,0 +1,35 @@ +package v1 + +import ( + "github.com/labstack/echo" + "git.kolaente.de/konrad/list/models" + "net/http" +) + +func GetAllNamespacesByCurrentUser(c echo.Context) error { + // swagger:operation GET /namespaces namespaces getNamespaces + // --- + // summary: Get all namespaces the currently logged in user has at least read access + // consumes: + // - application/json + // produces: + // - application/json + // responses: + // "200": + // "$ref": "#/responses/Namespace" + // "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."}) + } + + 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) +} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 7a0fddca..ab93b8b5 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -93,7 +93,7 @@ func RegisterRoutes(e *echo.Echo) { a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID) a.POST("/item/:id", apiv1.UpdateListItem) - a.GET("/namespaces") + a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser) a.PUT("/namespaces", apiv1.AddNamespace) a.GET("/namespaces/:id") a.POST("/namespaces/:id", apiv1.UpdateNamespace)