diff options
Diffstat (limited to 'db.go')
-rw-r--r-- | db.go | 42 |
1 files changed, 30 insertions, 12 deletions
@@ -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 } |