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-06-10 14:43:35 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
import "code.vikunja.io/web"
|
|
|
|
|
2018-06-10 14:43:35 +02:00
|
|
|
// CreateOrUpdateList updates a list or creates it if it doesn't exist
|
|
|
|
func CreateOrUpdateList(list *List) (err error) {
|
2018-07-09 23:17:19 +02:00
|
|
|
|
2018-07-26 10:29:09 +02:00
|
|
|
// Check we have at least a title
|
|
|
|
if list.Title == "" {
|
|
|
|
return ErrListTitleCannotBeEmpty{}
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:49:33 +02:00
|
|
|
// Check if the namespace exists
|
2018-09-19 08:45:23 +02:00
|
|
|
if list.NamespaceID != 0 {
|
|
|
|
_, err = GetNamespaceByID(list.NamespaceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-26 10:49:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 23:17:19 +02:00
|
|
|
if list.ID == 0 {
|
|
|
|
_, err = x.Insert(list)
|
|
|
|
} else {
|
|
|
|
_, err = x.ID(list.ID).Update(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:05:29 +02:00
|
|
|
err = list.ReadOne()
|
2018-07-09 23:17:19 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// Update implements the update method of CRUDable
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Updates a list
|
|
|
|
// @Description Updates a list. This does not include adding a task (see below).
|
|
|
|
// @tags list
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param id path int true "List ID"
|
|
|
|
// @Param list body models.List true "The list with updated values you want to update."
|
|
|
|
// @Success 200 {object} models.List "The updated list."
|
|
|
|
// @Failure 400 {object} models.HTTPError "Invalid list object provided."
|
|
|
|
// @Failure 403 {object} models.HTTPError "The user does not have access to the list"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists/{id} [post]
|
2018-07-21 15:17:02 +02:00
|
|
|
func (l *List) Update() (err error) {
|
2018-06-10 14:43:35 +02:00
|
|
|
// Check if it exists
|
2018-10-06 13:05:29 +02:00
|
|
|
if err = l.GetSimpleByID(); err != nil {
|
2018-06-10 14:43:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:17:19 +02:00
|
|
|
return CreateOrUpdateList(l)
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// Create implements the create method of CRUDable
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Creates a new list
|
|
|
|
// @Description Creates a new list in a given namespace. The user needs write-access to the namespace.
|
|
|
|
// @tags list
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param namespaceID path int true "Namespace ID"
|
|
|
|
// @Param list body models.List true "The list you want to create."
|
|
|
|
// @Success 200 {object} models.List "The created list."
|
|
|
|
// @Failure 400 {object} models.HTTPError "Invalid list object provided."
|
|
|
|
// @Failure 403 {object} models.HTTPError "The user does not have access to the list"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{namespaceID}/lists [put]
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) Create(a web.Auth) (err error) {
|
|
|
|
doer, err := getUserWithError(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:17:19 +02:00
|
|
|
// Check rights
|
2018-10-31 13:42:38 +01:00
|
|
|
u, err := GetUserByID(doer.ID)
|
2018-07-04 08:15:47 +02:00
|
|
|
if err != nil {
|
2018-06-10 14:43:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
l.OwnerID = u.ID
|
|
|
|
l.Owner.ID = u.ID
|
2018-07-12 23:18:50 +02:00
|
|
|
l.ID = 0 // Otherwise only the first time a new list would be created
|
2018-06-10 14:43:35 +02:00
|
|
|
|
2018-07-09 23:17:19 +02:00
|
|
|
return CreateOrUpdateList(l)
|
2018-07-10 14:02:23 +02:00
|
|
|
}
|