This commit is contained in:
Erik Winter 2024-10-25 08:58:31 +02:00
parent 5bafda072b
commit f7cc1082b9
4 changed files with 40 additions and 12 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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),
},
}