Displaying a response to user after posting form data

This commit is contained in:
Muyao CHEN 2024-07-01 14:19:38 +02:00
parent 6826634a01
commit 3ad57c754f
4 changed files with 71 additions and 8 deletions

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"encoding/gob"
"fmt" "fmt"
"go-udemy-web-1/internal/config" "go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/handlers" "go-udemy-web-1/internal/handlers"
"go-udemy-web-1/internal/models"
"go-udemy-web-1/internal/render" "go-udemy-web-1/internal/render"
"log" "log"
"net/http" "net/http"
@ -21,6 +23,9 @@ var (
// main is the main application function // main is the main application function
func main() { func main() {
// what am I going to put in the session
gob.Register(models.Reservation{})
// change this to true when in production // change this to true when in production
app.InProduction = false app.InProduction = false

View File

@ -27,6 +27,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Post("/availability-json", handlers.Repo.AvailabilityJSON) mux.Post("/availability-json", handlers.Repo.AvailabilityJSON)
mux.Get("/make-reservation", handlers.Repo.MakeReservation) mux.Get("/make-reservation", handlers.Repo.MakeReservation)
mux.Post("/make-reservation", handlers.Repo.PostMakeReservation) mux.Post("/make-reservation", handlers.Repo.PostMakeReservation)
mux.Get("/reservation-summary", handlers.Repo.ReservationSummary)
fileServer := http.FileServer(http.Dir("./static/")) fileServer := http.FileServer(http.Dir("./static/"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) mux.Handle("/static/*", http.StripPrefix("/static", fileServer))

View File

@ -113,6 +113,24 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
}) })
return 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 // Availability is the search for availability page handler

View File

@ -0,0 +1,39 @@
{{template "base" .}}
{{define "content"}}
{{$res := index .Data "reservation"}}
<div class="container">
<div class="row">
<div class="col mt-5">
<h1>Reservation Summary</h1>
<hr>
<table class="table table-striped">
<thead></thead>
<tbody>
<tr>
<td>Name: </td>
<td>{{$res.LastName}} {{$res.FirstName}}</td>
</tr>
<tr>
<td>Arrival: </td>
<td></td>
</tr>
<tr>
<td>Departure: </td>
<td></td>
</tr>
<tr>
<td>Email: </td>
<td>{{$res.Email}}</td>
</tr>
<tr>
<td>Phone: </td>
<td>{{$res.Phone}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
{{end}}