From e0244e28d7904cc9c3bb310ca8c389eb2462b447 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 3 Jul 2018 08:26:28 +0200 Subject: [PATCH] Added method to show all lists in a namespace --- models/lists.go | 20 ++++++--- routes/api/v1/list_by_namespace.go | 68 ++++++++++++++++++++++++++++++ routes/api/v1/namespace_show.go | 13 ++++++ routes/routes.go | 4 +- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 routes/api/v1/list_by_namespace.go diff --git a/models/lists.go b/models/lists.go index aa93c0e66e9..31ae74c6955 100644 --- a/models/lists.go +++ b/models/lists.go @@ -6,29 +6,30 @@ type List struct { Title string `xorm:"varchar(250)" json:"title"` Description string `xorm:"varchar(1000)" json:"description"` OwnerID int64 `xorm:"int(11)" json:"-"` - Owner User `xorm:"-" json:"owner"` + NamespaceID int64 `xorm:"int(11)" json:"-"` Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` + Owner User `xorm:"-" json:"owner"` Items []*ListItem `xorm:"-" json:"items"` } // GetListByID returns a list by its ID -func GetListByID(id int64) (list List, err error) { +func GetListByID(id int64) (list *List, err error) { list.ID = id exists, err := x.Get(&list) if err != nil { - return List{}, err + return &List{}, err } if !exists { - return List{}, ErrListDoesNotExist{ID: id} + return &List{}, ErrListDoesNotExist{ID: id} } // Get the list owner user, _, err := GetUserByID(list.OwnerID) if err != nil { - return List{}, err + return &List{}, err } list.Owner = user @@ -61,3 +62,12 @@ func GetListsByUser(user *User) (lists []*List, err error) { return } + +func GetListsByNamespaceID(nID int64) (lists []*List, err error) { + exists, err := x.Where("namespace_id = ?", nID).Get(lists) + if !exists { + return lists, ErrNamespaceDoesNotExist{} + } + + return +} \ No newline at end of file diff --git a/routes/api/v1/list_by_namespace.go b/routes/api/v1/list_by_namespace.go new file mode 100644 index 00000000000..a63811dd2b2 --- /dev/null +++ b/routes/api/v1/list_by_namespace.go @@ -0,0 +1,68 @@ +package v1 + +import ( + "github.com/labstack/echo" + "strconv" + "net/http" + "git.kolaente.de/konrad/list/models" +) + +func GetListsByNamespaceID(c echo.Context) error { + // swagger:operation GET /namespaces/{namespaceID}/lists namespaces getNamespaces + // --- + // summary: gets all lists belonging to that namespace + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: namespaceID + // in: path + // description: ID of the namespace + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/Namespace" + // "400": + // "$ref": "#/responses/Message" + // "500": + // "$ref": "#/responses/Message" + + + + // Check if we have our ID + id := c.Param("id") + // Make int + namespaceID, err := strconv.ParseInt(id, 10, 64) + + if err != nil { + return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // Check if the user has acces to that namespace + user, err := models.GetCurrentUser(c) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID}) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + if !has { + return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."}) + } + + // Get the lists + lists, err := models.GetListsByNamespaceID(namespaceID) + if err != nil { + + if models.IsErrNamespaceDoesNotExist(err) { + return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."}) + } + + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + + return c.JSON(http.StatusOK, lists) +} \ No newline at end of file diff --git a/routes/api/v1/namespace_show.go b/routes/api/v1/namespace_show.go index a367557830e..0d70be5b982 100644 --- a/routes/api/v1/namespace_show.go +++ b/routes/api/v1/namespace_show.go @@ -38,6 +38,19 @@ func ShowNamespace(c echo.Context) error { return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) } + // Check if the user has acces to that namespace + user, err := models.GetCurrentUser(c) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID}) + if err != nil { + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + if !has { + return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."}) + } + // Get the namespace namespace, err := models.GetNamespaceByID(namespaceID) if err != nil { diff --git a/routes/routes.go b/routes/routes.go index 9dacf827269..9bd43092c35 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -96,8 +96,8 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser) a.PUT("/namespaces", apiv1.AddNamespace) a.GET("/namespaces/:id", apiv1.ShowNamespace) - //a.GET("/namespaces/:id/lists") // Gets all lists for that namespace a.POST("/namespaces/:id", apiv1.UpdateNamespace) - //a.PUT("/namespaces/:id") // Creates a new list in that namespace // a.DELETE("/namespaces/:id") // Deletes a namespace with all lists + a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID) + //a.PUT("/namespaces/:id/lists") // Creates a new list in that namespace }