aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Ammons <gammons@gmail.com>2016-05-02 07:32:05 -0400
committerGrant Ammons <gammons@gmail.com>2016-05-02 07:32:05 -0400
commitb91b9d073680a0371af825238af230e9618aea12 (patch)
treebaf9ffba44af3c1b36c76dc9c0cc18e6402c03b1
parent34c89ec2ca4b458adb94534c4fae44c921181f59 (diff)
Add archive/unarchive
-rw-r--r--todo.go4
-rw-r--r--todolist/app.go22
-rw-r--r--todolist/file_store.go14
-rw-r--r--todolist/file_store_test.go17
-rw-r--r--todolist/store.go3
5 files changed, 60 insertions, 0 deletions
diff --git a/todo.go b/todo.go
index a945aba..7c567a6 100644
--- a/todo.go
+++ b/todo.go
@@ -34,5 +34,9 @@ func routeInput(command string, input string) {
app.CompleteTodo(input)
case command == "uc" || command == "uncomplete":
app.UncompleteTodo(input)
+ case command == "ar" || command == "archive":
+ app.ArchiveTodo(input)
+ case command == "uar" || command == "unarchive":
+ app.UnarchiveTodo(input)
}
}
diff --git a/todolist/app.go b/todolist/app.go
index 37961e0..598612c 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -58,6 +58,28 @@ func (a *App) UncompleteTodo(input string) {
}
}
+func (a *App) ArchiveTodo(input string) {
+ id := a.getId(input)
+ if id != -1 {
+ a.TodoStore.Archive(id)
+ a.TodoStore.Save()
+ fmt.Println("Todo archived.")
+ } else {
+ fmt.Println("Could not find id.")
+ }
+}
+
+func (a *App) UnarchiveTodo(input string) {
+ id := a.getId(input)
+ if id != -1 {
+ a.TodoStore.Unarchive(id)
+ a.TodoStore.Save()
+ fmt.Println("Todo unarchived.")
+ } 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 aa248d9..4556e6c 100644
--- a/todolist/file_store.go
+++ b/todolist/file_store.go
@@ -58,6 +58,20 @@ func (f *FileStore) Uncomplete(id int) {
f.Data = append(f.Data, *todo)
}
+func (f *FileStore) Archive(id int) {
+ todo := f.FindById(id)
+ todo.Archived = true
+ f.Delete(id)
+ f.Data = append(f.Data, *todo)
+}
+
+func (f *FileStore) Unarchive(id int) {
+ todo := f.FindById(id)
+ todo.Archived = false
+ 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 8817362..8bffa56 100644
--- a/todolist/file_store_test.go
+++ b/todolist/file_store_test.go
@@ -54,6 +54,23 @@ func TestComplete(t *testing.T) {
assert.Equal(true, store.FindById(1).Completed)
}
+func TestArchive(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ assert.Equal(false, store.FindById(2).Archived)
+ store.Archive(2)
+ assert.Equal(true, store.FindById(2).Archived)
+}
+func TestUnarchive(t *testing.T) {
+ assert := assert.New(t)
+ store := &FileStore{FileLocation: "todos.json"}
+ store.Load()
+ assert.Equal(true, store.FindById(1).Archived)
+ store.Unarchive(1)
+ assert.Equal(false, store.FindById(1).Archived)
+}
+
func TestUncomplete(t *testing.T) {
assert := assert.New(t)
store := &FileStore{FileLocation: "todos.json"}
diff --git a/todolist/store.go b/todolist/store.go
index 2397d08..d535036 100644
--- a/todolist/store.go
+++ b/todolist/store.go
@@ -11,6 +11,9 @@ type Store interface {
Complete(id int)
Uncomplete(id int)
+ Archive(id int)
+ Unarchive(id int)
+
IndexOf(t *Todo) int
FindById(id int) *Todo
NextId() int