From 0aa84e653f7b288fb2936e304741e9c02b24394b Mon Sep 17 00:00:00 2001 From: konrad Date: Thu, 12 Jul 2018 00:09:16 +0200 Subject: [PATCH] implemented namespace creation via interface method --- models/error.go | 16 ++++++++++++++++ models/namespaces_add_update.go | 18 ++++++++++++++++++ routes/api/v1/namespace_add_update.go | 4 ++-- routes/crud/create.go | 7 +++++++ routes/routes.go | 2 +- 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/models/error.go b/models/error.go index a25efa4c..70cd4d71 100644 --- a/models/error.go +++ b/models/error.go @@ -305,3 +305,19 @@ func IsErrUserDoesNotHaveWriteAccessToNamespace(err error) bool { func (err ErrUserDoesNotHaveWriteAccessToNamespace) Error() string { return fmt.Sprintf("You need to have write access to this namespace to do that [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID) } + +// ErrNamespaceNameCannotBeEmpty represents an error, where a namespace name is empty. +type ErrNamespaceNameCannotBeEmpty struct { + NamespaceID int64 + UserID int64 +} + +// IsErrNamespaceNameCannotBeEmpty checks if an error is a ErrNamespaceDoesNotExist. +func IsErrNamespaceNameCannotBeEmpty(err error) bool { + _, ok := err.(ErrNamespaceNameCannotBeEmpty) + return ok +} + +func (err ErrNamespaceNameCannotBeEmpty) Error() string { + return fmt.Sprintf("Namespace name cannot be emtpy [NamespaceID: %d, UserID: %d]", err.NamespaceID, err.UserID) +} \ No newline at end of file diff --git a/models/namespaces_add_update.go b/models/namespaces_add_update.go index 07efdfe5..2d023ba6 100644 --- a/models/namespaces_add_update.go +++ b/models/namespaces_add_update.go @@ -30,3 +30,21 @@ func CreateOrUpdateNamespace(namespace *Namespace) (err error) { return } + +func (n *Namespace) Create(doer *User, _ int64) (err error) { + // Check if we have at least a name + if n.Name == "" { + return ErrNamespaceNameCannotBeEmpty{NamespaceID:0, UserID:doer.ID} + } + + // Check if the User exists + n.Owner, _, err = GetUserByID(doer.ID) + if err != nil { + return + } + n.OwnerID = n.Owner.ID + + // Insert + _, err = x.Insert(n) + return +} diff --git a/routes/api/v1/namespace_add_update.go b/routes/api/v1/namespace_add_update.go index 057e0fe1..40154d7c 100644 --- a/routes/api/v1/namespace_add_update.go +++ b/routes/api/v1/namespace_add_update.go @@ -31,7 +31,7 @@ func AddNamespace(c echo.Context) error { // "500": // "$ref": "#/responses/Message" - return addOrUpdateNamespace(c) + return echo.NewHTTPError(http.StatusNotImplemented) } // UpdateNamespace ... @@ -63,7 +63,7 @@ func UpdateNamespace(c echo.Context) error { // "500": // "$ref": "#/responses/Message" - return addOrUpdateNamespace(c) + return echo.NewHTTPError(http.StatusNotImplemented) } // AddOrUpdateNamespace Adds or updates a new namespace diff --git a/routes/crud/create.go b/routes/crud/create.go index e3735667..6defe79b 100644 --- a/routes/crud/create.go +++ b/routes/crud/create.go @@ -44,6 +44,13 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusForbidden, "You need to have write access on that list.") } + if models.IsErrNamespaceDoesNotExist(err) { + return echo.NewHTTPError(http.StatusBadRequest, "The namespace does not exist.") + } + if models.IsErrNamespaceNameCannotBeEmpty(err) { + return echo.NewHTTPError(http.StatusNotFound, "The namespace name cannot be empty.") + } + return echo.NewHTTPError(http.StatusInternalServerError) } diff --git a/routes/routes.go b/routes/routes.go index d8b56b59..81204dca 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -107,7 +107,7 @@ func RegisterRoutes(e *echo.Echo) { CObject: &models.Namespace{}, } a.GET("/namespaces", namespaceHandler.ReadAllWeb) - a.PUT("/namespaces", apiv1.AddNamespace) + a.PUT("/namespaces", namespaceHandler.CreateWeb) a.GET("/namespaces/:id", apiv1.ShowNamespace) a.POST("/namespaces/:id", apiv1.UpdateNamespace) a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)