wip
This commit is contained in:
parent
ef7beacec8
commit
86c4cdc957
|
@ -1,7 +1,11 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
"go-mod.ewintr.nl/planner/item"
|
||||||
"go-mod.ewintr.nl/planner/plan/storage"
|
"go-mod.ewintr.nl/planner/plan/storage"
|
||||||
"go-mod.ewintr.nl/planner/sync/client"
|
"go-mod.ewintr.nl/planner/sync/client"
|
||||||
)
|
)
|
||||||
|
@ -18,25 +22,63 @@ var SyncCmd = &cli.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSyncCmd(client *client.Client, localRepo storage.LocalID, eventRepo storage.Event) *cli.Command {
|
func NewSyncCmd(client *client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event) *cli.Command {
|
||||||
SyncCmd.Action = func(cCtx *cli.Context) error {
|
SyncCmd.Action = func(cCtx *cli.Context) error {
|
||||||
return Sync(client, localRepo, eventRepo, cCtx.Bool("full"))
|
return Sync(client, syncRepo, localIDRepo, eventRepo, cCtx.Bool("full"))
|
||||||
}
|
}
|
||||||
return SyncCmd
|
return SyncCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func Sync(client *client.Client, localRepo storage.LocalID, eventRepo storage.Event, full bool) error {
|
func Sync(client *client.Client, syncRepo storage.Sync, localIDRepo storage.LocalID, eventRepo storage.Event, full bool) error {
|
||||||
// find local new and updated
|
// find local new and updated
|
||||||
|
sendItems, err := syncRepo.FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not get updated items: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// send new and updated
|
// send new and updated
|
||||||
//
|
if err := client.Update(sendItems); err != nil {
|
||||||
|
return fmt.Errorf("could not send updated items: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := syncRepo.DeleteAll(); err != nil {
|
||||||
|
return fmt.Errorf("could not clear updated items: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// get new/updated items
|
// get new/updated items
|
||||||
|
recItems, err := client.Updated(item.KindEvent)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not receive updates: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// delete deleted
|
lidMap, err := localIDRepo.FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not get local ids: %v", err)
|
||||||
|
}
|
||||||
|
for _, ri := range recItems {
|
||||||
|
var eBody item.EventBody
|
||||||
|
if err := json.Unmarshal([]byte(ri.Body), eBody); err != nil {
|
||||||
|
return fmt.Errorf("could not unmarshal event body: %v", err)
|
||||||
|
}
|
||||||
|
e := item.Event{
|
||||||
|
ID: ri.ID,
|
||||||
|
EventBody: eBody,
|
||||||
|
}
|
||||||
|
|
||||||
// update existing
|
if err := eventRepo.Store(e); err != nil {
|
||||||
|
return fmt.Errorf("could not store event: %v", err)
|
||||||
// localid and add new
|
}
|
||||||
|
lid, ok := lidMap[ri.ID]
|
||||||
|
if !ok {
|
||||||
|
lid, err = localIDRepo.Next()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not get next local id: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := localIDRepo.Store(ri.ID, lid); err != nil {
|
||||||
|
return fmt.Errorf("could not store local id: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,12 @@ type LocalID interface {
|
||||||
Delete(id string) error
|
Delete(id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Sync interface {
|
||||||
|
FindAll() ([]item.Item, error)
|
||||||
|
Store(i item.Item) error
|
||||||
|
DeleteAll() error
|
||||||
|
}
|
||||||
|
|
||||||
type Event interface {
|
type Event interface {
|
||||||
Store(event item.Event) error
|
Store(event item.Event) error
|
||||||
Find(id string) (item.Event, error)
|
Find(id string) (item.Event, error)
|
||||||
|
|
Loading…
Reference in New Issue