Compare commits
2 Commits
ab02f8e635
...
95cddfc950
Author | SHA1 | Date | |
---|---|---|---|
95cddfc950 | |||
5a646a2b27 |
@ -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))
|
||||
|
@ -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
|
||||
@ -67,14 +70,33 @@ func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
|
||||
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{})
|
||||
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
||||
if !ok {
|
||||
helpers.ServerError(w, errors.New("cannot get reservation from session"))
|
||||
return
|
||||
}
|
||||
|
||||
data["reservation"] = emptyReservation
|
||||
room, err := m.DB.GetRoomById(res.RoomID)
|
||||
if err != nil {
|
||||
helpers.ServerError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
res.Room.RoomName = room.RoomName
|
||||
|
||||
sd := res.StartDate.Format("2006-01-02")
|
||||
ed := res.EndDate.Format("2006-01-02")
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["start_date"] = sd
|
||||
stringMap["end_date"] = ed
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["reservation"] = res
|
||||
|
||||
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
|
||||
Form: forms.New(nil),
|
||||
Data: data,
|
||||
Form: forms.New(nil),
|
||||
Data: data,
|
||||
StringMap: stringMap,
|
||||
})
|
||||
}
|
||||
|
||||
@ -256,3 +278,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)
|
||||
}
|
||||
|
@ -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
|
||||
@ -126,3 +126,23 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]
|
||||
|
||||
return rooms, nil
|
||||
}
|
||||
|
||||
// GetRoomById gets a room by id
|
||||
func (m *postgresDBRepo) GetRoomById(id int) (models.Room, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var room models.Room
|
||||
stmt := `select
|
||||
id, room_name, created_at, updated_at
|
||||
from
|
||||
rooms
|
||||
where id = $1`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, stmt, id)
|
||||
err := row.Scan(&room.ID, &room.RoomName, &room.CreatedAt, &room.UpdatedAt)
|
||||
if err != nil {
|
||||
return room, err
|
||||
}
|
||||
return room, nil
|
||||
}
|
||||
|
@ -12,4 +12,5 @@ type DatabaseRepo interface {
|
||||
InsertRoomRestriction(res models.RoomRestriction) error
|
||||
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
|
||||
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
|
||||
GetRoomById(id int) (models.Room, error)
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
{{$rooms := index .Data "rooms"}}
|
||||
|
||||
{{range $rooms}}
|
||||
{{.RoomName}}<br>
|
||||
<li><a href="/choose-room/{{.ID}}">{{.RoomName}}</a></li>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -6,9 +6,19 @@
|
||||
<h1 class="text-center mt-3">Make reservation</h1>
|
||||
|
||||
{{$res := index .Data "reservation"}}
|
||||
<p><strong>Reservation Details</strong><br>
|
||||
Room: {{$res.Room.RoomName}}<br>
|
||||
Arrival: {{index .StringMap "start_date"}}<br>
|
||||
Departure: {{index .StringMap "end_date"}}
|
||||
</p>
|
||||
|
||||
<!-- <form method="post" action="" class="needs-validation" novalidate> -->
|
||||
<form method="post" action="" class="" novalidate>
|
||||
<form method="post" action="/make-reservation" class="" novalidate>
|
||||
<input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
|
||||
<input type="hidden" name="start_date" value="{{index .StringMap "start_date"}}">
|
||||
<input type="hidden" name="end_date" value="{{index .StringMap "end_date"}}">
|
||||
<input type="hidden" name="room_id" value="{{$res.RoomID}}">
|
||||
|
||||
|
||||
<div class="form-group mt-5">
|
||||
<label for="first_name">First name:</label>
|
||||
@ -27,20 +37,6 @@
|
||||
value="{{$res.LastName}}" required autocomplete="off">
|
||||
</div>
|
||||
|
||||
<div class="form-group mt-5">
|
||||
<label for="start_date">Start date:</label>
|
||||
<input type="text" name="start_date" id="start_date" class="form-control"
|
||||
required autocomplete="off">
|
||||
</div>
|
||||
|
||||
<div class="form-group mt-5">
|
||||
<label for="end_date">End date:</label>
|
||||
<input type="text" name="end_date" id="end_date" class="form-control"
|
||||
required autocomplete="off">
|
||||
</div>
|
||||
|
||||
<input type="hidden" name="room_id" value="1">
|
||||
|
||||
<div class="form-group mt-5">
|
||||
<label for="email">Email:</label>
|
||||
{{with .Form.Errors.Get "email"}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user