diff --git a/plan/command/add.go b/plan/command/add.go index 0e8724a..8f07e46 100644 --- a/plan/command/add.go +++ b/plan/command/add.go @@ -44,14 +44,14 @@ var AddCmd = &cli.Command{ }, } -func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event) *cli.Command { +func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command { AddCmd.Action = func(cCtx *cli.Context) error { - return Add(localRepo, eventRepo, cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) + return Add(localRepo, eventRepo, syncRepo, cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) } return AddCmd } -func Add(localIDRepo storage.LocalID, eventRepo storage.Event, nameStr, onStr, atStr, frStr string) error { +func Add(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, nameStr, onStr, atStr, frStr string) error { if nameStr == "" { return fmt.Errorf("%w: name is required", ErrInvalidArg) } @@ -103,5 +103,13 @@ func Add(localIDRepo storage.LocalID, eventRepo storage.Event, nameStr, onStr, a return fmt.Errorf("could not store local id: %v", err) } + it, err := e.Item() + if err != nil { + return fmt.Errorf("could not convert event to sync item: %v", err) + } + if err := syncRepo.Store(it); err != nil { + return fmt.Errorf("could not store sync item: %v", err) + } + return nil } diff --git a/plan/command/delete.go b/plan/command/delete.go index 4dfdb58..8e35b14 100644 --- a/plan/command/delete.go +++ b/plan/command/delete.go @@ -20,14 +20,14 @@ var DeleteCmd = &cli.Command{ }, } -func NewDeleteCmd(localRepo storage.LocalID, eventRepo storage.Event) *cli.Command { +func NewDeleteCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command { DeleteCmd.Action = func(cCtx *cli.Context) error { - return Delete(localRepo, eventRepo, cCtx.Int("localID")) + return Delete(localRepo, eventRepo, syncRepo, cCtx.Int("localID")) } return DeleteCmd } -func Delete(localRepo storage.LocalID, eventRepo storage.Event, localID int) error { +func Delete(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, localID int) error { var id string idMap, err := localRepo.FindAll() if err != nil { @@ -46,5 +46,17 @@ func Delete(localRepo storage.LocalID, eventRepo storage.Event, localID int) err return fmt.Errorf("could not delete event: %v", err) } + e, err := eventRepo.Find(id) + if err != nil { + return fmt.Errorf("could not get event: %v", err) + } + + it, err := e.Item() + if err != nil { + return fmt.Errorf("could not convert event to sync item: %v", err) + } + if err := syncRepo.Store(it); err != nil { + return fmt.Errorf("could not store sync item: %v", err) + } return nil } diff --git a/plan/command/update.go b/plan/command/update.go index e013251..883875a 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -41,14 +41,14 @@ var UpdateCmd = &cli.Command{ }, } -func NewUpdateCmd(localRepo storage.LocalID, eventRepo storage.Event) *cli.Command { +func NewUpdateCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command { UpdateCmd.Action = func(cCtx *cli.Context) error { - return Update(localRepo, eventRepo, cCtx.Int("localID"), cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) + return Update(localRepo, eventRepo, syncRepo, cCtx.Int("localID"), cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) } return UpdateCmd } -func Update(localRepo storage.LocalID, eventRepo storage.Event, localID int, nameStr, onStr, atStr, frStr string) error { +func Update(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, localID int, nameStr, onStr, atStr, frStr string) error { var id string idMap, err := localRepo.FindAll() if err != nil { @@ -99,5 +99,13 @@ func Update(localRepo storage.LocalID, eventRepo storage.Event, localID int, nam return fmt.Errorf("could not store event: %v", err) } + it, err := e.Item() + if err != nil { + return fmt.Errorf("could not convert event to sync item: %v", err) + } + if err := syncRepo.Store(it); err != nil { + return fmt.Errorf("could not store sync item: %v", err) + } + return nil } diff --git a/plan/main.go b/plan/main.go index 49d677f..438e6ba 100644 --- a/plan/main.go +++ b/plan/main.go @@ -37,10 +37,10 @@ func main() { Name: "plan", Usage: "Plan your day with events", Commands: []*cli.Command{ - command.NewAddCmd(localIDRepo, eventRepo), + command.NewAddCmd(localIDRepo, eventRepo, syncRepo), command.NewListCmd(localIDRepo, eventRepo), - command.NewUpdateCmd(localIDRepo, eventRepo), - command.NewDeleteCmd(localIDRepo, eventRepo), + command.NewUpdateCmd(localIDRepo, eventRepo, syncRepo), + command.NewDeleteCmd(localIDRepo, eventRepo, syncRepo), command.NewSyncCmd(syncClient, syncRepo, localIDRepo, eventRepo), }, }