2021-07-10 12:30:38 +02:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.ewintr.nl/gte/internal/storage"
|
2021-07-29 07:01:24 +02:00
|
|
|
"git.ewintr.nl/gte/internal/task"
|
2021-07-10 12:30:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-07-29 07:01:24 +02:00
|
|
|
ErrUpdateTask = errors.New("could not update tsk")
|
2021-07-10 12:30:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Update dispatches an updated version of a task
|
|
|
|
type Update struct {
|
|
|
|
local storage.LocalRepository
|
|
|
|
disp *storage.Dispatcher
|
|
|
|
taskId string
|
|
|
|
updates UpdateFields
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateFields map[string]string
|
|
|
|
|
|
|
|
func NewUpdate(local storage.LocalRepository, disp *storage.Dispatcher, taskId string, updates UpdateFields) *Update {
|
|
|
|
return &Update{
|
|
|
|
local: local,
|
|
|
|
disp: disp,
|
|
|
|
taskId: taskId,
|
|
|
|
updates: updates,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Update) Process() error {
|
2021-07-29 07:01:24 +02:00
|
|
|
tsk, err := u.local.FindById(u.taskId)
|
2021-07-10 12:30:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrUpdateTask, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range u.updates {
|
|
|
|
switch k {
|
2021-07-29 07:01:24 +02:00
|
|
|
case task.FIELD_DONE:
|
2021-07-10 12:30:38 +02:00
|
|
|
if v == "true" {
|
2021-07-29 07:01:24 +02:00
|
|
|
tsk.Done = true
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
2021-07-29 07:01:24 +02:00
|
|
|
case task.FIELD_DUE:
|
|
|
|
tsk.Due = task.NewDateFromString(v)
|
|
|
|
case task.FIELD_ACTION:
|
|
|
|
tsk.Action = v
|
|
|
|
case task.FIELD_PROJECT:
|
|
|
|
tsk.Project = v
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:01:24 +02:00
|
|
|
if err := u.disp.Dispatch(tsk); err != nil {
|
2021-07-10 12:30:38 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrUpdateTask, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|