2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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-11-26 21:17:33 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
import (
|
2019-07-16 16:15:40 +02:00
|
|
|
"code.vikunja.io/api/pkg/metrics"
|
2020-02-08 13:48:49 +01:00
|
|
|
"code.vikunja.io/api/pkg/timeutil"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2019-04-01 21:48:48 +02:00
|
|
|
"github.com/imdario/mergo"
|
2018-12-01 00:26:56 +01:00
|
|
|
"time"
|
2020-03-22 18:24:54 +01:00
|
|
|
"xorm.io/builder"
|
2018-12-01 00:26:56 +01:00
|
|
|
)
|
2018-11-19 12:37:59 +01:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// Namespace holds informations about a namespace
|
|
|
|
type Namespace struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this namespace.
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
|
|
|
|
// The name of this namespace.
|
2020-05-16 12:17:44 +02:00
|
|
|
Title string `xorm:"varchar(250) not null" json:"title" valid:"required,runelength(5|250)" minLength:"5" maxLength:"250"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The description of the namespace
|
2019-07-21 23:57:19 +02:00
|
|
|
Description string `xorm:"longtext null" json:"description"`
|
2018-10-05 18:46:01 +02:00
|
|
|
OwnerID int64 `xorm:"int(11) not null INDEX" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2020-03-22 22:09:32 +01:00
|
|
|
// The hex color of this namespace
|
|
|
|
HexColor string `xorm:"varchar(6) null" json:"hex_color" valid:"runelength(0|6)" maxLength:"6"`
|
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// Whether or not a namespace is archived.
|
|
|
|
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`
|
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who owns this namespace
|
2020-01-26 18:08:06 +01:00
|
|
|
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
|
2018-06-14 17:43:00 +02:00
|
|
|
|
2020-02-08 13:48:49 +01:00
|
|
|
// A timestamp when this namespace was created. You cannot change this value.
|
|
|
|
Created timeutil.TimeStamp `xorm:"created not null" json:"created"`
|
|
|
|
// A timestamp when this namespace was last updated. You cannot change this value.
|
|
|
|
Updated timeutil.TimeStamp `xorm:"updated not null" json:"updated"`
|
2018-07-09 23:17:19 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 11:16:42 +01:00
|
|
|
// PseudoNamespace is a pseudo namespace used to hold shared lists
|
|
|
|
var PseudoNamespace = Namespace{
|
|
|
|
ID: -1,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Shared Lists",
|
2018-12-04 11:16:42 +01:00
|
|
|
Description: "Lists of other users shared with you via teams or directly.",
|
2020-02-08 13:48:49 +01:00
|
|
|
Created: timeutil.FromTime(time.Now()),
|
|
|
|
Updated: timeutil.FromTime(time.Now()),
|
2018-12-04 11:16:42 +01:00
|
|
|
}
|
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
// TableName makes beautiful table names
|
|
|
|
func (Namespace) TableName() string {
|
|
|
|
return "namespaces"
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:31:37 +01:00
|
|
|
// GetSimpleByID gets a namespace without things like the owner, it more or less only checks if it exists.
|
|
|
|
func (n *Namespace) GetSimpleByID() (err error) {
|
|
|
|
if n.ID == 0 {
|
|
|
|
return ErrNamespaceDoesNotExist{ID: n.ID}
|
2018-09-13 20:07:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 11:16:42 +01:00
|
|
|
// Get the namesapce with shared lists
|
2019-03-08 22:31:37 +01:00
|
|
|
if n.ID == -1 {
|
|
|
|
*n = PseudoNamespace
|
|
|
|
return
|
2018-12-04 11:16:42 +01:00
|
|
|
}
|
|
|
|
|
2019-04-01 21:48:48 +02:00
|
|
|
namespaceFromDB := &Namespace{}
|
|
|
|
exists, err := x.Where("id = ?", n.ID).Get(namespaceFromDB)
|
2018-07-02 08:40:24 +02:00
|
|
|
if err != nil {
|
2019-03-08 22:31:37 +01:00
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
|
|
|
if !exists {
|
2019-03-08 22:31:37 +01:00
|
|
|
return ErrNamespaceDoesNotExist{ID: n.ID}
|
|
|
|
}
|
2019-04-01 21:48:48 +02:00
|
|
|
// We don't want to override the provided user struct because this would break updating, so we have to merge it
|
|
|
|
if err := mergo.Merge(namespaceFromDB, n, mergo.WithOverride); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*n = *namespaceFromDB
|
2019-03-08 22:31:37 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNamespaceByID returns a namespace object by its ID
|
|
|
|
func GetNamespaceByID(id int64) (namespace Namespace, err error) {
|
|
|
|
namespace = Namespace{ID: id}
|
|
|
|
err = namespace.GetSimpleByID()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the namespace Owner
|
2020-01-26 18:08:06 +01:00
|
|
|
namespace.Owner, err = user.GetUserByID(namespace.OwnerID)
|
2019-03-08 22:31:37 +01:00
|
|
|
return
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// CheckIsArchived returns an ErrNamespaceIsArchived if the namepace is archived.
|
|
|
|
func (n *Namespace) CheckIsArchived() error {
|
|
|
|
exists, err := x.
|
|
|
|
Where("id = ? AND is_archived = true", n.ID).
|
|
|
|
Exist(&Namespace{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return ErrNamespaceIsArchived{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 11:54:55 +02:00
|
|
|
// ReadOne gets one namespace
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Gets one namespace
|
|
|
|
// @Description Returns a namespace by its ID.
|
|
|
|
// @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"
|
|
|
|
// @Success 200 {object} models.Namespace "The Namespace"
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to that namespace."
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id} [get]
|
2018-07-21 15:08:46 +02:00
|
|
|
func (n *Namespace) ReadOne() (err error) {
|
2020-03-21 13:55:42 +01:00
|
|
|
*n, err = GetNamespaceByID(n.ID)
|
2018-07-12 11:54:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:46:35 +01:00
|
|
|
// NamespaceWithLists represents a namespace with list meta informations
|
|
|
|
type NamespaceWithLists struct {
|
|
|
|
Namespace `xorm:"extends"`
|
|
|
|
Lists []*List `xorm:"-" json:"lists"`
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:00:00 +02:00
|
|
|
// ReadAll gets all namespaces a user has access to
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Get all namespaces a user has access to
|
|
|
|
// @Description Returns all namespaces a user has access to.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-10-23 23:11:40 +02:00
|
|
|
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param s query string false "Search namespaces by name."
|
2020-03-15 22:50:39 +01:00
|
|
|
// @Param is_archived query bool false "If true, also returns all archived namespaces."
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Success 200 {array} models.NamespaceWithLists "The Namespaces."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces [get]
|
2019-10-23 23:11:40 +02:00
|
|
|
func (n *Namespace) ReadAll(a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
|
2019-08-31 22:56:41 +02:00
|
|
|
if _, is := a.(*LinkSharing); is {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, ErrGenericForbidden{}
|
2019-08-31 22:56:41 +02:00
|
|
|
}
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
doer, err := user.GetFromAuth(a)
|
2018-12-01 00:26:56 +01:00
|
|
|
if err != nil {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, err
|
2018-12-01 00:26:56 +01:00
|
|
|
}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2018-11-12 16:46:35 +01:00
|
|
|
all := []*NamespaceWithLists{}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2020-03-22 18:24:54 +01:00
|
|
|
// Adding a 1=1 condition by default here because xorm always needs a condition and cannot handle nil conditions
|
|
|
|
var isArchivedCond builder.Cond = builder.Eq{"1": 1}
|
|
|
|
if !n.IsArchived {
|
|
|
|
isArchivedCond = builder.And(
|
|
|
|
builder.Eq{"namespaces.is_archived": false},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-19 12:37:59 +01:00
|
|
|
// Create our pseudo-namespace to hold the shared lists
|
|
|
|
// We want this one at the beginning, which is why we create it here
|
2018-12-04 11:16:42 +01:00
|
|
|
pseudonamespace := PseudoNamespace
|
2019-08-14 21:59:31 +02:00
|
|
|
pseudonamespace.Owner = doer
|
2018-11-19 12:37:59 +01:00
|
|
|
all = append(all, &NamespaceWithLists{
|
2018-12-04 11:16:42 +01:00
|
|
|
pseudonamespace,
|
2018-11-19 12:37:59 +01:00
|
|
|
[]*List{},
|
|
|
|
})
|
|
|
|
|
2020-04-12 19:29:24 +02:00
|
|
|
limit, start := getLimitFromPageIndex(page, perPage)
|
|
|
|
|
|
|
|
query := x.Select("namespaces.*").
|
2018-07-11 13:00:00 +02:00
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
2018-09-06 08:46:34 +02:00
|
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
2018-07-11 13:00:00 +02:00
|
|
|
Where("team_members.user_id = ?", doer.ID).
|
|
|
|
Or("namespaces.owner_id = ?", doer.ID).
|
2018-09-06 08:46:34 +02:00
|
|
|
Or("users_namespace.user_id = ?", doer.ID).
|
2018-07-11 13:00:00 +02:00
|
|
|
GroupBy("namespaces.id").
|
2020-05-16 12:17:44 +02:00
|
|
|
Where("namespaces.title LIKE ?", "%"+search+"%").
|
2020-04-12 19:29:24 +02:00
|
|
|
Where(isArchivedCond)
|
|
|
|
if limit > 0 {
|
|
|
|
query = query.Limit(limit, start)
|
|
|
|
}
|
|
|
|
err = query.Find(&all)
|
2018-07-11 13:00:00 +02:00
|
|
|
if err != nil {
|
2019-10-23 23:11:40 +02:00
|
|
|
return all, 0, 0, err
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 07:27:49 +02:00
|
|
|
// Make a list of namespace ids
|
|
|
|
var namespaceids []int64
|
2020-05-15 17:10:22 +02:00
|
|
|
var userIDs []int64
|
2018-09-17 07:27:49 +02:00
|
|
|
for _, nsp := range all {
|
|
|
|
namespaceids = append(namespaceids, nsp.ID)
|
2020-05-15 17:10:22 +02:00
|
|
|
userIDs = append(userIDs, nsp.OwnerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all owners
|
|
|
|
userMap := make(map[int64]*user.User)
|
|
|
|
err = x.In("id", userIDs).Find(&userMap)
|
|
|
|
if err != nil {
|
|
|
|
return all, 0, 0, err
|
2018-09-17 07:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get all lists
|
|
|
|
lists := []*List{}
|
2020-03-22 18:24:54 +01:00
|
|
|
listQuery := x.
|
|
|
|
In("namespace_id", namespaceids)
|
|
|
|
|
|
|
|
if !n.IsArchived {
|
|
|
|
listQuery.And("is_archived = false")
|
|
|
|
}
|
|
|
|
err = listQuery.Find(&lists)
|
2018-09-17 07:27:49 +02:00
|
|
|
if err != nil {
|
2019-10-23 23:11:40 +02:00
|
|
|
return all, 0, 0, err
|
2018-09-17 07:27:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 12:37:59 +01:00
|
|
|
// Get all lists individually shared with our user (not via a namespace)
|
|
|
|
individualLists := []*List{}
|
2020-03-22 18:24:54 +01:00
|
|
|
iListQuery := x.Select("l.*").
|
2018-11-19 12:37:59 +01:00
|
|
|
Table("list").
|
|
|
|
Alias("l").
|
|
|
|
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
|
|
|
|
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tl.team_id").
|
|
|
|
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
|
|
|
|
Where("tm.user_id = ?", doer.ID).
|
|
|
|
Or("ul.user_id = ?", doer.ID).
|
2020-03-22 18:24:54 +01:00
|
|
|
GroupBy("l.id")
|
|
|
|
if !n.IsArchived {
|
|
|
|
iListQuery.And("l.is_archived = false")
|
|
|
|
}
|
|
|
|
err = iListQuery.Find(&individualLists)
|
2018-11-19 12:37:59 +01:00
|
|
|
if err != nil {
|
2019-10-23 23:11:40 +02:00
|
|
|
return nil, 0, 0, err
|
2018-11-19 12:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the namespace -1 so we now later which one it was
|
|
|
|
// + Append it to all lists we already have
|
|
|
|
for _, l := range individualLists {
|
|
|
|
l.NamespaceID = -1
|
|
|
|
lists = append(lists, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the pseudonamespace if we don't have any shared lists
|
|
|
|
if len(individualLists) == 0 {
|
|
|
|
all = append(all[:0], all[1:]...)
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:16:14 +02:00
|
|
|
// More details for the lists
|
2019-10-23 23:11:40 +02:00
|
|
|
err = AddListDetails(lists)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
2018-10-05 19:16:14 +02:00
|
|
|
|
2020-05-15 17:10:22 +02:00
|
|
|
nMap := make(map[int64]*NamespaceWithLists, len(all))
|
|
|
|
|
2018-09-17 07:27:49 +02:00
|
|
|
// Put objects in our namespace list
|
2020-05-15 17:10:22 +02:00
|
|
|
for _, n := range all {
|
2018-09-17 07:27:49 +02:00
|
|
|
|
|
|
|
// Users
|
2020-05-15 17:10:22 +02:00
|
|
|
n.Owner = userMap[n.OwnerID]
|
|
|
|
|
|
|
|
nMap[n.ID] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, list := range lists {
|
|
|
|
nMap[list.NamespaceID].Lists = append(nMap[list.NamespaceID].Lists, list)
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:11:40 +02:00
|
|
|
numberOfTotalItems, err = x.
|
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
|
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
|
|
|
Where("team_members.user_id = ?", doer.ID).
|
|
|
|
Or("namespaces.owner_id = ?", doer.ID).
|
|
|
|
Or("users_namespace.user_id = ?", doer.ID).
|
2020-03-15 22:50:39 +01:00
|
|
|
And("namespaces.is_archived = false").
|
2019-10-23 23:11:40 +02:00
|
|
|
GroupBy("namespaces.id").
|
2020-05-16 12:17:44 +02:00
|
|
|
Where("namespaces.title LIKE ?", "%"+search+"%").
|
2019-10-23 23:11:40 +02:00
|
|
|
Count(&NamespaceWithLists{})
|
|
|
|
if err != nil {
|
|
|
|
return all, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return all, len(all), numberOfTotalItems, nil
|
2018-07-11 13:00:00 +02:00
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
|
|
|
|
// Create implements the creation method via the interface
|
|
|
|
// @Summary Creates a new namespace
|
|
|
|
// @Description Creates a new namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param namespace body models.Namespace true "The namespace you want to create."
|
|
|
|
// @Success 200 {object} models.Namespace "The created namespace."
|
|
|
|
// @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"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces [put]
|
|
|
|
func (n *Namespace) Create(a web.Auth) (err error) {
|
|
|
|
// Check if we have at least a name
|
2020-05-16 12:17:44 +02:00
|
|
|
if n.Title == "" {
|
2019-07-16 16:15:40 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: 0, UserID: a.GetID()}
|
|
|
|
}
|
|
|
|
n.ID = 0 // This would otherwise prevent the creation of new lists after one was created
|
|
|
|
|
|
|
|
// Check if the User exists
|
2020-01-26 18:08:06 +01:00
|
|
|
n.Owner, err = user.GetUserByID(a.GetID())
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.OwnerID = n.Owner.ID
|
|
|
|
|
|
|
|
// Insert
|
|
|
|
if _, err = x.Insert(n); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.UpdateCount(1, metrics.NamespaceCountKey)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a namespace
|
|
|
|
// @Summary Deletes a namespace
|
|
|
|
// @Description Delets a namespace
|
|
|
|
// @tags namespace
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "Namespace ID"
|
|
|
|
// @Success 200 {object} models.Message "The namespace was successfully deleted."
|
|
|
|
// @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"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id} [delete]
|
|
|
|
func (n *Namespace) Delete() (err error) {
|
|
|
|
|
|
|
|
// Check if the namespace exists
|
|
|
|
_, err = GetNamespaceByID(n.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the namespace
|
|
|
|
_, err = x.ID(n.ID).Delete(&Namespace{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all lists with their tasks
|
2020-01-26 18:08:06 +01:00
|
|
|
lists, err := GetListsByNamespaceID(n.ID, &user.User{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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 tasks
|
|
|
|
for _, l := range lists {
|
|
|
|
listIDs = append(listIDs, l.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete tasks
|
2019-08-14 22:19:04 +02:00
|
|
|
_, err = x.In("list_id", listIDs).Delete(&Task{})
|
2019-07-16 16:15:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the lists
|
|
|
|
_, err = x.In("id", listIDs).Delete(&List{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.UpdateCount(-1, metrics.NamespaceCountKey)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements the update method via the interface
|
|
|
|
// @Summary Updates a namespace
|
|
|
|
// @Description Updates a namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @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."
|
|
|
|
// @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"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespace/{id} [post]
|
|
|
|
func (n *Namespace) Update() (err error) {
|
|
|
|
// Check if we have at least a name
|
2020-05-16 12:17:44 +02:00
|
|
|
if n.Title == "" {
|
2019-07-16 16:15:40 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the namespace exists
|
|
|
|
currentNamespace, err := GetNamespaceByID(n.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:50:39 +01:00
|
|
|
// Check if the namespace is archived and the update is not un-archiving it
|
|
|
|
if currentNamespace.IsArchived && n.IsArchived {
|
|
|
|
return ErrNamespaceIsArchived{NamespaceID: n.ID}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Check if the (new) owner exists
|
2020-03-15 22:50:39 +01:00
|
|
|
if n.Owner != nil {
|
|
|
|
n.OwnerID = n.Owner.ID
|
|
|
|
if currentNamespace.OwnerID != n.OwnerID {
|
|
|
|
n.Owner, err = user.GetUserByID(n.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-07-16 16:15:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:02:53 +01:00
|
|
|
// We need to specify the cols we want to update here to be able to un-archive lists
|
|
|
|
colsToUpdate := []string{
|
|
|
|
"name",
|
|
|
|
"is_archived",
|
|
|
|
}
|
|
|
|
if n.Description != "" {
|
|
|
|
colsToUpdate = append(colsToUpdate, "description")
|
|
|
|
}
|
2020-03-22 22:09:32 +01:00
|
|
|
if n.HexColor != "" {
|
|
|
|
colsToUpdate = append(colsToUpdate, "hex_color")
|
|
|
|
}
|
2020-03-22 19:02:53 +01:00
|
|
|
|
2019-07-16 16:15:40 +02:00
|
|
|
// Do the actual update
|
2020-03-22 19:02:53 +01:00
|
|
|
_, err = x.
|
|
|
|
ID(currentNamespace.ID).
|
|
|
|
Cols(colsToUpdate...).
|
|
|
|
Update(n)
|
2019-07-16 16:15:40 +02:00
|
|
|
return
|
|
|
|
}
|