vikunja-api/models/list_create_update.go

58 lines
1 KiB
Go
Raw Normal View History

2018-06-10 14:43:35 +02:00
package models
// CreateOrUpdateList updates a list or creates it if it doesn't exist
func CreateOrUpdateList(list *List) (err error) {
// Check we have at least a title
if list.Title == "" {
return ErrListTitleCannotBeEmpty{}
}
// Check if the namespace exists
_, err = GetNamespaceByID(list.NamespaceID)
if err != nil {
return err
}
if list.ID == 0 {
_, err = x.Insert(list)
} else {
_, err = x.ID(list.ID).Update(list)
}
if err != nil {
return
}
*list, err = GetListByID(list.ID)
return
}
2018-07-10 14:02:23 +02:00
// Update implements the update method of CRUDable
func (l *List) Update() (err error) {
2018-06-10 14:43:35 +02:00
// Check if it exists
2018-07-12 23:07:03 +02:00
_, err = GetListByID(l.ID)
2018-06-10 14:43:35 +02:00
if err != nil {
return
}
return CreateOrUpdateList(l)
}
2018-07-10 14:02:23 +02:00
// Create implements the create method of CRUDable
func (l *List) Create(doer *User) (err error) {
// Check rights
user, _, err := GetUserByID(doer.ID)
2018-07-04 08:15:47 +02:00
if err != nil {
2018-06-10 14:43:35 +02:00
return
}
l.OwnerID = user.ID
l.Owner.ID = user.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
return CreateOrUpdateList(l)
2018-07-10 14:02:23 +02:00
}