From 197fe227a4fd70e85b88212aa3d2b353bc25dd8d Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 17 Jan 2021 19:48:05 +0100 Subject: [PATCH] add support for changing task item. --- api/task.go | 18 ++++++++++++++++++ models/task.go | 4 ++++ svelte-ui/src/forms/TaskForm.svelte | 4 ++-- svelte-ui/src/models/task.ts | 1 + 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/api/task.go b/api/task.go index 2fc84ce..73238e2 100644 --- a/api/task.go +++ b/api/task.go @@ -86,6 +86,24 @@ func Task(g *gin.RouterGroup, db database.Database) { return nil, err } + if update.ItemID != nil && *update.ItemID != task.ItemID { + _, err := l.FindItem(c.Request.Context(), *update.ItemID) + if err != nil { + return nil, err + } + + logs, err := l.ListLogs(c.Request.Context(), models.LogFilter{ + TaskIDs: []string{task.ID}, + }) + if err != nil { + return nil, err + } + + if len(logs) > 0 { + return nil, slerrors.Forbidden("You cannot change the item if the task has logs.") + } + } + task.Update(update) if task.EndTime != nil && task.EndTime.Before(task.CreatedTime) { return nil, slerrors.BadRequest("Task end time must be later than it was created.") diff --git a/models/task.go b/models/task.go index 7a00139..ea8c756 100644 --- a/models/task.go +++ b/models/task.go @@ -20,6 +20,9 @@ type Task struct { } func (task *Task) Update(update TaskUpdate) { + if update.ItemID != nil { + task.ItemID = *update.ItemID + } if update.ItemAmount != nil { task.ItemAmount = *update.ItemAmount } @@ -42,6 +45,7 @@ func (task *Task) Update(update TaskUpdate) { } type TaskUpdate struct { + ItemID *string `json:"itemId"` ItemAmount *int `json:"itemAmount"` Name *string `json:"name"` Description *string `json:"description"` diff --git a/svelte-ui/src/forms/TaskForm.svelte b/svelte-ui/src/forms/TaskForm.svelte index 0371d33..ca7bc5e 100644 --- a/svelte-ui/src/forms/TaskForm.svelte +++ b/svelte-ui/src/forms/TaskForm.svelte @@ -86,7 +86,7 @@ clearEndTime: endTime == "", active: !completed, - name, description, itemAmount, + itemId, name, description, itemAmount, }).then(() => { markStale("goal", "project", "task"); modalStore.close(); @@ -108,7 +108,7 @@