diff --git a/context/context.go b/context/context.go index d428514..ba0482e 100644 --- a/context/context.go +++ b/context/context.go @@ -42,7 +42,7 @@ func (ctx *Context) WishlistDelete(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusSeeOther) } -func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request) { +func (ctx *Context) ForeignWishlistPost(w http.ResponseWriter, r *http.Request) { user := ctx.Auth.ExpectUser(r) if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -91,5 +91,5 @@ func (ctx *Context) UpdateForeignWishlist(w http.ResponseWriter, r *http.Request default: http.Error(w, "Invalid intent", http.StatusBadRequest) } - http.Redirect(w, r, "/"+userReference, http.StatusSeeOther) + http.Redirect(w, r, "/list/"+userReference, http.StatusSeeOther) } diff --git a/context/foreign_wishlist.go b/context/foreign_wishlist.go index ef8b84c..0c153e4 100644 --- a/context/foreign_wishlist.go +++ b/context/foreign_wishlist.go @@ -15,7 +15,7 @@ type ForeignWishlistProps struct { Gifts []db.Gift } -func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) { +func (ctx *Context) ForeignWishlist(w http.ResponseWriter, r *http.Request) { userReference := r.PathValue("userReference") user := ctx.Auth.ExpectUser(r) if user.Reference == userReference { diff --git a/context/home.go b/context/home.go index 4dfe814..8478f5a 100644 --- a/context/home.go +++ b/context/home.go @@ -31,3 +31,21 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) { p := HomeProps{Username: user.Name, Gifts: gifts, Todo: todo, Reference: user.Reference, HostUrl: env.HostUrl.String()} templates.Execute(w, "home.gotmpl", p) } + +func (ctx *Context) HomePost(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, "Couldn't parse form", http.StatusBadRequest) + return + } + switch r.Form.Get("intent") { + case "add_idea": + ctx.WishlistAdd(w, r) + return + case "delete_idea": + ctx.WishlistDelete(w, r) + return + default: + ctx.TodoUpdate(w, r) + return + } +} diff --git a/context/todo_update.go b/context/todo_update.go index 7cc70f0..a6abd13 100644 --- a/context/todo_update.go +++ b/context/todo_update.go @@ -11,15 +11,15 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - switch r.Form.Get("mode") { - case "unclaim": + switch r.Form.Get("intent") { + case "unclaim_todo": unclaims := r.Form["gift"] err := user.ClaimGifts([]string{}, unclaims) if err != nil { http.Error(w, "Failed to update claim...", http.StatusInternalServerError) return } - case "complete": + case "complete_todo": claims := r.Form["gift"] err := user.CompleteGifts(claims) if err != nil { @@ -28,7 +28,7 @@ func (ctx *Context) TodoUpdate(w http.ResponseWriter, r *http.Request) { return } default: - http.Error(w, "Invalid mode", http.StatusBadRequest) + http.Error(w, "Invalid intent", http.StatusBadRequest) } http.Redirect(w, r, "/", http.StatusSeeOther) } diff --git a/main.go b/main.go index cba9957..9ea69a8 100644 --- a/main.go +++ b/main.go @@ -35,12 +35,10 @@ func main() { publicMux.HandleFunc("GET /", authMiddleware.Login) publicMux.HandleFunc("POST /", authMiddleware.LoginPost) - protectedMux.HandleFunc("GET /", ctx.Home) - protectedMux.HandleFunc("GET /{userReference}", ctx.ViewForeignWishlist) - protectedMux.HandleFunc("POST /{userReference}/update", ctx.UpdateForeignWishlist) - protectedMux.HandleFunc("POST /wishlist/add", ctx.WishlistAdd) - protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete) - protectedMux.HandleFunc("POST /todo/update", ctx.TodoUpdate) + protectedMux.HandleFunc("GET /{$}", ctx.Home) + protectedMux.HandleFunc("POST /{$}", ctx.HomePost) + protectedMux.HandleFunc("GET /list/{userReference}", ctx.ForeignWishlist) + protectedMux.HandleFunc("POST /list/{userReference}", ctx.ForeignWishlistPost) protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost) http.Handle("/", authMiddleware) diff --git a/templates/foreign_wishlist.gotmpl b/templates/foreign_wishlist.gotmpl index e36f746..23d2b95 100644 --- a/templates/foreign_wishlist.gotmpl +++ b/templates/foreign_wishlist.gotmpl @@ -32,7 +32,7 @@

{{.Username}}'s list

{{with .Gifts}} -
-{{end}} \ No newline at end of file +{{end}} diff --git a/templates/home.gotmpl b/templates/home.gotmpl index 636fb84..503e4b5 100644 --- a/templates/home.gotmpl +++ b/templates/home.gotmpl @@ -2,7 +2,7 @@