diff --git a/plan/command/delete.go b/plan/command/delete.go index 39ed52a..9490b56 100644 --- a/plan/command/delete.go +++ b/plan/command/delete.go @@ -50,10 +50,6 @@ func (del *Delete) do() error { return fmt.Errorf("could not find local id") } - if err := del.eventRepo.Delete(id); err != nil { - return fmt.Errorf("could not delete event: %v", err) - } - e, err := del.eventRepo.Find(id) if err != nil { return fmt.Errorf("could not get event: %v", err) @@ -63,8 +59,18 @@ func (del *Delete) do() error { if err != nil { return fmt.Errorf("could not convert event to sync item: %v", err) } + it.Deleted = true if err := del.syncRepo.Store(it); err != nil { return fmt.Errorf("could not store sync item: %v", err) } + + if err := del.localIDRepo.Delete(id); err != nil { + return fmt.Errorf("could not delete local id: %v", err) + } + + if err := del.eventRepo.Delete(id); err != nil { + return fmt.Errorf("could not delete event: %v", err) + } + return nil } diff --git a/plan/command/delete_test.go b/plan/command/delete_test.go index 772562a..607516a 100644 --- a/plan/command/delete_test.go +++ b/plan/command/delete_test.go @@ -23,14 +23,24 @@ func TestDelete(t *testing.T) { } for _, tc := range []struct { - name string - localID int - expErr bool + name string + main []string + flags map[string]string + expErr bool }{ { - name: "not found", - localID: 5, - expErr: true, + name: "invalid", + main: []string{"update"}, + expErr: true, + }, + { + name: "not found", + main: []string{"delete", "5"}, + expErr: true, + }, + { + name: "valid", + main: []string{"delete", "1"}, }, } { t.Run(tc.name, func(t *testing.T) { @@ -44,7 +54,9 @@ func TestDelete(t *testing.T) { t.Errorf("exp nil, got %v", err) } - actErr := command.Delete(localRepo, eventRepo, syncRepo, tc.localID) != nil + cmd := command.NewDelete(localRepo, eventRepo, syncRepo) + + actErr := cmd.Execute(tc.main, tc.flags) != nil if tc.expErr != actErr { t.Errorf("exp %v, got %v", tc.expErr, actErr) } diff --git a/plan/storage/memory/event.go b/plan/storage/memory/event.go index 32214ed..2a3afdb 100644 --- a/plan/storage/memory/event.go +++ b/plan/storage/memory/event.go @@ -1,11 +1,11 @@ package memory import ( - "errors" "sort" "sync" "go-mod.ewintr.nl/planner/item" + "go-mod.ewintr.nl/planner/plan/storage" ) type Event struct { @@ -25,7 +25,7 @@ func (r *Event) Find(id string) (item.Event, error) { event, exists := r.events[id] if !exists { - return item.Event{}, errors.New("event not found") + return item.Event{}, storage.ErrNotFound } return event, nil } @@ -59,7 +59,7 @@ func (r *Event) Delete(id string) error { defer r.mutex.Unlock() if _, exists := r.events[id]; !exists { - return errors.New("event not found") + return storage.ErrNotFound } delete(r.events, id)