diff --git a/cmd/web/routes.go b/cmd/web/routes.go index f34ea36..c1bf556 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -28,6 +28,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/make-reservation", handlers.Repo.MakeReservation) mux.Post("/make-reservation", handlers.Repo.PostMakeReservation) mux.Get("/reservation-summary", handlers.Repo.ReservationSummary) + mux.Get("/choose-room/{id}", handlers.Repo.ChooseRoom) 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 bb52d6c..90a7553 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -2,6 +2,7 @@ package handlers import ( "encoding/json" + "errors" "go-udemy-web-1/internal/config" "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/forms" @@ -13,6 +14,8 @@ import ( "net/http" "strconv" "time" + + "github.com/go-chi/chi/v5" ) // Repo the repository used by the handlers @@ -256,3 +259,23 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) { w.Write(out) } + +func (m *Repository) ChooseRoom(w http.ResponseWriter, r *http.Request) { + roomID, err := strconv.Atoi(chi.URLParam(r, "id")) + if err != nil { + helpers.ServerError(w, err) + return + } + m.App.Session.Get(r.Context(), "reservation") + + res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation) + if !ok { + helpers.ServerError(w, errors.New("cannot get reservation from session")) + return + } + + res.RoomID = roomID + m.App.Session.Put(r.Context(), "reservation", res) + + http.Redirect(w, r, "/make-reservation", http.StatusSeeOther) +} diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go index 4b02729..a177681 100644 --- a/internal/repository/dbrepo/postgres.go +++ b/internal/repository/dbrepo/postgres.go @@ -100,7 +100,7 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([] rooms r where r.id not in (select - roomId + room_id from room_restrictions rr where diff --git a/templates/choose-room.page.tmpl b/templates/choose-room.page.tmpl index 4673df4..ee17224 100644 --- a/templates/choose-room.page.tmpl +++ b/templates/choose-room.page.tmpl @@ -8,7 +8,7 @@ {{$rooms := index .Data "rooms"}} {{range $rooms}} - {{.RoomName}}
+
  • {{.RoomName}}
  • {{end}}