2018-06-10 19:49:40 +02:00
|
|
|
package models
|
|
|
|
|
2018-09-10 19:34:34 +02:00
|
|
|
import (
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
)
|
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// Create is the implementation to create a list task
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Create a task
|
|
|
|
// @Description Inserts a task into a list.
|
|
|
|
// @tags task
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param id path int true "List ID"
|
|
|
|
// @Param task body models.ListTask true "The task object"
|
|
|
|
// @Success 200 {object} models.ListTask "The created task object."
|
|
|
|
// @Failure 400 {object} models.HTTPError "Invalid task object provided."
|
|
|
|
// @Failure 403 {object} models.HTTPError "The user does not have access to the list"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists/{id} [put]
|
2018-08-30 08:09:17 +02:00
|
|
|
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
|
2018-10-06 13:05:29 +02:00
|
|
|
l := &List{ID: i.ListID}
|
|
|
|
if err = l.GetSimpleByID(); err != nil {
|
2018-07-27 14:47:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
u, err := GetUserByID(doer.ID)
|
2018-09-10 07:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 23:07:03 +02:00
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
i.CreatedByID = u.ID
|
|
|
|
i.CreatedBy = u
|
2018-09-22 11:06:39 +02:00
|
|
|
_, err = x.Cols("text", "description", "done", "due_date_unix", "reminder_unix", "created_by_id", "list_id", "created", "updated").Insert(i)
|
2018-09-10 07:41:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates a list task
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Summary Update a task
|
|
|
|
// @Description Updates a task. This includes marking it as done.
|
|
|
|
// @tags task
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param id path int true "Task ID"
|
|
|
|
// @Param task body models.ListTask true "The task object"
|
|
|
|
// @Success 200 {object} models.ListTask "The updated task object."
|
|
|
|
// @Failure 400 {object} models.HTTPError "Invalid task object provided."
|
|
|
|
// @Failure 403 {object} models.HTTPError "The user does not have access to the task (aka its list)"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /tasks/{id} [post]
|
2018-09-10 07:41:39 +02:00
|
|
|
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
|
|
|
// For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand
|
2018-10-31 13:42:38 +01: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.
|
2018-09-10 19:34:34 +02:00
|
|
|
if err := mergo.Merge(&ot, i, mergo.WithOverride); err != nil {
|
|
|
|
return err
|
2018-09-10 07:59:45 +02:00
|
|
|
}
|
2018-09-10 19:22:00 +02:00
|
|
|
|
2018-09-10 19:38:35 +02: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
|
|
|
|
}
|
|
|
|
|
2018-09-10 19:34:34 +02:00
|
|
|
_, err = x.ID(i.ID).Cols("text", "description", "done", "due_date_unix", "reminder_unix").Update(ot)
|
|
|
|
*i = ot
|
2018-09-10 19:22:00 +02:00
|
|
|
return
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|