update asynchronous

This commit is contained in:
Erik Winter 2021-09-03 10:02:08 +02:00
parent 516f0923f5
commit 22cad82870
5 changed files with 42 additions and 73 deletions

View File

@ -6,7 +6,6 @@ import (
"git.ewintr.nl/gte/internal/process" "git.ewintr.nl/gte/internal/process"
"git.ewintr.nl/gte/internal/storage" "git.ewintr.nl/gte/internal/storage"
"git.ewintr.nl/gte/internal/task" "git.ewintr.nl/gte/internal/task"
"git.ewintr.nl/gte/pkg/msend"
) )
// Done updates a task to be marked done // Done updates a task to be marked done
@ -19,8 +18,6 @@ func NewDone(localId int, conf *configuration.Configuration) (*Done, error) {
if err != nil { if err != nil {
return &Done{}, err return &Done{}, err
} }
disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP()))
localTask, err := local.FindByLocalId(localId) localTask, err := local.FindByLocalId(localId)
if err != nil { if err != nil {
return &Done{}, err return &Done{}, err
@ -31,7 +28,7 @@ func NewDone(localId int, conf *configuration.Configuration) (*Done, error) {
Fields: []string{task.FIELD_DONE}, Fields: []string{task.FIELD_DONE},
Done: true, Done: true,
} }
updater := process.NewUpdate(local, disp, localTask.Id, update) updater := process.NewUpdate(local, localTask.Id, update)
return &Done{ return &Done{
doner: updater, doner: updater,

View File

@ -9,7 +9,6 @@ import (
"git.ewintr.nl/gte/internal/process" "git.ewintr.nl/gte/internal/process"
"git.ewintr.nl/gte/internal/storage" "git.ewintr.nl/gte/internal/storage"
"git.ewintr.nl/gte/internal/task" "git.ewintr.nl/gte/internal/task"
"git.ewintr.nl/gte/pkg/msend"
) )
type Update struct { type Update struct {
@ -22,7 +21,6 @@ func NewUpdate(localId int, conf *configuration.Configuration, cmdArgs []string)
return &Update{}, err return &Update{}, err
} }
disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP()))
update, err := ParseTaskFieldArgs(cmdArgs) update, err := ParseTaskFieldArgs(cmdArgs)
if err != nil { if err != nil {
return &Update{}, err return &Update{}, err
@ -33,7 +31,7 @@ func NewUpdate(localId int, conf *configuration.Configuration, cmdArgs []string)
} }
update.ForVersion = localTask.Version update.ForVersion = localTask.Version
updater := process.NewUpdate(local, disp, localTask.Id, update) updater := process.NewUpdate(local, localTask.Id, update)
return &Update{ return &Update{
updater: updater, updater: updater,
@ -45,7 +43,7 @@ func (u *Update) Do() string {
return format.FormatError(err) return format.FormatError(err)
} }
return "message sent\n" return "local task updated\n"
} }
func ParseTaskFieldArgs(args []string) (*task.LocalUpdate, error) { func ParseTaskFieldArgs(args []string) (*task.LocalUpdate, error) {

View File

@ -12,18 +12,16 @@ var (
ErrUpdateTask = errors.New("could not update tsk") ErrUpdateTask = errors.New("could not update tsk")
) )
// Update dispatches an updated version of a task // Update updates a local task
type Update struct { type Update struct {
local storage.LocalRepository local storage.LocalRepository
disp *storage.Dispatcher
taskId string taskId string
update *task.LocalUpdate update *task.LocalUpdate
} }
func NewUpdate(local storage.LocalRepository, disp *storage.Dispatcher, taskId string, update *task.LocalUpdate) *Update { func NewUpdate(local storage.LocalRepository, taskId string, update *task.LocalUpdate) *Update {
return &Update{ return &Update{
local: local, local: local,
disp: disp,
taskId: taskId, taskId: taskId,
update: update, update: update,
} }
@ -38,11 +36,6 @@ func (u *Update) Process() error {
if err := u.local.SetLocalUpdate(tsk); err != nil { if err := u.local.SetLocalUpdate(tsk); err != nil {
return fmt.Errorf("%w: %v", ErrUpdateTask, err) return fmt.Errorf("%w: %v", ErrUpdateTask, err)
} }
// create a new version and send it away
tsk.ApplyUpdate()
if err := u.disp.Dispatch(&tsk.Task); err != nil {
return fmt.Errorf("%w: %v", ErrUpdateTask, err)
}
return nil return nil
} }

View File

@ -7,25 +7,12 @@ import (
"git.ewintr.nl/gte/internal/process" "git.ewintr.nl/gte/internal/process"
"git.ewintr.nl/gte/internal/storage" "git.ewintr.nl/gte/internal/storage"
"git.ewintr.nl/gte/internal/task" "git.ewintr.nl/gte/internal/task"
"git.ewintr.nl/gte/pkg/msend"
) )
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
task1 := &task.Task{
Id: "id-1",
Version: 2,
Project: "project1",
Action: "action1",
Due: task.NewDate(2021, 7, 29),
Folder: task.FOLDER_PLANNED,
}
local := storage.NewMemory()
allTasks := []*task.Task{task1}
for _, tc := range []struct { for _, tc := range []struct {
name string name string
updates *task.LocalUpdate updates *task.LocalUpdate
exp *task.Task
}{ }{
{ {
name: "done", name: "done",
@ -34,15 +21,6 @@ func TestUpdate(t *testing.T) {
Fields: []string{task.FIELD_DONE}, Fields: []string{task.FIELD_DONE},
Done: true, Done: true,
}, },
exp: &task.Task{
Id: "id-1",
Version: 2,
Project: "project1",
Action: "action1",
Due: task.NewDate(2021, 7, 29),
Folder: task.FOLDER_PLANNED,
Done: true,
},
}, },
{ {
name: "fields", name: "fields",
@ -53,29 +31,27 @@ func TestUpdate(t *testing.T) {
Action: "action2", Action: "action2",
Due: task.NewDate(2021, 8, 1), Due: task.NewDate(2021, 8, 1),
}, },
exp: &task.Task{
Id: "id-1",
Version: 2,
Project: "project2",
Action: "action2",
Due: task.NewDate(2021, 8, 1),
Folder: task.FOLDER_PLANNED,
},
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
test.OK(t, local.SetTasks(allTasks)) task1 := &task.Task{
out := msend.NewMemory() Id: "id-1",
disp := storage.NewDispatcher(out) Version: 2,
Project: "project1",
update := process.NewUpdate(local, disp, task1.Id, tc.updates) Action: "action1",
test.OK(t, update.Process()) Due: task.NewDate(2021, 7, 29),
expMsg := &msend.Message{ Folder: task.FOLDER_PLANNED,
Subject: tc.exp.FormatSubject(),
Body: tc.exp.FormatBody(),
} }
test.Assert(t, len(out.Messages) == 1, "amount of messages was not one") local := storage.NewMemory()
test.Equals(t, expMsg, out.Messages[0]) allTasks := []*task.Task{task1}
test.OK(t, local.SetTasks(allTasks))
update := process.NewUpdate(local, task1.Id, tc.updates)
test.OK(t, update.Process())
lt, err := local.FindById(task1.Id)
test.OK(t, err)
test.Equals(t, task.STATUS_UPDATED, lt.LocalStatus)
test.Equals(t, tc.updates, lt.LocalUpdate)
}) })
} }
} }

View File

@ -132,23 +132,23 @@ func (lu *LocalUpdate) Add(newUpdate *LocalUpdate) {
} }
func (lu LocalUpdate) Value() (driver.Value, error) { func (lu LocalUpdate) Value() (driver.Value, error) {
var recurStr string v := fmt.Sprintf("forversion: %d\n", lu.ForVersion)
if lu.Recur != nil { for _, f := range lu.Fields {
recurStr = lu.Recur.String() switch f {
case FIELD_ACTION:
v += fmt.Sprintf("action: %s\n", lu.Action)
case FIELD_PROJECT:
v += fmt.Sprintf("project: %s\n", lu.Project)
case FIELD_RECUR:
v += fmt.Sprintf("recur: %s\n", lu.Recur.String())
case FIELD_DUE:
v += fmt.Sprintf("due: %s\n", lu.Due)
case FIELD_DONE:
v += fmt.Sprintf("done: %t\n", lu.Done)
}
} }
return fmt.Sprintf(`forversion: %d return v, nil
action: %s
project: %s
recur: %s
due: %s
done: %t`,
lu.ForVersion,
lu.Action,
lu.Project,
recurStr,
lu.Due.String(),
lu.Done), nil
} }
func (lu *LocalUpdate) Scan(value interface{}) error { func (lu *LocalUpdate) Scan(value interface{}) error {
@ -172,15 +172,20 @@ func (lu *LocalUpdate) Scan(value interface{}) error {
newLu.ForVersion = d newLu.ForVersion = d
case "action": case "action":
newLu.Action = v newLu.Action = v
newLu.Fields = append(newLu.Fields, FIELD_ACTION)
case "project": case "project":
newLu.Project = v newLu.Project = v
newLu.Fields = append(newLu.Fields, FIELD_PROJECT)
case "recur": case "recur":
newLu.Recur = NewRecurrer(v) newLu.Recur = NewRecurrer(v)
newLu.Fields = append(newLu.Fields, FIELD_RECUR)
case "due": case "due":
newLu.Due = NewDateFromString(v) newLu.Due = NewDateFromString(v)
newLu.Fields = append(newLu.Fields, FIELD_DUE)
case "done": case "done":
if v == "true" { if v == "true" {
newLu.Done = true newLu.Done = true
newLu.Fields = append(newLu.Fields, FIELD_DONE)
} }
} }
} }