api/models/list_items_create_update.go

52 lines
990 B
Go
Raw Normal View History

package models
2018-08-30 06:09:17 +00:00
// Create is the implementation to create a list task
func (i *ListTask) Create(doer *User) (err error) {
//i.ListID = lID
i.ID = 0
2018-08-30 06:09:17 +00:00
return createOrUpdateListTask(i, doer)
}
2018-08-30 06:09:17 +00:00
// Update updates a list task
func (i *ListTask) Update() (err error) {
// Check if the task exists
_, err = GetListTaskByID(i.ID)
if err != nil {
return
}
2018-08-30 06:09:17 +00:00
return createOrUpdateListTask(i, &User{})
}
// Helper function for creation or updating of new lists as both methods share most of their logic
2018-08-30 06:09:17 +00:00
func createOrUpdateListTask(i *ListTask, doer *User) (err error) {
// Check if we have at least a text
if i.Text == "" {
2018-08-30 06:09:17 +00:00
return ErrListTaskCannotBeEmpty{}
}
2018-07-27 12:47:52 +00: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-08-30 17:14:02 +00:00
user, err := GetUserByID(doer.ID)
2018-07-12 21:07:03 +00:00
if err != nil {
return err
}
i.CreatedByID = user.ID
i.CreatedBy = user
_, err = x.Insert(i)
}
return
}