From 9775b67a2d7cfa55f8a34ac67102a6fa6029943a Mon Sep 17 00:00:00 2001 From: vinchent Date: Thu, 11 Jul 2024 23:01:10 +0200 Subject: [PATCH] Connecting the rooms page to make reservation page --- cmd/web/routes.go | 1 + internal/handlers/handlers.go | 34 ++++++++++++++++++++ templates/majors.page.tmpl | 59 +++++++++++++++++++++++++++++++++-- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index c1bf556..44f7249 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -29,6 +29,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Post("/make-reservation", handlers.Repo.PostMakeReservation) mux.Get("/reservation-summary", handlers.Repo.ReservationSummary) mux.Get("/choose-room/{id}", handlers.Repo.ChooseRoom) + mux.Get("/book-room", handlers.Repo.BookRoom) 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 107c07a..47f5ab4 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -301,6 +301,7 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) { w.Write(out) } +// ChooseRoom displays list of available rooms func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) { roomID, err := strconv.Atoi(chi.URLParam(r, "id")) if err != nil { @@ -320,3 +321,36 @@ func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/make-reservation", http.StatusSeeOther) } + +// BookRoom takes URL parameters, builds a sessional variable, and takes user to make reservation +func (m *Repository) BookRoom(w http.ResponseWriter, r *http.Request) { + roomID, _ := strconv.Atoi(r.URL.Query().Get("id")) + sd := r.URL.Query().Get("s") + ed := r.URL.Query().Get("e") + + var res models.Reservation + + layout := "2006-01-02" + startDate, err := time.Parse(layout, sd) + if err != nil { + helpers.ServerError(w, err) + return + } + endDate, err := time.Parse(layout, ed) + if err != nil { + helpers.ServerError(w, err) + return + } + room, err := m.DB.GetRoomById(roomID) + if err != nil { + helpers.ServerError(w, err) + return + } + res.RoomID = roomID + res.StartDate = startDate + res.EndDate = endDate + res.Room.RoomName = room.RoomName + + m.App.Session.Put(r.Context(), "reservation", res) + http.Redirect(w, r, "/make-reservation", http.StatusSeeOther) +} diff --git a/templates/majors.page.tmpl b/templates/majors.page.tmpl index fa38f09..46a5959 100644 --- a/templates/majors.page.tmpl +++ b/templates/majors.page.tmpl @@ -32,7 +32,7 @@ {{end}}