diff --git a/plan/command/sync.go b/plan/command/sync.go index 40075e0..a8922e7 100644 --- a/plan/command/sync.go +++ b/plan/command/sync.go @@ -2,6 +2,7 @@ package command import ( "encoding/json" + "errors" "fmt" "github.com/urfave/cli/v2" @@ -52,33 +53,46 @@ func Sync(client client.Client, syncRepo storage.Sync, localIDRepo storage.Local return fmt.Errorf("could not receive updates: %v", err) } - // import to local + updated := make([]item.Item, 0) + for _, ri := range recItems { + if ri.Deleted { + if err := localIDRepo.Delete(ri.ID); err != nil { + return fmt.Errorf("could not delete local id: %v", err) + } + if err := 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 := localIDRepo.FindAll() if err != nil { return fmt.Errorf("could not get local ids: %v", err) } - for _, ri := range recItems { + for _, u := range updated { var eBody item.EventBody - if err := json.Unmarshal([]byte(ri.Body), &eBody); err != nil { + if err := json.Unmarshal([]byte(u.Body), &eBody); err != nil { return fmt.Errorf("could not unmarshal event body: %v", err) } e := item.Event{ - ID: ri.ID, + ID: u.ID, EventBody: eBody, } - if err := eventRepo.Store(e); err != nil { return fmt.Errorf("could not store event: %v", err) } - lid, ok := lidMap[ri.ID] + lid, ok := lidMap[u.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) + + if err := localIDRepo.Store(u.ID, lid); err != nil { + return fmt.Errorf("could not store local id: %v", err) + } } } diff --git a/plan/main.go b/plan/main.go index 438e6ba..d48a2e0 100644 --- a/plan/main.go +++ b/plan/main.go @@ -30,7 +30,6 @@ func main() { os.Exit(1) } - fmt.Printf("%+v\n", conf) syncClient := client.New(conf.SyncURL, conf.ApiKey) app := &cli.App{