Displaying a response to user after posting form data
This commit is contained in:
parent
6826634a01
commit
3ad57c754f
@ -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
|
||||
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
|
39
templates/reservation-summary.page.tmpl
Normal file
39
templates/reservation-summary.page.tmpl
Normal 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}}
|
Loading…
Reference in New Issue
Block a user