diff options
author | Julian Jørgensen <julian@jtle.dk> | 2024-05-24 21:30:23 +0200 |
---|---|---|
committer | Julian Jørgensen <julian@jtle.dk> | 2024-05-24 21:30:23 +0200 |
commit | 2f83424ce8a0653e6e158318df1511daecf1a42d (patch) | |
tree | b29c1b5c8df940873521d013697209195956151e /tidsreg.go | |
parent | 19f8454a680c5231df68fee36ed9758587df316c (diff) |
Time format rework
Diffstat (limited to 'tidsreg.go')
-rw-r--r-- | tidsreg.go | 62 |
1 files changed, 34 insertions, 28 deletions
@@ -16,32 +16,17 @@ type Server struct { srv *Service } -func funcMapFormatTime(t *time.Time) string { - if t == nil { - return "" - } - return t.Format("15:04") -} - -func parseStringTime(str string) (*time.Time, error) { - if str == "" { - return nil, nil - } - t, err := time.Parse("15:04", str) - return &t, err -} - -func parseTaskFromForm(r *http.Request) (*Task, error) { +func parseTaskFromForm(r *http.Request) (*Entry, error) { err := r.ParseForm() if err != nil { return nil, err } - from, err := parseStringTime(r.PostFormValue("from")) + from, err := TimeFromString(r.PostFormValue("from")) if err != nil { return nil, err } - to, err := parseStringTime(r.PostFormValue("to")) + to, err := TimeFromString(r.PostFormValue("to")) if err != nil { return nil, err } @@ -55,7 +40,7 @@ func parseTaskFromForm(r *http.Request) (*Task, error) { } } - task := &Task { + task := &Entry { Id: int(id), From: from, To: to, @@ -65,10 +50,7 @@ func parseTaskFromForm(r *http.Request) (*Task, error) { } func (s *Server) renderTemplate(w http.ResponseWriter, name string, page interface{}) { - tmpl := template.New("").Funcs(template.FuncMap { - "formatTime": funcMapFormatTime, - }) - tmpl, err := tmpl.ParseFiles("templates/index.html", "templates/parts/entry.html", "templates/parts/entryRows.html") + tmpl, err := template.ParseFiles("templates/index.html", "templates/parts/entry.html", "templates/parts/entryRows.html") if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return @@ -123,7 +105,7 @@ func (s *Server) postStart(w http.ResponseWriter, r *http.Request) { writeError(w, err.Error(), http.StatusInternalServerError) return } - w.Header().Add("HX-Trigger", "changedTasks") + w.Header().Add("HX-Trigger", "changedEntries") s.renderTemplate(w, "entry.html", page) } @@ -146,7 +128,7 @@ func (s *Server) postStop(w http.ResponseWriter, r *http.Request) { return } - w.Header().Add("HX-Trigger", "changedTasks") + w.Header().Add("HX-Trigger", "changedEntries") s.getTracking(w, r) } @@ -174,7 +156,7 @@ func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { return } - var detached *Task = nil + var detached *Entry = nil if r.URL.Query().Get("detached") == "true" { detached = task } @@ -185,13 +167,13 @@ func (s *Server) putSave(w http.ResponseWriter, r *http.Request) { return } - w.Header().Add("HX-Trigger", "changedTasks") + w.Header().Add("HX-Trigger", "changedEntries") s.renderTemplate(w, "entry.html", page) } func (s *Server) getNew(w http.ResponseWriter, _ *http.Request) { - task := &Task { + task := &Entry { Id: -1, From: nil, To: nil, @@ -208,6 +190,29 @@ func (s *Server) getNew(w http.ResponseWriter, _ *http.Request) { s.renderTemplate(w, "entry.html", page) } +func (s *Server) getEdit(w http.ResponseWriter, r *http.Request) { + idLarge, err := strconv.ParseInt(r.URL.Query().Get("id"), 10, 32) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + id := int(idLarge) + + task, err := s.srv.Db.QueryTask(&id) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + page, err := s.srv.GetEntryPage(task) + if err != nil { + writeError(w, err.Error(), http.StatusInternalServerError) + return + } + + s.renderTemplate(w, "entry.html", page) +} + func (s *Server) getEntries(w http.ResponseWriter, _ *http.Request) { page, err := s.srv.GetRootPage() if err != nil { @@ -233,6 +238,7 @@ func main() { r.HandleFunc("/stop", s.postStop).Methods("POST") r.HandleFunc("/save", s.putSave).Methods("PUT") r.HandleFunc("/entryRows", s.getEntries).Methods("GET") + r.HandleFunc("/edit", s.getEdit).Methods("GET") r.HandleFunc("/", s.rootHandle) http.Handle("/", r) |