Added functions for adding items to a todolist
This commit is contained in:
37
models/list_items_create_update.go
Normal file
37
models/list_items_create_update.go
Normal file
@ -0,0 +1,37 @@
|
||||
package models
|
||||
|
||||
// CreateOrUpdateListItem adds or updates a todo item to a list
|
||||
func CreateOrUpdateListItem(item *ListItem) (err error) {
|
||||
|
||||
// Check if we have at least a text
|
||||
if item.Text == "" {
|
||||
return ErrListItemCannotBeEmpty{}
|
||||
}
|
||||
|
||||
// Check if the list exists
|
||||
_, err = GetListByID(item.ListID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the user exists
|
||||
_, _, err = GetUserByID(item.CreatedBy.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
item.CreatedByID = item.CreatedBy.ID
|
||||
|
||||
if item.ID != 0 {
|
||||
_, err = x.ID(item.ID).Update(item)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = x.Insert(item)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user