Implemented deletion of namespaces
This commit is contained in:
parent
55c02bc973
commit
c5888093fe
4 changed files with 16 additions and 46 deletions
|
@ -1,29 +1,26 @@
|
|||
package models
|
||||
|
||||
// DeleteNamespaceByID deletes a namespace and takes its id as an argument
|
||||
func DeleteNamespaceByID(namespaceID int64, doer *User) (err error) {
|
||||
// Delete deletes a namespace
|
||||
func (n *Namespace) Delete(id int64) (err error) {
|
||||
|
||||
// Check if the namespace exists
|
||||
namespace, err := GetNamespaceByID(namespaceID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the user is namespace admin
|
||||
err = doer.IsNamespaceAdmin(&namespace)
|
||||
_, err = GetNamespaceByID(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the namespace
|
||||
_, err = x.ID(namespaceID).Delete(&Namespace{})
|
||||
_, err = x.ID(id).Delete(&Namespace{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete all lists with their items
|
||||
lists, err := GetListsByNamespaceID(namespaceID)
|
||||
lists, err := GetListsByNamespaceID(id)
|
||||
var listIDs []int64
|
||||
// We need to do that for here because we need the list ids to delete two times:
|
||||
// 1) to delete the lists itself
|
||||
// 2) to delete the list items
|
||||
for _, list := range lists {
|
||||
listIDs = append(listIDs, list.ID)
|
||||
}
|
||||
|
|
|
@ -88,6 +88,12 @@ func (n *Namespace) CanUpdate(user *User, id int64) bool {
|
|||
return nn.IsAdmin(user)
|
||||
}
|
||||
|
||||
// CanDelete checks if the user can delete a namespace
|
||||
func (n *Namespace) CanDelete(user *User, id int64) bool {
|
||||
nn, _ := GetNamespaceByID(id)
|
||||
return nn.IsAdmin(user)
|
||||
}
|
||||
|
||||
// CanCreate checks if the user can create a new namespace
|
||||
func (n *Namespace) CanCreate(user *User, id int64) bool {
|
||||
// This is currently a dummy function, later on we could imagine global limits etc.
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// DeleteNamespaceByID ...
|
||||
|
@ -34,36 +32,5 @@ func DeleteNamespaceByID(c echo.Context) error {
|
|||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
// Check if we have our ID
|
||||
id := c.Param("id")
|
||||
// Make int
|
||||
itemID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
|
||||
// Check if the user has the right to delete that namespace
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
err = models.DeleteNamespaceByID(itemID, &user)
|
||||
if err != nil {
|
||||
if models.IsErrNeedToBeNamespaceOwner(err) {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You need to be the namespace owner to delete a namespace."})
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return c.JSON(http.StatusNotFound, models.Message{"This namespace does not exist."})
|
||||
}
|
||||
|
||||
if models.IsErrUserNeedsToBeNamespaceAdmin(err) {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to delete a namespace."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{"The namespace was deleted with success."})
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
||||
|
|
|
@ -110,6 +110,6 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
|
||||
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
||||
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)
|
||||
a.DELETE("/namespaces/:id", namespaceHandler.DeleteWeb)
|
||||
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue