2018-07-04 19:21:04 +02:00
|
|
|
package models
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// DeleteNamespaceByID deletes a namespace and takes its id as an argument
|
2018-07-04 19:21:04 +02:00
|
|
|
func DeleteNamespaceByID(namespaceID int64, doer *User) (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)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the namespace
|
|
|
|
_, err = x.ID(namespaceID).Delete(&Namespace{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all lists with their items
|
|
|
|
lists, err := GetListsByNamespaceID(namespaceID)
|
|
|
|
var listIDs []int64
|
|
|
|
for _, list := range lists {
|
|
|
|
listIDs = append(listIDs, list.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete items
|
|
|
|
_, err = x.In("list_id", listIDs).Delete(&ListItem{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the lists
|
|
|
|
_, err = x.In("id", listIDs).Delete(&List{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|