Connection search availability to the make reservation page
This commit is contained in:
		| @ -28,6 +28,7 @@ func routes(app *config.AppConfig) http.Handler { | |||||||
| 	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) | 	mux.Get("/reservation-summary", handlers.Repo.ReservationSummary) | ||||||
|  | 	mux.Get("/choose-room/{id}", handlers.Repo.ChooseRoom) | ||||||
|  |  | ||||||
| 	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)) | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ package handlers | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
|  | 	"errors" | ||||||
| 	"go-udemy-web-1/internal/config" | 	"go-udemy-web-1/internal/config" | ||||||
| 	"go-udemy-web-1/internal/driver" | 	"go-udemy-web-1/internal/driver" | ||||||
| 	"go-udemy-web-1/internal/forms" | 	"go-udemy-web-1/internal/forms" | ||||||
| @ -13,6 +14,8 @@ import ( | |||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/go-chi/chi/v5" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // Repo the repository used by the handlers | // Repo the repository used by the handlers | ||||||
| @ -256,3 +259,23 @@ func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) { | |||||||
|  |  | ||||||
| 	w.Write(out) | 	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) | ||||||
|  | } | ||||||
|  | |||||||
| @ -100,7 +100,7 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([] | |||||||
|                 rooms r |                 rooms r | ||||||
|             where r.id not in |             where r.id not in | ||||||
|                 (select |                 (select | ||||||
|                     roomId |                     room_id | ||||||
|                 from |                 from | ||||||
|                     room_restrictions rr |                     room_restrictions rr | ||||||
|                 where |                 where | ||||||
|  | |||||||
| @ -8,7 +8,7 @@ | |||||||
|             {{$rooms := index .Data "rooms"}} |             {{$rooms := index .Data "rooms"}} | ||||||
|  |  | ||||||
|             {{range $rooms}} |             {{range $rooms}} | ||||||
|             {{.RoomName}}<br> |             <li><a href="/choose-room/{{.ID}}">{{.RoomName}}</a></li> | ||||||
|             {{end}} |             {{end}} | ||||||
|         </div> |         </div> | ||||||
|     </div> |     </div> | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user