2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-04 19:21:04 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
import (
|
|
|
|
"code.vikunja.io/api/pkg/metrics"
|
|
|
|
_ "code.vikunja.io/web" // For swaggerdocs generation
|
|
|
|
)
|
2018-12-01 02:59:17 +01:00
|
|
|
|
2018-07-12 23:33:37 +02:00
|
|
|
// Delete deletes a namespace
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Deletes a namespace
|
|
|
|
// @Description Delets a namespace
|
|
|
|
// @tags namespace
|
|
|
|
// @Produce json
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param id path int true "Namespace ID"
|
|
|
|
// @Success 200 {object} models.Message "The namespace was successfully deleted."
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid namespace object provided."
|
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the namespace"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id} [delete]
|
2018-07-18 08:15:38 +02:00
|
|
|
func (n *Namespace) Delete() (err error) {
|
2018-07-04 19:21:04 +02:00
|
|
|
|
|
|
|
// Check if the namespace exists
|
2018-07-18 08:15:38 +02:00
|
|
|
_, err = GetNamespaceByID(n.ID)
|
2018-07-04 19:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the namespace
|
2018-07-18 08:15:38 +02:00
|
|
|
_, err = x.ID(n.ID).Delete(&Namespace{})
|
2018-07-04 19:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Delete all lists with their tasks
|
2018-12-04 11:16:42 +01:00
|
|
|
lists, err := GetListsByNamespaceID(n.ID, &User{})
|
2019-02-18 20:32:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-04 19:21:04 +02:00
|
|
|
var listIDs []int64
|
2018-07-12 23:33:37 +02:00
|
|
|
// We need to do that for here because we need the list ids to delete two times:
|
|
|
|
// 1) to delete the lists itself
|
2018-08-30 08:09:17 +02:00
|
|
|
// 2) to delete the list tasks
|
2018-10-31 13:42:38 +01:00
|
|
|
for _, l := range lists {
|
|
|
|
listIDs = append(listIDs, l.ID)
|
2018-07-04 19:21:04 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Delete tasks
|
|
|
|
_, err = x.In("list_id", listIDs).Delete(&ListTask{})
|
2018-07-04 19:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the lists
|
|
|
|
_, err = x.In("id", listIDs).Delete(&List{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:50:35 +01:00
|
|
|
metrics.UpdateCount(-1, metrics.NamespaceCountKey)
|
|
|
|
|
2018-07-04 19:21:04 +02:00
|
|
|
return
|
|
|
|
}
|