From 54e9313ad14269f57a13b32aac59aea17f86d151 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Mon, 2 May 2016 06:34:38 -0400 Subject: Get completing todos to work correctly --- todo.go | 2 ++ todolist/app.go | 11 +++++++++++ todolist/file_store.go | 7 +++++++ todolist/file_store_test.go | 9 +++++++++ todolist/store.go | 6 ++++-- todolist/todos.json | 2 +- 6 files changed, 34 insertions(+), 3 deletions(-) diff --git a/todo.go b/todo.go index 992989a..813b1fc 100644 --- a/todo.go +++ b/todo.go @@ -30,5 +30,7 @@ func routeInput(command string, input string) { app.AddTodo(input) case command == "d" || command == "del": app.DeleteTodo(input) + case command == "c" || command == "complete": + app.CompleteTodo(input) } } diff --git a/todolist/app.go b/todolist/app.go index 267f482..fec64d2 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -36,6 +36,17 @@ func (a *App) DeleteTodo(input string) { } } +func (a *App) CompleteTodo(input string) { + id := a.getId(input) + if id != -1 { + a.TodoStore.Complete(id) + a.TodoStore.Save() + fmt.Println("Todo completed.") + } else { + fmt.Println("Could not find id.") + } +} + 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 579bf17..ce25b01 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -44,6 +44,13 @@ func (f *FileStore) Delete(id int) { f.Data = append(f.Data[:i], f.Data[i+1:]...) } +func (f *FileStore) Complete(id int) { + todo := f.FindById(id) + todo.Completed = true + f.Delete(id) + f.Data = append(f.Data, *todo) +} + func (f *FileStore) IndexOf(todoToFind *Todo) int { for i, todo := range f.Data { if todo.Id == todoToFind.Id { diff --git a/todolist/file_store_test.go b/todolist/file_store_test.go index d71c119..43b90d2 100644 --- a/todolist/file_store_test.go +++ b/todolist/file_store_test.go @@ -44,3 +44,12 @@ func TestDelete(t *testing.T) { store.Delete(1) assert.Equal(1, len(store.Data)) } + +func TestComplete(t *testing.T) { + assert := assert.New(t) + store := &FileStore{FileLocation: "todos.json"} + store.Load() + assert.Equal(false, store.FindById(1).Completed) + store.Complete(1) + assert.Equal(true, store.FindById(1).Completed) +} diff --git a/todolist/store.go b/todolist/store.go index 94f2f5e..066e3e3 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -2,13 +2,15 @@ package todolist type Store interface { Load() + Save() Todos() []Todo Add(t *Todo) - Save() + Delete(id int) + + Complete(id int) IndexOf(t *Todo) int FindById(id int) *Todo - Delete(id int) NextId() int } diff --git a/todolist/todos.json b/todolist/todos.json index d3da483..2dbce64 100644 --- a/todolist/todos.json +++ b/todolist/todos.json @@ -1 +1 @@ -[{"id":1,"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":true,"archived":true},{"id":2,"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":"","completed":false,"archived":false}] \ No newline at end of file +[{"id":1,"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":false,"archived":true},{"id":2,"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":"","completed":false,"archived":false}] \ No newline at end of file -- cgit v1.3