aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/todo_list.go
diff options
context:
space:
mode:
Diffstat (limited to 'todolist/todo_list.go')
-rw-r--r--todolist/todo_list.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/todolist/todo_list.go b/todolist/todo_list.go
index 71484c6..19fe0b7 100644
--- a/todolist/todo_list.go
+++ b/todolist/todo_list.go
@@ -92,14 +92,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 {
@@ -110,3 +128,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)
+ }
+}