diff options
| -rw-r--r-- | todo.go | 2 | ||||
| -rw-r--r-- | todolist/app.go | 23 | ||||
| -rw-r--r-- | todolist/file_store.go | 29 | ||||
| -rw-r--r-- | todolist/file_store_test.go | 19 | ||||
| -rw-r--r-- | todolist/store.go | 5 |
5 files changed, 76 insertions, 2 deletions
@@ -28,5 +28,7 @@ func routeInput(command string, input string) { app.ListTodos(input) case command == "a" || command == "add": app.AddTodo(input) + case command == "d" || command == "del": + app.DeleteTodo(input) } } diff --git a/todolist/app.go b/todolist/app.go index e0f11d2..74fb8dc 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -3,6 +3,7 @@ package todolist import ( "fmt" "regexp" + "strconv" ) type App struct { @@ -24,6 +25,28 @@ func (a *App) AddTodo(input string) { fmt.Println("Todo added.") } +func (a *App) DeleteTodo(input string) { + id := a.getId(input) + if id != -1 { + a.TodoStore.Delete(id) + a.TodoStore.Save() + fmt.Println("Todo deleted.") + } else { + fmt.Println("Could not find id.") + } +} + +func (a *App) getId(input string) int { + + re, _ := regexp.Compile("\\d+") + if re.MatchString(input) { + id, _ := strconv.Atoi(re.FindString(input)) + return id + } else { + return -1 + } +} + func (a *App) ListTodos(input string) { //filtered := NewFilter(a.TodoStore.Todos()).filter() grouped := a.getGroups(input) diff --git a/todolist/file_store.go b/todolist/file_store.go index 3b5fbfe..992aeb3 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -23,6 +23,35 @@ func (f *FileStore) Add(todo *Todo) { f.Data = append(f.Data, *todo) } +func (f *FileStore) FindById(id int) *Todo { + for _, todo := range f.Data { + if todo.Id == id { + return &todo + } + } + return nil +} + +func (f *FileStore) Delete(id int) { + i := -1 + for index, todo := range f.Data { + if todo.Id == id { + i = index + } + } + + f.Data = append(f.Data[:i], f.Data[i+1:]...) +} + +func (f *FileStore) IndexOf(todoToFind *Todo) int { + for i, todo := range f.Data { + if todo.Id == todoToFind.Id { + return i + } + } + return -1 +} + func (f *FileStore) Load() { data, err := ioutil.ReadFile(f.FileLocation) if err != nil { diff --git a/todolist/file_store_test.go b/todolist/file_store_test.go index 3e10491..d71c119 100644 --- a/todolist/file_store_test.go +++ b/todolist/file_store_test.go @@ -25,3 +25,22 @@ func TestNextId(t *testing.T) { store.Load() assert.Equal(3, store.NextId()) } + +func TestIndexOf(t *testing.T) { + assert := assert.New(t) + todo := &Todo{Subject: "Grant"} + store := &FileStore{FileLocation: "todos.json"} + store.Load() + + assert.Equal(-1, store.IndexOf(todo)) + assert.Equal(0, store.IndexOf(&store.Data[0])) +} + +func TestDelete(t *testing.T) { + assert := assert.New(t) + store := &FileStore{FileLocation: "todos.json"} + store.Load() + assert.Equal(2, len(store.Data)) + store.Delete(1) + assert.Equal(1, len(store.Data)) +} diff --git a/todolist/store.go b/todolist/store.go index d3f42d2..94f2f5e 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -7,7 +7,8 @@ type Store interface { Add(t *Todo) Save() - //Find(id int) Todo - //Remove(t *Todo) + IndexOf(t *Todo) int + FindById(id int) *Todo + Delete(id int) NextId() int } |
