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-02 08:54:44 +02:00
|
|
|
package models
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2018-12-01 02:59:17 +01:00
|
|
|
import _ "code.vikunja.io/web" // For swaggerdocs generation
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Update implements the update method via the interface
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Updates a namespace
|
|
|
|
// @Description Updates a namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @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"
|
|
|
|
// @Param namespace body models.Namespace true "The namespace with updated values you want to update."
|
|
|
|
// @Success 200 {object} models.Namespace "The updated namespace."
|
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 /namespace/{id} [post]
|
2018-07-21 15:17:02 +02:00
|
|
|
func (n *Namespace) Update() (err error) {
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if we have at least a name
|
|
|
|
if n.Name == "" {
|
2018-07-21 15:17:02 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID}
|
2018-07-04 08:15:47 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if the namespace exists
|
2018-07-21 15:17:02 +02:00
|
|
|
currentNamespace, err := GetNamespaceByID(n.ID)
|
2018-07-04 08:15:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if the (new) owner exists
|
2018-10-06 18:55:14 +02:00
|
|
|
n.OwnerID = n.Owner.ID
|
2018-07-12 00:30:31 +02:00
|
|
|
if currentNamespace.OwnerID != n.OwnerID {
|
2018-08-30 19:14:02 +02:00
|
|
|
n.Owner, err = GetUserByID(n.OwnerID)
|
2018-07-12 00:30:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-12 00:09:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Do the actual update
|
|
|
|
_, err = x.ID(currentNamespace.ID).Update(n)
|
2018-07-12 00:09:16 +02:00
|
|
|
return
|
2018-07-12 00:37:31 +02:00
|
|
|
}
|