From 2f83424ce8a0653e6e158318df1511daecf1a42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Fri, 24 May 2024 21:30:23 +0200 Subject: Time format rework --- db.go | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'db.go') diff --git a/db.go b/db.go index ccee504..76cb4e7 100644 --- a/db.go +++ b/db.go @@ -6,17 +6,17 @@ import ( ) type Database interface { - GetTracking() (*Task, error) - GetTasks() ([]*Task, error) - QueryTask(id *int) (*Task, error) - StartNewEntry(now time.Time, task *Task) (*Task, error) + GetTracking() (*Entry, error) + GetTasks() ([]*Entry, error) + QueryTask(id *int) (*Entry, error) + StartNewEntry(now time.Time, task *Entry) (*Entry, error) StopEntry(now time.Time) error - SaveEntry(task *Task) (*Task, error) + SaveEntry(task *Entry) (*Entry, error) } type InMemDb struct { - tasks []*Task - current *Task + tasks []*Entry + current *Entry } var ( @@ -26,17 +26,17 @@ var ( func NewInMemDb() Database { return &InMemDb { - tasks: []*Task{}, + tasks: []*Entry{}, current: nil, } } -func (db *InMemDb) GetTracking() (*Task, error) { +func (db *InMemDb) GetTracking() (*Entry, error) { return db.current, nil } -func (db *InMemDb) GetTasks() ([]*Task, error) { - res := make([]*Task, 0, len(db.tasks)) +func (db *InMemDb) GetTasks() ([]*Entry, error) { + res := make([]*Entry, 0, len(db.tasks)) for _, task := range db.tasks { res = append(res, task) } @@ -44,18 +44,15 @@ func (db *InMemDb) GetTasks() ([]*Task, error) { return res, nil } -func (db *InMemDb) QueryTask(id *int) (*Task, error) { - for _, task := range db.tasks { - if id != nil && task.Id == *id { - return task, nil - } +func (db *InMemDb) QueryTask(id *int) (*Entry, error) { + if id != nil && *id >= 0 || *id < len(db.tasks) { + return db.tasks[*id], nil } - return nil, ErrNotFound } -func (db *InMemDb) StartNewEntry(now time.Time, task *Task) (*Task, error) { - newTask := &Task { +func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { + newTask := &Entry { Id: len(db.tasks), From: task.From, To: nil, @@ -64,7 +61,8 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Task) (*Task, error) { } if newTask.From == nil { - newTask.From = &now + from := TimeFromStd(now) + newTask.From = &from } db.tasks = append(db.tasks, newTask) @@ -75,7 +73,8 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Task) (*Task, error) { func (db *InMemDb) StopEntry(now time.Time) error { if db.current != nil { - db.current.To = &now + to := TimeFromStd(now) + db.current.To = &to db.current = nil } else { return ErrNotRunning @@ -83,7 +82,7 @@ func (db *InMemDb) StopEntry(now time.Time) error { return nil } -func (db *InMemDb) SaveEntry(task *Task) (*Task, error) { +func (db *InMemDb) SaveEntry(task *Entry) (*Entry, error) { if task.Id < 0 || task.Id >= len(db.tasks) { copyTask := *task copyTask.Id = len(db.tasks) -- cgit v1.2.3