Connecting handlers to new database functions

This commit is contained in:
Muyao CHEN 2024-07-10 12:35:16 +02:00
parent 5f649d6d8e
commit ab02f8e635
2 changed files with 60 additions and 2 deletions

View File

@ -2,7 +2,6 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"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"
@ -190,7 +189,49 @@ func (m *Repository) Availability(w http.ResponseWriter, r *http.Request) {
func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) { func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
start := r.Form.Get("start") start := r.Form.Get("start")
end := r.Form.Get("end") end := r.Form.Get("end")
fmt.Fprintf(w, "Posted to search availability from %s to %s", start, end)
layout := "2006-01-02"
startDate, err := time.Parse(layout, start)
if err != nil {
helpers.ServerError(w, err)
return
}
endDate, err := time.Parse(layout, end)
if err != nil {
helpers.ServerError(w, err)
return
}
rooms, err := m.DB.SearchAvailabilityForAllRooms(startDate, endDate)
if err != nil {
helpers.ServerError(w, err)
return
}
for _, i := range rooms {
m.App.InfoLog.Println("ROOM:", i.ID, i.RoomName)
}
if len(rooms) == 0 {
// No availability
m.App.InfoLog.Println("No availability")
m.App.Session.Put(r.Context(), "error", "No availability")
http.Redirect(w, r, "/search-availability", http.StatusSeeOther)
}
data := make(map[string]interface{})
data["rooms"] = rooms
res := models.Reservation{
StartDate: startDate,
EndDate: endDate,
}
m.App.Session.Put(r.Context(), "reservation", res)
render.Template(w, r, "choose-room.page.tmpl", &models.TemplateData{
Data: data,
})
} }
type responseJSON struct { type responseJSON struct {

View File

@ -0,0 +1,17 @@
{{template "base" .}}
{{define "content"}}
<div class="container">
<div class="row">
<div class="col">
<h1>Choose a Room</h1>
{{$rooms := index .Data "rooms"}}
{{range $rooms}}
{{.RoomName}}<br>
{{end}}
</div>
</div>
</div>
{{end}}