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.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
|
||||||
@ -67,14 +70,33 @@ func (m *Repository) Majors(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (m *Repository) MakeReservation(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
|
// For the first time render emptyReservation so that this object is
|
||||||
// filled with the info when sent back.
|
// filled with the info when sent back.
|
||||||
var emptyReservation models.Reservation
|
res, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
|
||||||
data := make(map[string]interface{})
|
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{
|
render.Template(w, r, "make-reservation.page.tmpl", &models.TemplateData{
|
||||||
Form: forms.New(nil),
|
Form: forms.New(nil),
|
||||||
Data: data,
|
Data: data,
|
||||||
|
StringMap: stringMap,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,3 +278,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
|
||||||
@ -126,3 +126,23 @@ func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]
|
|||||||
|
|
||||||
return rooms, nil
|
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
|
InsertRoomRestriction(res models.RoomRestriction) error
|
||||||
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
|
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
|
||||||
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
|
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
|
||||||
|
GetRoomById(id int) (models.Room, error)
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
|
@ -6,9 +6,19 @@
|
|||||||
<h1 class="text-center mt-3">Make reservation</h1>
|
<h1 class="text-center mt-3">Make reservation</h1>
|
||||||
|
|
||||||
{{$res := index .Data "reservation"}}
|
{{$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="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="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">
|
<div class="form-group mt-5">
|
||||||
<label for="first_name">First name:</label>
|
<label for="first_name">First name:</label>
|
||||||
@ -27,20 +37,6 @@
|
|||||||
value="{{$res.LastName}}" required autocomplete="off">
|
value="{{$res.LastName}}" required autocomplete="off">
|
||||||
</div>
|
</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">
|
<div class="form-group mt-5">
|
||||||
<label for="email">Email:</label>
|
<label for="email">Email:</label>
|
||||||
{{with .Form.Errors.Get "email"}}
|
{{with .Form.Errors.Get "email"}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user