vikunja-api/models/list_items_create_update.go

52 lines
993 B
Go
Raw Normal View History

package models
// Create is the implementation to create a list item
func (i *ListItem) Create(doer *User) (err error) {
//i.ListID = lID
i.ID = 0
2018-07-12 23:07:03 +02:00
return createOrUpdateListItem(i, doer)
}
// Update updates a list item
func (i *ListItem) Update() (err error) {
2018-07-12 23:07:03 +02:00
// Check if the item exists
_, err = GetListItemByID(i.ID)
if err != nil {
return
}
2018-07-12 23:07:03 +02:00
return createOrUpdateListItem(i, &User{})
}
// Helper function for creation or updating of new lists as both methods share most of their logic
2018-07-12 23:07:03 +02:00
func createOrUpdateListItem(i *ListItem, doer *User) (err error) {
// Check if we have at least a text
if i.Text == "" {
return ErrListItemCannotBeEmpty{}
}
2018-07-27 14:47:52 +02:00
// Check if the list exists
_, err = GetListByID(i.ListID)
if err != nil {
return
}
// Do the update
if i.ID != 0 {
_, err = x.ID(i.ID).Update(i)
} else {
2018-07-12 23:07:03 +02:00
user, _, err := GetUserByID(doer.ID)
if err != nil {
return err
}
i.CreatedByID = user.ID
i.CreatedBy = user
_, err = x.Insert(i)
}
return
}