This commit is contained in:
Erik Winter 2024-11-30 10:16:48 +01:00
parent 662d8de191
commit 7701a5615b
3 changed files with 32 additions and 14 deletions

View File

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

View File

@ -24,14 +24,24 @@ func TestDelete(t *testing.T) {
for _, tc := range []struct {
name string
localID int
main []string
flags map[string]string
expErr bool
}{
{
name: "not found",
localID: 5,
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) {
eventRepo := memory.NewEvent()
@ -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)
}

View File

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