Cleaning up our make reservation page and testing everything

This commit is contained in:
vinchent 2024-07-10 22:34:27 +02:00
parent 5a646a2b27
commit 95cddfc950
4 changed files with 56 additions and 20 deletions

View File

@ -70,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,
StringMap: stringMap,
})
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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"}}