From 6cdd02af0684500b5376d4d4a08feb796fdaf889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20J=C3=B8rgensen?= Date: Fri, 24 May 2024 23:33:49 +0200 Subject: Add date stuff --- page.go | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'page.go') diff --git a/page.go b/page.go index bae3e7c..b58436d 100644 --- a/page.go +++ b/page.go @@ -1,24 +1,29 @@ package main +import "time" + type Service struct { Db Database } type DateInfo struct { - Date string - Tomorrow string - Yesterday string + Date Date + Tomorrow Date + Yesterday Date + IsToday bool } type EntryPage struct { Entry *Entry Detached bool Tracking *Entry + DateInfo DateInfo } type RootPage struct { - Entry *EntryPage - Entries []*Entry + DateInfo DateInfo + Entry *EntryPage + Entries []*Entry } type TasksPage struct { @@ -31,6 +36,15 @@ func NewService(db Database) *Service { } } +func NewDateInfo(t time.Time) DateInfo { + return DateInfo{ + Date: DateFromStd(t), + Tomorrow: DateFromStd(t.Add(24 * time.Hour)), + Yesterday: DateFromStd(t.Add(-24 * time.Hour)), + IsToday: DateFromStd(time.Now()) == DateFromStd(t), + } +} + func (srv *Service) GetTasksPage() (*TasksPage, error) { tasks, err := srv.Db.GetTasks() if err != nil { @@ -40,16 +54,16 @@ func (srv *Service) GetTasksPage() (*TasksPage, error) { Tasks: tasks, }, nil } -func (srv *Service) GetEntryPage(detached *Entry) (*EntryPage, error) { +func (srv *Service) GetEntryPage(detached *Entry, dt DateInfo) (*EntryPage, error) { tracking, err := srv.Db.GetTracking() if err != nil { return nil, err } - page := &EntryPage{ Entry: tracking, Detached: false, Tracking: tracking, + DateInfo: dt, } if detached != nil { @@ -60,18 +74,26 @@ func (srv *Service) GetEntryPage(detached *Entry) (*EntryPage, error) { return page, nil } -func (srv *Service) GetRootPage() (*RootPage, error) { - entry, err := srv.GetEntryPage(nil) +func (srv *Service) GetRootPage(date Date) (*RootPage, error) { + var entry *Entry = nil + + if string(date) != time.Now().Format("2006-01-02") { + // If not today detach + entry = NewEntry(date) + } + + entryPage, err := srv.GetEntryPage(entry, NewDateInfo(date.ToStd())) if err != nil { return nil, err } - entries, err := srv.Db.GetEntries() + entries, err := srv.Db.QueryEntry(nil, &date) if err != nil { return nil, err } return &RootPage{ - Entry: entry, - Entries: entries, + DateInfo: entryPage.DateInfo, + Entry: entryPage, + Entries: entries, }, nil } -- cgit v1.2.3