From 6cdd02af0684500b5376d4d4a08feb796fdaf889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Fri, 24 May 2024 23:33:49 +0200 Subject: Add date stuff --- db.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'db.go') diff --git a/db.go b/db.go index a1b0c33..45296d6 100644 --- a/db.go +++ b/db.go @@ -8,7 +8,7 @@ import ( type Database interface { GetTracking() (*Entry, error) GetEntries() ([]*Entry, error) - QueryEntry(id *int) ([]*Entry, error) + QueryEntry(id *int, date *Date) ([]*Entry, error) StartNewEntry(now time.Time, task *Entry) (*Entry, error) StopEntry(now time.Time) error SaveEntry(task *Entry) (*Entry, error) @@ -49,12 +49,16 @@ func (db *InMemDb) GetEntries() ([]*Entry, error) { return res, nil } -func (db *InMemDb) QueryEntry(id *int) ([]*Entry, error) { +func (db *InMemDb) QueryEntry(id *int, date *Date) ([]*Entry, error) { res := make([]*Entry, 0, 1) for _, entry := range db.entries { - if id != nil && entry.Id == *id { - res = append(res, entry) + if id != nil && entry.Id != *id { + continue + } + if date != nil && entry.Date != *date { + continue } + res = append(res, entry) } return res, nil @@ -65,6 +69,7 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { Id: len(db.entries), From: task.From, To: nil, + Date: DateFromStd(now), Tag: task.Tag, Comment: task.Comment, } @@ -77,6 +82,7 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { db.entries = append(db.entries, newTask) db.StopEntry(now) db.current = newTask + return newTask, nil } @@ -91,20 +97,21 @@ func (db *InMemDb) StopEntry(now time.Time) error { return nil } -func (db *InMemDb) SaveEntry(task *Entry) (*Entry, error) { - if task.Id < 0 || task.Id >= len(db.entries) { - copyTask := *task +func (db *InMemDb) SaveEntry(entry *Entry) (*Entry, error) { + if entry.Id < 0 || entry.Id >= len(db.entries) { + copyTask := *entry 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 + existent := db.entries[entry.Id] + existent.Comment = entry.Comment + existent.Tag = entry.Tag + existent.From = entry.From + existent.To = entry.To + existent.Date = entry.Date return existent, nil } -- cgit v1.2.3