Skip to content

Commit

Permalink
add undone handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zemirco committed Jan 24, 2016
1 parent e1c9c51 commit c06cf9b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
23 changes: 16 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</thead>
<tbody>
{{ range $index, $item := .Data.Todos }}
<tr {{ if $item.Done }}class="success"{{ end }}>
<tr {{ if $item.Done }}style="text-decoration: line-through;"{{ end }}>
<td>
{{ add $index 1 }}
</td>
Expand All @@ -41,12 +41,21 @@
</span>
</td>
<td>
<form action="/{{ $item.ID }}/done" method="post">
<button type="submit" class="btn btn-info btn-xs">
<span class="glyphicon glyphicon-ok"></span>
Done
</button>
</form>
{{ if $item.Done }}
<form action="/{{ $item.ID }}/undone" method="post">
<button type="submit" class="btn btn-info btn-xs">
<span class="glyphicon glyphicon-eye-open"></span>
Open
</button>
</form>
{{ else }}
<form action="/{{ $item.ID }}/done" method="post">
<button type="submit" class="btn btn-info btn-xs">
<span class="glyphicon glyphicon-eye-close"></span>
Close
</button>
</form>
{{ end }}
</td>
<td>
<form action="/{{ $item.ID }}/delete" method="post">
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

0 comments on commit c06cf9b

Please sign in to comment.