From 2610f9b67e4a915859c029da9b17e9d262b6564e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Sat, 25 May 2024 11:53:43 +0200 Subject: Add task ui and editing --- db.go | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'db.go') diff --git a/db.go b/db.go index 45296d6..363bc93 100644 --- a/db.go +++ b/db.go @@ -19,9 +19,9 @@ type Database interface { } type InMemDb struct { - entries []*Entry + entries []*Entry current *Entry - tasks []*Task + tasks []*Task } var ( @@ -30,7 +30,7 @@ var ( ) func NewInMemDb() Database { - return &InMemDb { + return &InMemDb{ entries: []*Entry{}, current: nil, } @@ -65,12 +65,12 @@ func (db *InMemDb) QueryEntry(id *int, date *Date) ([]*Entry, error) { } func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { - newTask := &Entry { - Id: len(db.entries), - From: task.From, - To: nil, - Date: DateFromStd(now), - Tag: task.Tag, + newTask := &Entry{ + Id: len(db.entries), + From: task.From, + To: nil, + Date: DateFromStd(now), + Tag: task.Tag, Comment: task.Comment, } @@ -139,8 +139,26 @@ func (db *InMemDb) QueryTask(id *int, group *string, ident *string) ([]*Task, er res = append(res, task) } - return res, ErrNotFound + return res, nil } -func (db *InMemDb) SaveTask(task *Task) (*Task, error) { - return nil, nil +func (db *InMemDb) SaveTask(task *Task) (newTask *Task, err error) { + var tasks []*Task + + tasks, err = db.QueryTask(&task.Id, nil, nil) + if err != nil { + return + } + if len(tasks) > 0 { + newTask = tasks[0] + newTask.Text = task.Text + newTask.Group = task.Group + newTask.Ident = task.Ident + } else { + taskCopy := *task + taskCopy.Id = len(db.tasks) + db.tasks = append(db.tasks, &taskCopy) + newTask = &taskCopy + } + + return } -- cgit v1.2.3