aboutsummaryrefslogtreecommitdiffstats
path: root/todolist
diff options
context:
space:
mode:
Diffstat (limited to 'todolist')
-rw-r--r--todolist/app.go7
-rw-r--r--todolist/todo_list.go34
-rw-r--r--todolist/todo_list_test.go51
3 files changed, 90 insertions, 2 deletions
diff --git a/todolist/app.go b/todolist/app.go
index 1ab6b20..20a35ef 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -183,6 +183,13 @@ func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos {
return grouped
}
+func (a *App) GarbageCollect() {
+ a.Load()
+ a.TodoList.GarbageCollect()
+ a.Save()
+ fmt.Println("Garbage collection complete.")
+}
+
func (a *App) Load() error {
todos, err := a.TodoStore.Load()
if err != nil {
diff --git a/todolist/todo_list.go b/todolist/todo_list.go
index d7e1f83..ea8ecbe 100644
--- a/todolist/todo_list.go
+++ b/todolist/todo_list.go
@@ -78,14 +78,32 @@ func (t *TodoList) Todos() []*Todo {
return t.Data
}
-func (t *TodoList) NextId() int {
+func (t *TodoList) MaxId() int {
maxId := 0
for _, todo := range t.Data {
if todo.Id > maxId {
maxId = todo.Id
}
}
- return maxId + 1
+ return maxId
+}
+
+func (t *TodoList) NextId() int {
+ var found bool
+ maxID := t.MaxId()
+ for i := 1; i <= maxID; i++ {
+ found = false
+ for _, todo := range t.Data {
+ if todo.Id == i {
+ found = true
+ break
+ }
+ }
+ if !found {
+ return i
+ }
+ }
+ return maxID + 1
}
func (t *TodoList) FindById(id int) *Todo {
@@ -96,3 +114,15 @@ func (t *TodoList) FindById(id int) *Todo {
}
return nil
}
+
+func (t *TodoList) GarbageCollect() {
+ var toDelete []*Todo
+ for _, todo := range t.Data {
+ if todo.Archived {
+ toDelete = append(toDelete, todo)
+ }
+ }
+ for _, todo := range toDelete {
+ t.Delete(todo.Id)
+ }
+}
diff --git a/todolist/todo_list_test.go b/todolist/todo_list_test.go
index 8a40b14..eb0bc84 100644
--- a/todolist/todo_list_test.go
+++ b/todolist/todo_list_test.go
@@ -8,8 +8,42 @@ import (
func TestNextId(t *testing.T) {
assert := assert.New(t)
+ todo := &Todo{Subject: "testing", Completed: false, Archived: false}
list := &TodoList{}
assert.Equal(1, list.NextId())
+ list.Add(todo)
+ assert.Equal(2, list.NextId())
+}
+
+func TestNextIdWhenTodoDeleted(t *testing.T) {
+ assert := assert.New(t)
+ todo := &Todo{Subject: "testing", Completed: false, Archived: false}
+ todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false}
+ todo3 := &Todo{Subject: "testing3", Completed: false, Archived: false}
+ list := &TodoList{}
+
+ list.Add(todo)
+ list.Add(todo2)
+ list.Add(todo3)
+
+ list.Delete(2)
+ assert.Equal(2, list.NextId())
+ list.Add(todo2)
+ assert.Equal(4, list.NextId())
+ list.Delete(1)
+ assert.Equal(1, list.NextId())
+}
+
+func TestMaxId(t *testing.T) {
+ assert := assert.New(t)
+ todo := &Todo{Subject: "testing", Completed: false, Archived: false}
+ todo2 := &Todo{Subject: "testing 2", Completed: false, Archived: false}
+ list := &TodoList{}
+ assert.Equal(0, list.MaxId())
+ list.Add(todo)
+ assert.Equal(1, list.MaxId())
+ list.Add(todo2)
+ assert.Equal(2, list.MaxId())
}
func TestIndexOf(t *testing.T) {
@@ -77,3 +111,20 @@ func TestUncomplete(t *testing.T) {
list.Uncomplete(2)
assert.Equal(false, list.FindById(2).Completed)
}
+
+func TestGarbageCollect(t *testing.T) {
+ assert := assert.New(t)
+ list := &TodoList{}
+ todo := &Todo{Subject: "testing", Completed: false, Archived: true}
+ todo2 := &Todo{Subject: "testing2", Completed: false, Archived: false}
+ todo3 := &Todo{Subject: "testing3", Completed: false, Archived: true}
+ list.Add(todo)
+ list.Add(todo2)
+ list.Add(todo3)
+
+ list.GarbageCollect()
+
+ assert.Equal(len(list.Data), 1)
+ assert.Equal(1, list.NextId())
+ assert.Equal(2, list.MaxId())
+}