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) {
|
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
|
|
|
|
_, err = GetNamespaceByID(list.NamespaceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
*list, err = GetListByID(list.ID)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// Update implements the update method of CRUDable
|
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-07-12 23:07:03 +02:00
|
|
|
_, err = GetListByID(l.ID)
|
2018-06-10 14:43:35 +02:00
|
|
|
if err != nil {
|
|
|
|
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-07-18 08:56:19 +02:00
|
|
|
func (l *List) Create(doer *User) (err error) {
|
2018-07-09 23:17:19 +02:00
|
|
|
// Check rights
|
2018-08-30 19:14:02 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-24 17:29:13 +02:00
|
|
|
l.OwnerID = user.ID
|
2018-07-09 23:17:19 +02:00
|
|
|
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
|
|
|
|
2018-07-09 23:17:19 +02:00
|
|
|
return CreateOrUpdateList(l)
|
2018-07-10 14:02:23 +02:00
|
|
|
}
|