api/pkg/models/list_create_update.go

58 lines
1.0 KiB
Go
Raw Normal View History

2018-06-10 12:43:35 +00: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
2018-09-19 06:45:23 +00:00
if list.NamespaceID != 0 {
_, 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
}
err = list.ReadOne()
return
}
2018-07-10 12:02:23 +00:00
// Update implements the update method of CRUDable
func (l *List) Update() (err error) {
2018-06-10 12:43:35 +00:00
// Check if it exists
if err = l.GetSimpleByID(); err != nil {
2018-06-10 12:43:35 +00:00
return
}
return CreateOrUpdateList(l)
}
2018-07-10 12:02:23 +00:00
// Create implements the create method of CRUDable
func (l *List) Create(doer *User) (err error) {
// Check rights
2018-10-31 12:42:38 +00:00
u, err := GetUserByID(doer.ID)
2018-07-04 06:15:47 +00:00
if err != nil {
2018-06-10 12:43:35 +00:00
return
}
2018-10-31 12:42:38 +00:00
l.OwnerID = u.ID
l.Owner.ID = u.ID
2018-07-12 21:18:50 +00:00
l.ID = 0 // Otherwise only the first time a new list would be created
2018-06-10 12:43:35 +00:00
return CreateOrUpdateList(l)
2018-07-10 12:02:23 +00:00
}