api/models/namespace_delete.go

42 lines
816 B
Go
Raw Normal View History

package models
2018-07-12 21:33:37 +00:00
// Delete deletes a namespace
func (n *Namespace) Delete(id int64) (err error) {
// Check if the namespace exists
2018-07-12 21:33:37 +00:00
_, err = GetNamespaceByID(id)
if err != nil {
return
}
// Delete the namespace
2018-07-12 21:33:37 +00:00
_, err = x.ID(id).Delete(&Namespace{})
if err != nil {
return
}
// Delete all lists with their items
2018-07-12 21:33:37 +00:00
lists, err := GetListsByNamespaceID(id)
var listIDs []int64
2018-07-12 21:33:37 +00:00
// 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)
}
// 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
}