Skip to content

Commit

Permalink
added the final step to redirect from get-request to make reservation…
Browse files Browse the repository at this point in the history
… via writing a reservation into session.
  • Loading branch information
jagottsicher committed Jul 13, 2023
1 parent c90d9b5 commit b1a3ac9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Post("/reservation", handlers.Repo.PostReservation)
mux.Post("/reservation-json", handlers.Repo.ReservationJSON)
mux.Get("/choose-bungalow/{id}", handlers.Repo.ChooseBungalow)
mux.Get("/book-bungalow", handlers.Repo.BookBungalow)
mux.Get("/make-reservation", handlers.Repo.MakeReservation)
mux.Post("/make-reservation", handlers.Repo.PostMakeReservation)
mux.Get("/reservation-overview", handlers.Repo.ReservationOverview)
Expand Down
33 changes: 33 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,36 @@ func (m *Repository) ChooseBungalow(w http.ResponseWriter, r *http.Request) {

http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}

// BookBungalow takes URL parameters from get request,
// builds a reservation, stores it in a session,
// and redirects to make-reservation page
func (m *Repository) BookBungalow(w http.ResponseWriter, r *http.Request) {

bungalowID, _ := strconv.Atoi(r.URL.Query().Get("id"))

sd := r.URL.Query().Get("s")
ed := r.URL.Query().Get("e")

layout := "2006-01-02"
startDate, _ := time.Parse(layout, sd)
endDate, _ := time.Parse(layout, ed)

var res models.Reservation

bungalow, err := m.DB.GetBungalowByID(bungalowID)
if err != nil {
m.App.Session.Put(r.Context(), "error", "Cannot find bungalow!")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}

res.Bungalow.BungalowName = bungalow.BungalowName
res.BungalowID = bungalowID
res.StartDate = startDate
res.EndDate = endDate

m.App.Session.Put(r.Context(), "reservation", res)

http.Redirect(w, r, "/make-reservation", http.StatusSeeOther)
}

0 comments on commit b1a3ac9

Please sign in to comment.