2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-10 19:49:40 +02:00
|
|
|
package models
|
|
|
|
|
2018-09-10 19:34:34 +02:00
|
|
|
import (
|
2018-12-12 23:50:35 +01:00
|
|
|
"code.vikunja.io/api/pkg/metrics"
|
2018-12-01 00:26:56 +01:00
|
|
|
"code.vikunja.io/web"
|
2018-09-10 19:34:34 +02:00
|
|
|
"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
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @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."
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid task object provided."
|
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists/{id} [put]
|
2018-12-29 15:29:50 +01:00
|
|
|
func (t *ListTask) Create(a web.Auth) (err error) {
|
2018-12-01 00:26:56 +01:00
|
|
|
doer, err := getUserWithError(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-29 15:29:50 +01:00
|
|
|
t.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
|
2018-12-29 15:29:50 +01:00
|
|
|
if t.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-12-29 15:29:50 +01:00
|
|
|
l := &List{ID: t.ListID}
|
2018-10-06 13:05:29 +02:00
|
|
|
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-12-29 15:29:50 +01:00
|
|
|
t.CreatedByID = u.ID
|
|
|
|
t.CreatedBy = u
|
|
|
|
if _, err = x.Insert(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the assignees
|
|
|
|
if err := t.updateTaskAssignees(t.Assignees); err != nil {
|
2018-12-12 23:50:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.UpdateCount(1, metrics.TaskCountKey)
|
|
|
|
return
|
2018-09-10 07:41:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @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."
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid task object provided."
|
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the task (aka its list)"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /tasks/{id} [post]
|
2018-12-29 15:29:50 +01:00
|
|
|
func (t *ListTask) Update() (err error) {
|
2018-09-10 07:41:39 +02:00
|
|
|
// Check if the task exists
|
2018-12-29 15:29:50 +01:00
|
|
|
ot, err := GetListTaskByID(t.ID)
|
2018-09-10 07:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:49:46 +01:00
|
|
|
// When a repeating task is marked as done, we update all deadlines and reminders and set it as undone
|
2018-12-29 15:29:50 +01:00
|
|
|
updateDone(&ot, t)
|
|
|
|
|
|
|
|
// Update the assignees
|
|
|
|
if err := ot.updateTaskAssignees(t.Assignees); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 21:24:00 +01: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-12-29 15:29:50 +01:00
|
|
|
if err := mergo.Merge(&ot, t, mergo.WithOverride); err != nil {
|
2018-09-10 19:34:34 +02:00
|
|
|
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.
|
2018-12-29 15:29:50 +01:00
|
|
|
if t.Done == false {
|
2018-09-10 19:38:35 +02:00
|
|
|
ot.Done = false
|
|
|
|
}
|
|
|
|
|
2018-12-29 15:29:50 +01:00
|
|
|
_, err = x.ID(t.ID).
|
2018-12-22 19:06:14 +01:00
|
|
|
Cols("text",
|
|
|
|
"description",
|
|
|
|
"done",
|
|
|
|
"due_date_unix",
|
|
|
|
"reminders_unix",
|
|
|
|
"repeat_after",
|
|
|
|
"parent_task_id",
|
|
|
|
"priority",
|
|
|
|
"start_date_unix",
|
|
|
|
"end_date_unix").
|
|
|
|
Update(ot)
|
2018-12-29 15:29:50 +01:00
|
|
|
*t = ot
|
2018-09-10 19:22:00 +02:00
|
|
|
return
|
2018-07-11 02:13:53 +02:00
|
|
|
}
|
2018-12-28 22:49:46 +01:00
|
|
|
|
|
|
|
func updateDone(oldTask *ListTask, newTask *ListTask) {
|
|
|
|
if !oldTask.Done && newTask.Done && oldTask.RepeatAfter > 0 {
|
|
|
|
oldTask.DueDateUnix = oldTask.DueDateUnix + oldTask.RepeatAfter // assuming we'll save the old task (merged)
|
|
|
|
|
|
|
|
for in, r := range oldTask.RemindersUnix {
|
|
|
|
oldTask.RemindersUnix[in] = r + oldTask.RepeatAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
newTask.Done = false
|
|
|
|
}
|
|
|
|
}
|