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 --- tidsreg.go | 100 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 27 deletions(-) (limited to 'tidsreg.go') diff --git a/tidsreg.go b/tidsreg.go index 987a40f..43e4adb 100644 --- a/tidsreg.go +++ b/tidsreg.go @@ -1,9 +1,11 @@ package main import ( + "errors" "fmt" "log" "net/http" + "net/url" "strconv" "text/template" "time" @@ -13,15 +15,29 @@ import ( type Server struct { template *template.Template - srv *Service + srv *Service } -func parseTaskFromForm(r *http.Request) (*Entry, error) { +func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) { err := r.ParseForm() if err != nil { return nil, err } + var date Date + dateP, err := DateFromString(r.PostFormValue("date")) + if err != nil { + return nil, err + } + if dateP != nil { + date = *dateP + } else { + if useToday { + date = DateFromStd(time.Now()) + } else { + return nil, errors.New("date missing from post") + } + } from, err := TimeFromString(r.PostFormValue("from")) if err != nil { return nil, err @@ -40,10 +56,11 @@ func parseTaskFromForm(r *http.Request) (*Entry, error) { } } - task := &Entry { - Id: int(id), - From: from, - To: to, + task := &Entry{ + Id: int(id), + From: from, + Date: date, + To: to, Comment: r.PostFormValue("comment"), } return task, nil @@ -69,6 +86,22 @@ func writeError(w http.ResponseWriter, err string, code int) { return } +func parseDateFromUrl(url *url.URL, allowToday bool) (Date, error) { + date, err := DateFromString(url.Query().Get("date")) + if err != nil { + return "", nil + } + if date == nil { + if allowToday { + return DateFromStd(time.Now()), nil + } else { + return "", errors.New("date query param empty") + } + } + + return *date, nil +} + func (s *Server) rootHandle(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { w.WriteHeader(http.StatusNotFound) @@ -76,7 +109,13 @@ func (s *Server) rootHandle(w http.ResponseWriter, r *http.Request) { return } - page, err := s.srv.GetRootPage() + date, err := parseDateFromUrl(r.URL, true) + if err != nil { + writeError(w, err.Error(), http.StatusBadRequest) + return + } + + page, err := s.srv.GetRootPage(date) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -85,11 +124,10 @@ func (s *Server) rootHandle(w http.ResponseWriter, r *http.Request) { s.renderTemplate(w, "index.html", page) } - func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { now := time.Now() - task, err := parseTaskFromForm(r) + task, err := parseTaskFromForm(r, true) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -100,7 +138,7 @@ func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { writeError(w, err.Error(), http.StatusInternalServerError) return } - page, err := s.srv.GetEntryPage(nil) + page, err := s.srv.GetEntryPage(nil, NewDateInfo(now)) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -112,7 +150,7 @@ func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { func (s *Server) postStop(w http.ResponseWriter, r *http.Request) { now := time.Now() - task, err := parseTaskFromForm(r) + task, err := parseTaskFromForm(r, true) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -134,7 +172,8 @@ func (s *Server) postStop(w http.ResponseWriter, r *http.Request) { } func (s *Server) getTracking(w http.ResponseWriter, _ *http.Request) { - page, err := s.srv.GetEntryPage(nil) + nowDt := NewDateInfo(time.Now()) + page, err := s.srv.GetEntryPage(nil, nowDt) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -144,7 +183,7 @@ func (s *Server) getTracking(w http.ResponseWriter, _ *http.Request) { } func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { - task, err := parseTaskFromForm(r) + task, err := parseTaskFromForm(r, false) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) return @@ -161,7 +200,7 @@ func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { detached = task } - page, err := s.srv.GetEntryPage(detached) + page, err := s.srv.GetEntryPage(detached, NewDateInfo(task.Date.ToStd())) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -172,16 +211,16 @@ func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { s.renderTemplate(w, "entry.html", page) } -func (s *Server) getNew(w http.ResponseWriter, _ *http.Request) { - task := &Entry { - Id: -1, - From: nil, - To: nil, - Tag: nil, - Comment: "", +func (s *Server) getNew(w http.ResponseWriter, r *http.Request) { + date, err := parseDateFromUrl(r.URL, true) + if err != nil { + writeError(w, err.Error(), http.StatusBadRequest) + return } - page, err := s.srv.GetEntryPage(task) + task := NewEntry(date) + + page, err := s.srv.GetEntryPage(task, NewDateInfo(date.ToStd())) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -198,7 +237,7 @@ func (s *Server) getEdit(w http.ResponseWriter, r *http.Request) { } id := int(idLarge) - entries, err := s.srv.Db.QueryEntry(&id) + entries, err := s.srv.Db.QueryEntry(&id, nil) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -208,7 +247,8 @@ func (s *Server) getEdit(w http.ResponseWriter, r *http.Request) { return } - page, err := s.srv.GetEntryPage(entries[0]) + page, err := s.srv.GetEntryPage(entries[0], + NewDateInfo(entries[0].Date.ToStd())) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -217,8 +257,14 @@ func (s *Server) getEdit(w http.ResponseWriter, r *http.Request) { s.renderTemplate(w, "entry.html", page) } -func (s *Server) getEntries(w http.ResponseWriter, _ *http.Request) { - page, err := s.srv.GetRootPage() +func (s *Server) getEntries(w http.ResponseWriter, r *http.Request) { + date, err := parseDateFromUrl(r.URL, true) + if err != nil { + writeError(w, err.Error(), http.StatusBadRequest) + return + } + + page, err := s.srv.GetRootPage(date) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -242,7 +288,7 @@ func main() { s := Server{ template: nil, - srv: NewService(NewInMemDb()), + srv: NewService(NewInMemDb()), } r := mux.NewRouter() -- cgit v1.2.3