api/pkg/models/list_tasks_create_update.go

57 lines
1.4 KiB
Go
Raw Normal View History

package models
import (
"github.com/imdario/mergo"
)
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.ID = 0
// 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
l := &List{ID: i.ListID}
if err = l.GetSimpleByID(); err != nil {
2018-07-27 12:47:52 +00:00
return
}
2018-10-31 12:42:38 +00:00
u, err := GetUserByID(doer.ID)
if err != nil {
return err
}
2018-07-12 21:07:03 +00:00
2018-10-31 12:42:38 +00:00
i.CreatedByID = u.ID
i.CreatedBy = u
2018-09-22 09:06:39 +00:00
_, err = x.Cols("text", "description", "done", "due_date_unix", "reminder_unix", "created_by_id", "list_id", "created", "updated").Insert(i)
return err
}
// Update updates a list task
func (i *ListTask) Update() (err error) {
// Check if the task exists
ot, err := GetListTaskByID(i.ID)
if err != nil {
return
}
// For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand
2018-10-31 12:42:38 +00:00
// Which is why we merge the actual task struct with the one we got from the
// The user struct overrides values in the actual one.
if err := mergo.Merge(&ot, i, mergo.WithOverride); err != nil {
return err
}
2018-09-10 17:22:00 +00:00
// And because a false is considered to be a null value, we need to explicitly check that case here.
if i.Done == false {
ot.Done = false
}
_, err = x.ID(i.ID).Cols("text", "description", "done", "due_date_unix", "reminder_unix").Update(ot)
*i = ot
2018-09-10 17:22:00 +00:00
return
}