From c06cf9bdac4364c8df9b739cf5131227a29300ab Mon Sep 17 00:00:00 2001 From: Mirco Zeiss Date: Sun, 24 Jan 2016 06:53:24 +0100 Subject: [PATCH] add undone handler --- index.html | 23 ++++++++++++++++------- main.go | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index a4012d8..c84aa47 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@ {{ range $index, $item := .Data.Todos }} - + {{ add $index 1 }} @@ -41,12 +41,21 @@ -
- -
+ {{ if $item.Done }} +
+ +
+ {{ else }} +
+ +
+ {{ end }}
diff --git a/main.go b/main.go index e455d28..29613ef 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func main() { r.Methods("GET").Path("/logout").Handler(appHandler(LogoutHandler)) r.Methods("POST").Path("/{id}/delete").Handler(appHandler(DeleteHandler)) r.Methods("POST").Path("/{id}/done").Handler(appHandler(DoneHandler)) + r.Methods("POST").Path("/{id}/undone").Handler(appHandler(UndoneHandler)) // add logging rWithLogging := handlers.LoggingHandler(os.Stdout, r) log.Fatal(http.ListenAndServe(":3000", rWithLogging)) @@ -188,3 +189,24 @@ func DoneHandler(w http.ResponseWriter, r *http.Request) *appError { http.Redirect(w, r, "/", http.StatusSeeOther) return nil } + +func UndoneHandler(w http.ResponseWriter, r *http.Request) *appError { + vars := mux.Vars(r) + id := vars["id"] + // get username from session + session, err := RediStore.Get(r, "session") + if err != nil { + return InternalServerError(fmt.Errorf("get session from redistore: %v", err)) + } + username := session.Values["username"].(string) + t, err := db.GetTodoByID(username, id) + if err != nil { + return InternalServerError(fmt.Errorf("get todo by id: %v", err)) + } + t.Done = false + if err := db.UpdateTodo(username, t); err != nil { + return InternalServerError(fmt.Errorf("update todo: %v", err)) + } + http.Redirect(w, r, "/", http.StatusSeeOther) + return nil +}