From eabd0e769c2bfef7a7aa748d794675d3ff7a920b Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 24 May 2024 21:44:05 +0200 Subject: Add tasks --- db.go | 77 +++++++++++++++++++++++++++++++++++++++------------- model.go | 7 +++++ page.go | 40 +++++++++++++++++++-------- templates/index.html | 65 ++++++++++++++++++++++++++++++++++---------- tidsreg.go | 19 +++++++++++-- 5 files changed, 161 insertions(+), 47 deletions(-) diff --git a/db.go b/db.go index 76cb4e7..a1b0c33 100644 --- a/db.go +++ b/db.go @@ -7,26 +7,31 @@ import ( type Database interface { GetTracking() (*Entry, error) - GetTasks() ([]*Entry, error) - QueryTask(id *int) (*Entry, error) + GetEntries() ([]*Entry, error) + QueryEntry(id *int) ([]*Entry, error) StartNewEntry(now time.Time, task *Entry) (*Entry, error) StopEntry(now time.Time) error SaveEntry(task *Entry) (*Entry, error) + + GetTasks() ([]*Task, error) + QueryTask(id *int, group *string, ident *string) ([]*Task, error) + SaveTask(task *Task) (*Task, error) } type InMemDb struct { - tasks []*Entry + entries []*Entry current *Entry + tasks []*Task } var ( - ErrNotFound = errors.New("Not found") + ErrNotFound = errors.New("Not found") ErrNotRunning = errors.New("Not running") ) func NewInMemDb() Database { return &InMemDb { - tasks: []*Entry{}, + entries: []*Entry{}, current: nil, } } @@ -35,25 +40,29 @@ func (db *InMemDb) GetTracking() (*Entry, error) { return db.current, nil } -func (db *InMemDb) GetTasks() ([]*Entry, error) { - res := make([]*Entry, 0, len(db.tasks)) - for _, task := range db.tasks { +func (db *InMemDb) GetEntries() ([]*Entry, error) { + res := make([]*Entry, 0, len(db.entries)) + for _, task := range db.entries { res = append(res, task) } return res, nil } -func (db *InMemDb) QueryTask(id *int) (*Entry, error) { - if id != nil && *id >= 0 || *id < len(db.tasks) { - return db.tasks[*id], nil +func (db *InMemDb) QueryEntry(id *int) ([]*Entry, error) { + res := make([]*Entry, 0, 1) + for _, entry := range db.entries { + if id != nil && entry.Id == *id { + res = append(res, entry) + } } - return nil, ErrNotFound + + return res, nil } func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { newTask := &Entry { - Id: len(db.tasks), + Id: len(db.entries), From: task.From, To: nil, Tag: task.Tag, @@ -65,7 +74,7 @@ func (db *InMemDb) StartNewEntry(now time.Time, task *Entry) (*Entry, error) { newTask.From = &from } - db.tasks = append(db.tasks, newTask) + db.entries = append(db.entries, newTask) db.StopEntry(now) db.current = newTask return newTask, nil @@ -83,18 +92,48 @@ func (db *InMemDb) StopEntry(now time.Time) error { } func (db *InMemDb) SaveEntry(task *Entry) (*Entry, error) { - if task.Id < 0 || task.Id >= len(db.tasks) { + if task.Id < 0 || task.Id >= len(db.entries) { copyTask := *task - copyTask.Id = len(db.tasks) - db.tasks = append(db.tasks, ©Task) + copyTask.Id = len(db.entries) + db.entries = append(db.entries, ©Task) return ©Task, nil } - - existent := db.tasks[task.Id] + + existent := db.entries[task.Id] existent.Comment = task.Comment existent.Tag = task.Tag existent.From = task.From existent.To = task.To return existent, 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, group *string, ident *string) ([]*Task, error) { + res := make([]*Task, 0, 1) + for _, task := range db.tasks { + if id != nil && task.Id != *id { + continue + } + if group != nil && task.Group != group { + continue + } + if ident != nil && task.Ident != ident { + continue + } + res = append(res, task) + } + + return res, ErrNotFound +} +func (db *InMemDb) SaveTask(task *Task) (*Task, error) { + return nil, nil +} diff --git a/model.go b/model.go index b6d5c79..96be9e4 100644 --- a/model.go +++ b/model.go @@ -15,6 +15,13 @@ type Entry struct { Comment string } +type Task struct { + Id int + Group *string + Ident *string + Text string +} + type Date string type Time string diff --git a/page.go b/page.go index c3ab160..bae3e7c 100644 --- a/page.go +++ b/page.go @@ -4,31 +4,50 @@ type Service struct { Db Database } +type DateInfo struct { + Date string + Tomorrow string + Yesterday string +} + type EntryPage struct { - Entry *Entry + Entry *Entry Detached bool Tracking *Entry } type RootPage struct { - Entry *EntryPage + Entry *EntryPage Entries []*Entry } +type TasksPage struct { + Tasks []*Task +} + func NewService(db Database) *Service { - return &Service { + return &Service{ Db: db, } } +func (srv *Service) GetTasksPage() (*TasksPage, error) { + tasks, err := srv.Db.GetTasks() + if err != nil { + return nil, err + } + return &TasksPage{ + Tasks: tasks, + }, nil +} func (srv *Service) GetEntryPage(detached *Entry) (*EntryPage, error) { tracking, err := srv.Db.GetTracking() if err != nil { return nil, err } - - page := &EntryPage { - Entry: tracking, + + page := &EntryPage{ + Entry: tracking, Detached: false, Tracking: tracking, } @@ -38,7 +57,6 @@ func (srv *Service) GetEntryPage(detached *Entry) (*EntryPage, error) { page.Detached = true } - return page, nil } @@ -47,13 +65,13 @@ func (srv *Service) GetRootPage() (*RootPage, error) { if err != nil { return nil, err } - tasks, err := srv.Db.GetTasks() + entries, err := srv.Db.GetEntries() if err != nil { return nil, err } - return &RootPage { - Entry: entry, - Entries: tasks, + return &RootPage{ + Entry: entry, + Entries: entries, }, nil } diff --git a/templates/index.html b/templates/index.html index c408fb0..eecf879 100644 --- a/templates/index.html +++ b/templates/index.html @@ -60,25 +60,60 @@
- {{template "entry.html" .Entry}} -
- Uge 42 +
+ {{template "entry.html" .Entry}} +
+
+ + + + + + + + + + + + {{ template "entryRows.html" . }} +
IdFraTilMærkerBeskrivelseHandlinger
+
+
-
-
-
+
+
+ + + + +
- + + + + + + + - - - - - - + + - - {{ template "entryRows.html" . }} +
Trifork + +
+ SVT-112: fdsa Dsa ds wqw fdsf fds +
IdFraTilMærkerBeskrivelseHandlinger + Helligdag
diff --git a/tidsreg.go b/tidsreg.go index 28dea95..987a40f 100644 --- a/tidsreg.go +++ b/tidsreg.go @@ -198,13 +198,17 @@ func (s *Server) getEdit(w http.ResponseWriter, r *http.Request) { } id := int(idLarge) - task, err := s.srv.Db.QueryTask(&id) + entries, err := s.srv.Db.QueryEntry(&id) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return } + if len(entries) < 1 { + writeError(w, "could not find entry by id", http.StatusNotFound) + return + } - page, err := s.srv.GetEntryPage(task) + page, err := s.srv.GetEntryPage(entries[0]) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -223,6 +227,16 @@ func (s *Server) getEntries(w http.ResponseWriter, _ *http.Request) { s.renderTemplate(w, "entryRows.html", page) } +func (s *Server) getTasksList(w http.ResponseWriter, _ *http.Request) { + page, err := s.srv.GetTasksPage() + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + s.renderTemplate(w, "tasksPage.html", page) +} + func main() { fmt.Println("Hello world!") @@ -239,6 +253,7 @@ func main() { r.HandleFunc("/save", s.putSave).Methods("PUT") r.HandleFunc("/entryRows", s.getEntries).Methods("GET") r.HandleFunc("/edit", s.getEdit).Methods("GET") + r.HandleFunc("/task/list", s.getTasksList).Methods("GET") r.HandleFunc("/", s.rootHandle) http.Handle("/", r) -- cgit v1.2.3