2018-06-10 19:49:40 +02:00
|
|
|
package models
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Create is the implementation to create a list task
|
|
|
|
func (i *ListTask) Create(doer *User) (err error) {
|
2018-07-11 02:39:55 +02:00
|
|
|
i.ID = 0
|
2018-06-10 19:49:40 +02:00
|
|
|
|
2018-07-11 02:13:53 +02:00
|
|
|
// Check if we have at least a text
|
|
|
|
if i.Text == "" {
|
2018-08-30 08:09:17 +02:00
|
|
|
return ErrListTaskCannotBeEmpty{}
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 14:47:52 +02:00
|
|
|
// Check if the list exists
|
|
|
|
_, err = GetListByID(i.ListID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:41:39 +02:00
|
|
|
user, err := GetUserByID(doer.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 23:07:03 +02:00
|
|
|
|
2018-09-10 07:41:39 +02:00
|
|
|
i.CreatedByID = user.ID
|
|
|
|
i.CreatedBy = user
|
|
|
|
_, err = x.Insert(i)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a list task
|
|
|
|
func (i *ListTask) Update() (err error) {
|
|
|
|
// Check if the task exists
|
2018-09-10 07:59:45 +02:00
|
|
|
ot, err := GetListTaskByID(i.ID)
|
2018-09-10 07:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-10 07:59:45 +02:00
|
|
|
// If we dont have a text, use the one from the original task
|
|
|
|
if i.Text == "" {
|
|
|
|
i.Text = ot.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
// For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand
|
|
|
|
if i.Done != ot.Done {
|
|
|
|
_, err = x.ID(i.ID).Cols("done").Update(i)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
_, err = x.ID(i.ID).Update(i)
|
|
|
|
return
|
|
|
|
}
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|