From 19f8454a680c5231df68fee36ed9758587df316c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Thu, 23 May 2024 21:34:28 +0200 Subject: Rudimentary task editing --- db.go | 101 +++++++++++++++++++ go.mod | 2 + go.sum | 2 + model.go | 11 ++ page.go | 59 +++++++++++ templates/index.html | 75 ++++---------- templates/parts/entry.html | 81 +++++++++++++++ templates/parts/entryRows.html | 12 +++ tidsreg.go | 223 +++++++++++++++++++++++++++++++++++++++-- 9 files changed, 503 insertions(+), 63 deletions(-) create mode 100644 db.go create mode 100644 go.sum create mode 100644 model.go create mode 100644 page.go create mode 100644 templates/parts/entry.html create mode 100644 templates/parts/entryRows.html diff --git a/db.go b/db.go new file mode 100644 index 0000000..ccee504 --- /dev/null +++ b/db.go @@ -0,0 +1,101 @@ +package main + +import ( + "errors" + "time" +) + +type Database interface { + GetTracking() (*Task, error) + GetTasks() ([]*Task, error) + QueryTask(id *int) (*Task, error) + StartNewEntry(now time.Time, task *Task) (*Task, error) + StopEntry(now time.Time) error + SaveEntry(task *Task) (*Task, error) +} + +type InMemDb struct { + tasks []*Task + current *Task +} + +var ( + ErrNotFound = errors.New("Not found") + ErrNotRunning = errors.New("Not running") +) + +func NewInMemDb() Database { + return &InMemDb { + tasks: []*Task{}, + current: nil, + } +} + +func (db *InMemDb) GetTracking() (*Task, error) { + return db.current, 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) (*Task, error) { + for _, task := range db.tasks { + if id != nil && task.Id == *id { + return task, nil + } + } + + return nil, ErrNotFound +} + +func (db *InMemDb) StartNewEntry(now time.Time, task *Task) (*Task, error) { + newTask := &Task { + Id: len(db.tasks), + From: task.From, + To: nil, + Tag: task.Tag, + Comment: task.Comment, + } + + if newTask.From == nil { + newTask.From = &now + } + + db.tasks = append(db.tasks, newTask) + db.StopEntry(now) + db.current = newTask + return newTask, nil +} + +func (db *InMemDb) StopEntry(now time.Time) error { + if db.current != nil { + db.current.To = &now + db.current = nil + } else { + return ErrNotRunning + } + return nil +} + +func (db *InMemDb) SaveEntry(task *Task) (*Task, error) { + if task.Id < 0 || task.Id >= len(db.tasks) { + copyTask := *task + copyTask.Id = len(db.tasks) + db.tasks = append(db.tasks, ©Task) + + return ©Task, nil + } + + existent := db.tasks[task.Id] + existent.Comment = task.Comment + existent.Tag = task.Tag + existent.From = task.From + existent.To = task.To + return existent, nil +} diff --git a/go.mod b/go.mod index 421fa47..ab6e58d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module tidsreg go 1.22.2 + +require github.com/gorilla/mux v1.8.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/model.go b/model.go new file mode 100644 index 0000000..0f52ef9 --- /dev/null +++ b/model.go @@ -0,0 +1,11 @@ +package main + +import "time" + +type Task struct { + Id int + From *time.Time + To *time.Time + Tag *string + Comment string +} diff --git a/page.go b/page.go new file mode 100644 index 0000000..384ca57 --- /dev/null +++ b/page.go @@ -0,0 +1,59 @@ +package main + +type Service struct { + Db Database +} + +type EntryPage struct { + Task *Task + Detached bool + Tracking *Task +} + +type RootPage struct { + Entry *EntryPage + Tasks []*Task +} + +func NewService(db Database) *Service { + return &Service { + Db: db, + } +} + +func (srv *Service) GetEntryPage(detached *Task) (*EntryPage, error) { + tracking, err := srv.Db.GetTracking() + if err != nil { + return nil, err + } + + page := &EntryPage { + Task: tracking, + Detached: false, + Tracking: tracking, + } + + if detached != nil { + page.Task = detached + page.Detached = true + } + + + return page, nil +} + +func (srv *Service) GetRootPage() (*RootPage, error) { + entry, err := srv.GetEntryPage(nil) + if err != nil { + return nil, err + } + tasks, err := srv.Db.GetTasks() + if err != nil { + return nil, err + } + + return &RootPage { + Entry: entry, + Tasks: tasks, + }, nil +} diff --git a/templates/index.html b/templates/index.html index 3654855..c408fb0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,6 +6,9 @@ Tidsreg + + +