delete
This commit is contained in:
parent
662d8de191
commit
7701a5615b
|
@ -50,10 +50,6 @@ func (del *Delete) do() error {
|
||||||
return fmt.Errorf("could not find local id")
|
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)
|
e, err := del.eventRepo.Find(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not get event: %v", err)
|
return fmt.Errorf("could not get event: %v", err)
|
||||||
|
@ -63,8 +59,18 @@ func (del *Delete) do() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not convert event to sync item: %v", err)
|
return fmt.Errorf("could not convert event to sync item: %v", err)
|
||||||
}
|
}
|
||||||
|
it.Deleted = true
|
||||||
if err := del.syncRepo.Store(it); err != nil {
|
if err := del.syncRepo.Store(it); err != nil {
|
||||||
return fmt.Errorf("could not store sync item: %v", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,14 +24,24 @@ func TestDelete(t *testing.T) {
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
localID int
|
main []string
|
||||||
|
flags map[string]string
|
||||||
expErr bool
|
expErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "not found",
|
name: "invalid",
|
||||||
localID: 5,
|
main: []string{"update"},
|
||||||
expErr: true,
|
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) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
eventRepo := memory.NewEvent()
|
eventRepo := memory.NewEvent()
|
||||||
|
@ -44,7 +54,9 @@ func TestDelete(t *testing.T) {
|
||||||
t.Errorf("exp nil, got %v", err)
|
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 {
|
if tc.expErr != actErr {
|
||||||
t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
t.Errorf("exp %v, got %v", tc.expErr, actErr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go-mod.ewintr.nl/planner/item"
|
"go-mod.ewintr.nl/planner/item"
|
||||||
|
"go-mod.ewintr.nl/planner/plan/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
|
@ -25,7 +25,7 @@ func (r *Event) Find(id string) (item.Event, error) {
|
||||||
|
|
||||||
event, exists := r.events[id]
|
event, exists := r.events[id]
|
||||||
if !exists {
|
if !exists {
|
||||||
return item.Event{}, errors.New("event not found")
|
return item.Event{}, storage.ErrNotFound
|
||||||
}
|
}
|
||||||
return event, nil
|
return event, nil
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ func (r *Event) Delete(id string) error {
|
||||||
defer r.mutex.Unlock()
|
defer r.mutex.Unlock()
|
||||||
|
|
||||||
if _, exists := r.events[id]; !exists {
|
if _, exists := r.events[id]; !exists {
|
||||||
return errors.New("event not found")
|
return storage.ErrNotFound
|
||||||
}
|
}
|
||||||
delete(r.events, id)
|
delete(r.events, id)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue