From 06cae09f770ac84dabc69437223400b7e1048ed1 Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 2 Jul 2018 09:09:32 +0200 Subject: [PATCH] Added method to show one namespace --- models/namespaces_list.go | 9 +- public/swagger/swagger.v1.json | 137 +++++++++++++++++++++++++++++++ routes/api/v1/list_show.go | 2 +- routes/api/v1/namespace_show.go | 52 ++++++++++++ routes/api/v1/namespaces_list.go | 5 +- routes/routes.go | 7 +- 6 files changed, 199 insertions(+), 13 deletions(-) create mode 100644 routes/api/v1/namespace_show.go diff --git a/models/namespaces_list.go b/models/namespaces_list.go index fbde00e0047..ed32902d69d 100644 --- a/models/namespaces_list.go +++ b/models/namespaces_list.go @@ -36,11 +36,8 @@ func GetAllNamespacesByUserID(userID int64) (namespaces []*Namespace, err error) // Get all namespaces of teams that user is part of /*err = x.Table("namespaces"). - Join("INNER", ). - Find(namespaces)*/ - - - + Join("INNER", ). + Find(namespaces)*/ return -} \ No newline at end of file +} diff --git a/public/swagger/swagger.v1.json b/public/swagger/swagger.v1.json index 25abe7ed5e7..f8c16e36db0 100644 --- a/public/swagger/swagger.v1.json +++ b/public/swagger/swagger.v1.json @@ -352,6 +352,143 @@ } } }, + "/namespaces": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "namespaces" + ], + "summary": "Get all namespaces the currently logged in user has at least read access", + "operationId": "getNamespaces", + "responses": { + "200": { + "$ref": "#/responses/Namespace" + }, + "500": { + "$ref": "#/responses/Message" + } + } + }, + "put": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "namespaces" + ], + "summary": "Creates a new namespace owned by the currently logged in user", + "operationId": "addNamespace", + "parameters": [ + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/Namespace" + } + } + ], + "responses": { + "200": { + "$ref": "#/responses/Namespace" + }, + "400": { + "$ref": "#/responses/Message" + }, + "403": { + "$ref": "#/responses/Message" + }, + "500": { + "$ref": "#/responses/Message" + } + } + } + }, + "/namespaces/{namespaceID}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "namespaces" + ], + "summary": "gets one namespace with all todo items", + "operationId": "getNamespace", + "parameters": [ + { + "type": "string", + "description": "ID of the namespace to show", + "name": "namespaceID", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/Namespace" + }, + "400": { + "$ref": "#/responses/Message" + }, + "500": { + "$ref": "#/responses/Message" + } + } + }, + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "namespaces" + ], + "summary": "Updates a namespace", + "operationId": "upadteNamespace", + "parameters": [ + { + "type": "string", + "description": "ID of the namespace to update", + "name": "namespaceID", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/Namespace" + } + } + ], + "responses": { + "200": { + "$ref": "#/responses/Namespace" + }, + "400": { + "$ref": "#/responses/Message" + }, + "403": { + "$ref": "#/responses/Message" + }, + "500": { + "$ref": "#/responses/Message" + } + } + } + }, "/register": { "post": { "consumes": [ diff --git a/routes/api/v1/list_show.go b/routes/api/v1/list_show.go index 73ffc4b065b..a07759b9c7d 100644 --- a/routes/api/v1/list_show.go +++ b/routes/api/v1/list_show.go @@ -7,7 +7,7 @@ import ( "strconv" ) -// AddOrUpdateList Adds or updates a new list +// GetListByID Adds or updates a new list func GetListByID(c echo.Context) error { // swagger:operation GET /lists/{listID} lists getList // --- diff --git a/routes/api/v1/namespace_show.go b/routes/api/v1/namespace_show.go new file mode 100644 index 00000000000..a367557830e --- /dev/null +++ b/routes/api/v1/namespace_show.go @@ -0,0 +1,52 @@ +package v1 + +import ( + "git.kolaente.de/konrad/list/models" + "github.com/labstack/echo" + "net/http" + "strconv" +) + +func ShowNamespace(c echo.Context) error { + // swagger:operation GET /namespaces/{namespaceID} namespaces getNamespace + // --- + // summary: gets one namespace with all todo items + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: namespaceID + // in: path + // description: ID of the namespace to show + // 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."}) + } + + // Get the namespace + namespace, err := models.GetNamespaceByID(namespaceID) + if err != nil { + if models.IsErrNamespaceDoesNotExist(err) { + return c.JSON(http.StatusBadRequest, models.Message{"The namespace does not exist."}) + } + + return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + + return c.JSON(http.StatusOK, namespace) +} diff --git a/routes/api/v1/namespaces_list.go b/routes/api/v1/namespaces_list.go index eff70208157..98bc62a4e5b 100644 --- a/routes/api/v1/namespaces_list.go +++ b/routes/api/v1/namespaces_list.go @@ -1,8 +1,8 @@ package v1 import ( - "github.com/labstack/echo" "git.kolaente.de/konrad/list/models" + "github.com/labstack/echo" "net/http" ) @@ -20,7 +20,6 @@ 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."}) @@ -32,4 +31,4 @@ func GetAllNamespacesByCurrentUser(c echo.Context) error { } return c.JSON(http.StatusOK, namespaces) -} \ No newline at end of file +} diff --git a/routes/routes.go b/routes/routes.go index ab93b8b5a6d..9dacf827269 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -95,8 +95,9 @@ func RegisterRoutes(e *echo.Echo) { a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser) a.PUT("/namespaces", apiv1.AddNamespace) - a.GET("/namespaces/:id") + 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") - a.DELETE("/namespaces/:id") + //a.PUT("/namespaces/:id") // Creates a new list in that namespace + // a.DELETE("/namespaces/:id") // Deletes a namespace with all lists }