Cleaning up the reservation summary page and improbing validation

This commit is contained in:
vinchent 2024-07-10 22:54:28 +02:00
parent 95cddfc950
commit e0a29d21c3
3 changed files with 32 additions and 38 deletions

View File

@ -83,6 +83,7 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
}
res.Room.RoomName = room.RoomName
m.App.Session.Put(r.Context(), "reservation", res)
sd := res.StartDate.Format("2006-01-02")
ed := res.EndDate.Format("2006-01-02")
@ -102,43 +103,21 @@ func (m *Repository) MakeReservation(w http.ResponseWriter, r *http.Request) {
// PostMakeReservation is the make reservation page post handler
func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request) {
reservation, ok := m.App.Session.Get(r.Context(), "reservation").(models.Reservation)
if !ok {
helpers.ServerError(w, errors.New("connot get item from session"))
return
}
err := r.ParseForm()
if err != nil {
helpers.ServerError(w, err)
return
}
sd := r.Form.Get("start_date")
ed := r.Form.Get("end_date")
// 2024-07-08 -- 01/02 03:04:05PM '06 -0700
layout := "2006-01-02"
startDate, err := time.Parse(layout, sd)
if err != nil {
helpers.ServerError(w, err)
return
}
endDate, err := time.Parse(layout, ed)
if err != nil {
helpers.ServerError(w, err)
return
}
roomId, err := strconv.Atoi(r.Form.Get("room_id"))
if err != nil {
helpers.ServerError(w, err)
return
}
reservation := models.Reservation{
FirstName: r.Form.Get("first_name"),
LastName: r.Form.Get("last_name"),
Email: r.Form.Get("email"),
Phone: r.Form.Get("phone"),
StartDate: startDate,
EndDate: endDate,
RoomID: roomId,
}
reservation.FirstName = r.Form.Get("first_name")
reservation.LastName = r.Form.Get("last_name")
reservation.Email = r.Form.Get("email")
reservation.Phone = r.Form.Get("phone")
form := forms.New(r.PostForm)
@ -163,11 +142,13 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
return
}
m.App.Session.Put(r.Context(), "reservation", reservation)
restriction := models.RoomRestriction{
StartDate: startDate,
EndDate: endDate,
ID: 0,
RoomID: roomId,
StartDate: reservation.StartDate,
EndDate: reservation.EndDate,
ID: reservation.ID,
RoomID: reservation.RoomID,
ReservationID: newReservationID,
RestrictionID: 1,
}
@ -197,8 +178,16 @@ func (m *Repository) ReservationSummary(w http.ResponseWriter, r *http.Request)
data := make(map[string]interface{})
data["reservation"] = reservation
sd := reservation.StartDate.Format("2006-01-02")
ed := reservation.EndDate.Format("2006-01-02")
stringMap := map[string]string{
"start_date": sd,
"end_date": ed,
}
render.Template(w, r, "reservation-summary.page.tmpl", &models.TemplateData{
Data: data,
Data: data,
StringMap: stringMap,
})
}

View File

@ -33,6 +33,7 @@
const elem = document.getElementById('reservation-dates');
const rangepicker = new DateRangePicker(elem, {
"format": "yyyy-mm-dd",
"minDate": new Date(),
});
</script>
{{end}}

View File

@ -15,13 +15,17 @@
<td>Name: </td>
<td>{{$res.LastName}} {{$res.FirstName}}</td>
</tr>
<tr>
<td>Room: </td>
<td>{{$res.Room.RoomName}}</td>
</tr>
<tr>
<td>Arrival: </td>
<td></td>
<td>{{index .StringMap "start_date"}}</td>
</tr>
<tr>
<td>Departure: </td>
<td></td>
<td>{{index .StringMap "end_date"}}</td>
</tr>
<tr>
<td>Email: </td>