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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package main
import (
"errors"
"time"
)
type Database interface {
GetTracking() (*Entry, error)
GetEntries() ([]*Entry, error)
QueryEntry(id *int) ([]*Entry, error)
StartNewEntry(now time.Time, task *Entry) (*Entry, error)
StopEntry(now time.Time) error
SaveEntry(task *Entry) (*Entry, error)
GetTasks() ([]*Task, error)
QueryTask(id *int, group *string, ident *string) ([]*Task, error)
SaveTask(task *Task) (*Task, error)
}
type InMemDb struct {
entries []*Entry
current *Entry
tasks []*Task
}
var (
ErrNotFound = errors.New("Not found")
ErrNotRunning = errors.New("Not running")
)
func NewInMemDb() Database {
return &InMemDb {
entries: []*Entry{},
current: nil,
}
}
func (db *InMemDb) GetTracking() (*Entry, error) {
return db.current, nil
}
func (db *InMemDb) GetEntries() ([]*Entry, error) {
res := make([]*Entry, 0, len(db.entries))
for _, task := range db.entries {
res = append(res, task)
}
return res, nil
}
func (db *InMemDb) QueryEntry(id *int) ([]*Entry, error) {
res := make([]*Entry, 0, 1)
for _, entry := range db.entries {
if id != nil && entry.Id == *id {
res = append(res, entry)
}
}
return res, nil
}
func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) {
newTask := &Entry {
Id: len(db.entries),
From: task.From,
To: nil,
Tag: task.Tag,
Comment: task.Comment,
}
if newTask.From == nil {
from := TimeFromStd(now)
newTask.From = &from
}
db.entries = append(db.entries, newTask)
db.StopEntry(now)
db.current = newTask
return newTask, nil
}
func (db *InMemDb) StopEntry(now time.Time) error {
if db.current != nil {
to := TimeFromStd(now)
db.current.To = &to
db.current = nil
} else {
return ErrNotRunning
}
return nil
}
func (db *InMemDb) SaveEntry(task *Entry) (*Entry, error) {
if task.Id < 0 || task.Id >= len(db.entries) {
copyTask := *task
copyTask.Id = len(db.entries)
db.entries = append(db.entries, ©Task)
return ©Task, nil
}
existent := db.entries[task.Id]
existent.Comment = task.Comment
existent.Tag = task.Tag
existent.From = task.From
existent.To = task.To
return existent, nil
}
func (db *InMemDb) GetTasks() ([]*Task, error) {
res := make([]*Task, 0, len(db.tasks))
for _, task := range db.tasks {
res = append(res, task)
}
return res, nil
}
func (db *InMemDb) QueryTask(id *int, group *string, ident *string) ([]*Task, error) {
res := make([]*Task, 0, 1)
for _, task := range db.tasks {
if id != nil && task.Id != *id {
continue
}
if group != nil && task.Group != group {
continue
}
if ident != nil && task.Ident != ident {
continue
}
res = append(res, task)
}
return res, ErrNotFound
}
func (db *InMemDb) SaveTask(task *Task) (*Task, error) {
return nil, nil
}
|