planner/plan/command/sync.go

105 lines
2.6 KiB
Go

package command
import (
"encoding/json"
"errors"
"fmt"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
"go-mod.ewintr.nl/planner/sync/client"
)
type Sync struct {
client client.Client
syncRepo storage.Sync
localIDRepo storage.LocalID
eventRepo storage.Event
}
func NewSync(client client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) Command {
return &Sync{
client: client,
syncRepo: syncRepo,
localIDRepo: localIDRepo,
eventRepo: eventRepo,
}
}
func (sync *Sync) Execute(main []string, flags map[string]string) error {
if len(main) == 0 || main[0] != "sync" {
return ErrWrongCommand
}
return sync.do()
}
func (sync *Sync) do() error {
// local new and updated
sendItems, err := sync.syncRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get updated items: %v", err)
}
if err := sync.client.Update(sendItems); err != nil {
return fmt.Errorf("could not send updated items: %v", err)
}
if err := sync.syncRepo.DeleteAll(); err != nil {
return fmt.Errorf("could not clear updated items: %v", err)
}
// get new/updated items
ts, err := sync.syncRepo.LastUpdate()
if err != nil {
return fmt.Errorf("could not find timestamp of last update: %v", err)
}
recItems, err := sync.client.Updated([]item.Kind{item.KindEvent}, ts)
if err != nil {
return fmt.Errorf("could not receive updates: %v", err)
}
updated := make([]item.Item, 0)
for _, ri := range recItems {
if ri.Deleted {
if err := sync.localIDRepo.Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("could not delete local id: %v", err)
}
if err := sync.eventRepo.Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("could not delete event: %v", err)
}
continue
}
updated = append(updated, ri)
}
lidMap, err := sync.localIDRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
for _, u := range updated {
var eBody item.EventBody
if err := json.Unmarshal([]byte(u.Body), &eBody); err != nil {
return fmt.Errorf("could not unmarshal event body: %v", err)
}
e := item.Event{
ID: u.ID,
EventBody: eBody,
}
if err := sync.eventRepo.Store(e); err != nil {
return fmt.Errorf("could not store event: %v", err)
}
lid, ok := lidMap[u.ID]
if !ok {
lid, err = sync.localIDRepo.Next()
if err != nil {
return fmt.Errorf("could not get next local id: %v", err)
}
if err := sync.localIDRepo.Store(u.ID, lid); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
}
}
return nil
}