summaryrefslogtreecommitdiff
path: root/tidsreg.go
diff options
context:
space:
mode:
Diffstat (limited to 'tidsreg.go')
-rw-r--r--tidsreg.go92
1 files changed, 85 insertions, 7 deletions
diff --git a/tidsreg.go b/tidsreg.go
index 43e4adb..e327f74 100644
--- a/tidsreg.go
+++ b/tidsreg.go
@@ -18,7 +18,46 @@ type Server struct {
srv *Service
}
-func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) {
+func parseTaskFromForm(r *http.Request) (*Task, error) {
+ err := r.ParseForm()
+ if err != nil {
+ return nil, err
+ }
+
+ var id int64
+ if r.PostFormValue("id") == "" {
+ id = -1
+ } else {
+ id, err = strconv.ParseInt(r.PostFormValue("id"), 10, 32)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ var group *string
+ {
+ groupStr := r.PostFormValue("group")
+ if groupStr != "" {
+ group = &groupStr
+ }
+ }
+ var ident *string
+ {
+ identStr := r.PostFormValue("ident")
+ if identStr != "" {
+ ident = &identStr
+ }
+ }
+
+ return &Task{
+ Id: int(id),
+ Group: group,
+ Ident: ident,
+ Text: r.PostFormValue("text"),
+ }, nil
+}
+
+func parseEntryFromForm(r *http.Request, useToday bool) (*Entry, error) {
err := r.ParseForm()
if err != nil {
return nil, err
@@ -59,7 +98,7 @@ func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) {
task := &Entry{
Id: int(id),
From: from,
- Date: date,
+ Date: date,
To: to,
Comment: r.PostFormValue("comment"),
}
@@ -67,7 +106,7 @@ func parseTaskFromForm(r *http.Request, useToday bool) (*Entry, error) {
}
func (s *Server) renderTemplate(w http.ResponseWriter, name string, page interface{}) {
- tmpl, err := template.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", "templates/parts/task.html", "templates/parts/taskRows.html")
if err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
return
@@ -127,7 +166,7 @@ func (s *Server) rootHandle(w http.ResponseWriter, r *http.Request) {
func (s *Server) postStart(w http.ResponseWriter, r *http.Request) {
now := time.Now()
- task, err := parseTaskFromForm(r, true)
+ task, err := parseEntryFromForm(r, true)
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
@@ -150,7 +189,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, true)
+ task, err := parseEntryFromForm(r, true)
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
@@ -183,7 +222,7 @@ func (s *Server) getTracking(w http.ResponseWriter, _ *http.Request) {
}
func (s *Server) putSave(w http.ResponseWriter, r *http.Request) {
- task, err := parseTaskFromForm(r, false)
+ task, err := parseEntryFromForm(r, false)
if err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
@@ -280,7 +319,44 @@ func (s *Server) getTasksList(w http.ResponseWriter, _ *http.Request) {
return
}
- s.renderTemplate(w, "tasksPage.html", page)
+ s.renderTemplate(w, "taskRows.html", page)
+}
+
+func (s *Server) postTaskSave(w http.ResponseWriter, r *http.Request) {
+ task, err := parseTaskFromForm(r)
+ if err != nil {
+ writeError(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ task, err = s.srv.Db.SaveTask(task)
+ if err != nil {
+ writeError(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ log.Printf("Hej %+v\n", task)
+
+ page, err := s.srv.GetTaskPage(nil, task)
+ if err != nil {
+ writeError(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Add("HX-Trigger", "changedTasks")
+
+ s.renderTemplate(w, "task.html", page)
+}
+
+func (s *Server) getTaskEmpty(w http.ResponseWriter, _ *http.Request) {
+ task := NewTask()
+
+ page, err := s.srv.GetTaskPage(nil, task)
+ if err != nil {
+ writeError(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ s.renderTemplate(w, "task.html", page)
}
func main() {
@@ -300,6 +376,8 @@ func main() {
r.HandleFunc("/entryRows", s.getEntries).Methods("GET")
r.HandleFunc("/edit", s.getEdit).Methods("GET")
r.HandleFunc("/task/list", s.getTasksList).Methods("GET")
+ r.HandleFunc("/task/save", s.postTaskSave).Methods("POST")
+ r.HandleFunc("/task/empty", s.getTaskEmpty).Methods("GET")
r.HandleFunc("/", s.rootHandle)
http.Handle("/", r)