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 {
|
2021-08-20 13:46:56 +02:00
|
|
|
local storage.LocalRepository
|
|
|
|
disp *storage.Dispatcher
|
|
|
|
taskId string
|
2021-08-22 13:29:04 +02:00
|
|
|
update *task.LocalUpdate
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 13:29:04 +02:00
|
|
|
func NewUpdate(local storage.LocalRepository, disp *storage.Dispatcher, taskId string, update *task.LocalUpdate) *Update {
|
2021-07-10 12:30:38 +02:00
|
|
|
return &Update{
|
2021-08-20 13:46:56 +02:00
|
|
|
local: local,
|
|
|
|
disp: disp,
|
|
|
|
taskId: taskId,
|
|
|
|
update: update,
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-08-22 13:29:04 +02:00
|
|
|
tsk.AddUpdate(u.update)
|
|
|
|
if err := u.local.SetLocalUpdate(tsk); err != nil {
|
2021-08-21 20:47:29 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrUpdateTask, err)
|
|
|
|
}
|
2021-08-22 16:45:03 +02:00
|
|
|
// create a new version and send it away
|
2021-08-22 13:29:04 +02:00
|
|
|
tsk.ApplyUpdate()
|
2021-08-20 09:06:35 +02:00
|
|
|
if err := u.disp.Dispatch(&tsk.Task); err != nil {
|
2021-07-10 12:30:38 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrUpdateTask, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|