aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-05-02 06:34:38 -0400
committerGrant Ammons <gammons@gmail.com>2016-05-02 06:34:38 -0400
commit54e9313ad14269f57a13b32aac59aea17f86d151 (patch)
tree9c6364af5e93d6b80df893d317894f60d6662940
parentb1fc3359eb7498236621d1aa6a9ff18f8cce73c0 (diff)
Get completing todos to work correctly
-rw-r--r--todo.go2
-rw-r--r--todolist/app.go11
-rw-r--r--todolist/file_store.go7
-rw-r--r--todolist/file_store_test.go9
-rw-r--r--todolist/store.go6
-rw-r--r--todolist/todos.json2
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