From 3ad57c754f94ff07240bdce87545dfe0accae2d8 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Mon, 1 Jul 2024 14:19:38 +0200 Subject: [PATCH] Displaying a response to user after posting form data --- cmd/web/main.go | 5 ++++ cmd/web/routes.go | 1 + internal/handlers/handlers.go | 34 ++++++++++++++++----- templates/reservation-summary.page.tmpl | 39 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 templates/reservation-summary.page.tmpl diff --git a/cmd/web/main.go b/cmd/web/main.go index ee18d48..c6972a1 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -1,9 +1,11 @@ package main import ( + "encoding/gob" "fmt" "go-udemy-web-1/internal/config" "go-udemy-web-1/internal/handlers" + "go-udemy-web-1/internal/models" "go-udemy-web-1/internal/render" "log" "net/http" @@ -21,6 +23,9 @@ var ( // main is the main application function func main() { + // what am I going to put in the session + gob.Register(models.Reservation{}) + // change this to true when in production app.InProduction = false diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 5f8d939..f34ea36 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -27,6 +27,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Post("/availability-json", handlers.Repo.AvailabilityJSON) mux.Get("/make-reservation", handlers.Repo.MakeReservation) mux.Post("/make-reservation", handlers.Repo.PostMakeReservation) + mux.Get("/reservation-summary", handlers.Repo.ReservationSummary) fileServer := http.FileServer(http.Dir("./static/")) mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 5f7d15c..a310008 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -69,16 +69,16 @@ func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) { // MakeReservation is the make reservation page handler func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) { - // For the first time render emptyReservation so that this object is - // filled with the info when sent back. - var emptyReservation models.Reservation - data := make(map[string]interface{}) + // For the first time render emptyReservation so that this object is + // filled with the info when sent back. + var emptyReservation models.Reservation + data := make(map[string]interface{}) - data["reservation"] = emptyReservation + data["reservation"] = emptyReservation render.RenderTemplate(w, r, "make-reservation.page.tmpl", &models.TemplateData{ Form: forms.New(nil), - Data: data, + Data: data, }) } @@ -100,8 +100,8 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) form := forms.New(r.PostForm) form.Required("first_name", "last_name", "email") - form.MinLength("first_name", 2, r) - form.IsEmail("email", r) + form.MinLength("first_name", 2, r) + form.IsEmail("email", r) if !form.Valid() { data := make(map[string]interface{}) @@ -113,6 +113,24 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) }) return } + + m.App.Session.Put(r.Context(), "reservation", reservation) + + http.Redirect(w, r, "/reservation-summary", http.StatusSeeOther) +} + +func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request) { + reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation) + if !ok { + log.Println("connot get item from reservation") + } + + data := make(map[string]interface{}) + data["reservation"] = reservation + + render.RenderTemplate(w, r, "reservation-summary.page.tmpl", &models.TemplateData{ + Data: data, + }) } // Availability is the search for availability page handler diff --git a/templates/reservation-summary.page.tmpl b/templates/reservation-summary.page.tmpl new file mode 100644 index 0000000..649668e --- /dev/null +++ b/templates/reservation-summary.page.tmpl @@ -0,0 +1,39 @@ +{{template "base" .}} +{{define "content"}} +{{$res := index .Data "reservation"}} +
+
+
+

Reservation Summary

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Name: {{$res.LastName}} {{$res.FirstName}}
Arrival:
Departure:
Email: {{$res.Email}}
Phone: {{$res.Phone}}
+
+
+
+{{end}}