aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/app.go
blob: 74fb8dc04f393b764bec3409d8ca64ff68d7bc3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package todolist

import (
	"fmt"
	"regexp"
	"strconv"
)

type App struct {
	TodoStore Store
}

func NewApp() *App {
	app := &App{TodoStore: NewFileStore()}
	app.TodoStore.Load()
	return app
}

func (a *App) AddTodo(input string) {
	parser := &Parser{}
	todo := parser.ParseNewTodo(input)

	a.TodoStore.Add(todo)
	a.TodoStore.Save()
	fmt.Println("Todo added.")
}

func (a *App) DeleteTodo(input string) {
	id := a.getId(input)
	if id != -1 {
		a.TodoStore.Delete(id)
		a.TodoStore.Save()
		fmt.Println("Todo deleted.")
	} else {
		fmt.Println("Could not find id.")
	}
}

func (a *App) getId(input string) int {

	re, _ := regexp.Compile("\\d+")
	if re.MatchString(input) {
		id, _ := strconv.Atoi(re.FindString(input))
		return id
	} else {
		return -1
	}
}

func (a *App) ListTodos(input string) {
	//filtered := NewFilter(a.TodoStore.Todos()).filter()
	grouped := a.getGroups(input)

	formatter := NewFormatter(grouped)
	formatter.Print()
}

func (a *App) getGroups(input string) *GroupedTodos {
	grouper := &Grouper{}
	contextRegex, _ := regexp.Compile(`by c.*$`)
	projectRegex, _ := regexp.Compile(`by p.*$`)

	var grouped *GroupedTodos

	if contextRegex.MatchString(input) {
		grouped = grouper.GroupByContext(a.TodoStore.Todos())
	} else if projectRegex.MatchString(input) {
		grouped = grouper.GroupByContext(a.TodoStore.Todos())
	} else {
		grouped = grouper.GroupByNothing(a.TodoStore.Todos())
	}
	return grouped
}