vikunja-api/models/list_items_create_update.go

44 lines
871 B
Go
Raw Normal View History

package models
// CreateOrUpdateListItem adds or updates a todo item to a list
func CreateOrUpdateListItem(item *ListItem) (newItem *ListItem, err error) {
// Check if the list exists
_, err = GetListByID(item.ListID)
if err != nil {
return
}
// Check if the user exists
2018-06-12 17:57:38 +00:00
user, _, err := GetUserByID(item.CreatedBy.ID)
if err != nil {
return
}
item.CreatedByID = item.CreatedBy.ID
2018-06-12 17:57:38 +00:00
item.CreatedBy = user
2018-06-13 11:45:22 +00:00
// TODO: Check if the user has the right to add/update an item to that list
if item.ID != 0 {
_, err = x.ID(item.ID).Update(item)
if err != nil {
return
}
} else {
// Check if we have at least a text
if item.Text == "" {
return newItem, ErrListItemCannotBeEmpty{}
}
2018-06-13 11:45:22 +00:00
_, err = x.Insert(item)
if err != nil {
return
}
}
// Get the new/updated item
finalItem, err := GetListItemByID(item.ID)
return &finalItem, err
}