2018-06-10 19:49:40 +02:00
|
|
|
package models
|
|
|
|
|
2018-07-11 02:39:55 +02:00
|
|
|
// Create is the implementation to create a list item
|
2018-07-18 08:56:19 +02:00
|
|
|
func (i *ListItem) Create(doer *User) (err error) {
|
|
|
|
//i.ListID = lID
|
2018-07-11 02:39:55 +02:00
|
|
|
i.ID = 0
|
2018-06-10 19:49:40 +02:00
|
|
|
|
2018-07-12 23:07:03 +02:00
|
|
|
return createOrUpdateListItem(i, doer)
|
2018-07-11 02:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a list item
|
2018-07-21 15:17:02 +02:00
|
|
|
func (i *ListItem) Update() (err error) {
|
2018-07-12 23:07:03 +02:00
|
|
|
// Check if the item exists
|
2018-07-21 15:17:02 +02:00
|
|
|
_, err = GetListItemByID(i.ID)
|
2018-06-10 19:49:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-06-13 12:18:55 +02:00
|
|
|
|
2018-07-12 23:07:03 +02:00
|
|
|
return createOrUpdateListItem(i, &User{})
|
2018-06-10 19:49:40 +02:00
|
|
|
}
|
2018-07-11 02:13:53 +02:00
|
|
|
|
2018-07-11 02:39:55 +02:00
|
|
|
// 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) {
|
2018-07-11 02:13:53 +02:00
|
|
|
|
|
|
|
// Check if we have at least a text
|
|
|
|
if i.Text == "" {
|
|
|
|
return ErrListItemCannotBeEmpty{}
|
|
|
|
}
|
|
|
|
|
2018-07-11 02:39:55 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-07-11 02:39:55 +02:00
|
|
|
i.CreatedByID = user.ID
|
|
|
|
i.CreatedBy = user
|
|
|
|
_, err = x.Insert(i)
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|