diff options
Diffstat (limited to 'db.go')
-rw-r--r-- | db.go | 77 |
1 files changed, 58 insertions, 19 deletions
@@ -7,26 +7,31 @@ import ( type Database interface { GetTracking() (*Entry, error) - GetTasks() ([]*Entry, error) - QueryTask(id *int) (*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 { - tasks []*Entry + entries []*Entry current *Entry + tasks []*Task } var ( - ErrNotFound = errors.New("Not found") + ErrNotFound = errors.New("Not found") ErrNotRunning = errors.New("Not running") ) func NewInMemDb() Database { return &InMemDb { - tasks: []*Entry{}, + entries: []*Entry{}, current: nil, } } @@ -35,25 +40,29 @@ func (db *InMemDb) GetTracking() (*Entry, error) { return db.current, nil } -func (db *InMemDb) GetTasks() ([]*Entry, error) { - res := make([]*Entry, 0, len(db.tasks)) - for _, task := range db.tasks { +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) QueryTask(id *int) (*Entry, error) { - if id != nil && *id >= 0 || *id < len(db.tasks) { - return db.tasks[*id], 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 nil, ErrNotFound + + return res, nil } func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { newTask := &Entry { - Id: len(db.tasks), + Id: len(db.entries), From: task.From, To: nil, Tag: task.Tag, @@ -65,7 +74,7 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { newTask.From = &from } - db.tasks = append(db.tasks, newTask) + db.entries = append(db.entries, newTask) db.StopEntry(now) db.current = newTask return newTask, nil @@ -83,18 +92,48 @@ func (db *InMemDb) StopEntry(now time.Time) error { } func (db *InMemDb) SaveEntry(task *Entry) (*Entry, error) { - if task.Id < 0 || task.Id >= len(db.tasks) { + if task.Id < 0 || task.Id >= len(db.entries) { copyTask := *task - copyTask.Id = len(db.tasks) - db.tasks = append(db.tasks, ©Task) + copyTask.Id = len(db.entries) + db.entries = append(db.entries, ©Task) return ©Task, nil } - - existent := db.tasks[task.Id] + + 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 +} |