Searching for availability by Room

This commit is contained in:
vinchent 2024-07-10 23:09:45 +02:00
parent e0a29d21c3
commit 5eb9284b6f
2 changed files with 37 additions and 6 deletions

View File

@ -246,15 +246,42 @@ func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
} }
type responseJSON struct { type responseJSON struct {
OK string `json:"ok"` OK bool `json:"ok"`
Message string `json:"message"` Message string `json:"message"`
} }
// AvailabilityJSON is the search for availability page handler // AvailabilityJSON is the search for availability page handler
func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) { func (m *Repository) AvailabilityJSON(w http.ResponseWriter, r *http.Request) {
sd := r.Form.Get("start")
ed := r.Form.Get("end")
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
}
available, err := m.DB.SearchAvailabilityByDatesByRoomID(startDate, endDate, roomID)
if err != nil {
helpers.ServerError(w, err)
return
}
resp := responseJSON{ resp := responseJSON{
OK: "true", OK: available,
Message: "Available!", Message: "",
} }
out, err := json.MarshalIndent(resp, "", " ") out, err := json.MarshalIndent(resp, "", " ")

View File

@ -51,6 +51,7 @@
const rp = new DateRangePicker(elem, { const rp = new DateRangePicker(elem, {
"format": "yyyy-mm-dd", "format": "yyyy-mm-dd",
showOnFocus: true, showOnFocus: true,
minDate: new Date(),
}); });
}, },
didOpen: () => { didOpen: () => {
@ -60,11 +61,10 @@
] ]
}, },
callback: (result) => { callback: (result) => {
console.log(result);
const formElem = document.getElementById("check-availability-form"); const formElem = document.getElementById("check-availability-form");
let formData = new FormData(formElem); let formData = new FormData(formElem);
formData.append("csrf_token", "{{.CSRFToken}}"); formData.append("csrf_token", "{{.CSRFToken}}");
formData.append("room_id", "1")
fetch('/availability-json', { fetch('/availability-json', {
method: "post", method: "post",
@ -72,7 +72,11 @@
}) })
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
console.log(data); if (data.ok) {
console.log("room is available")
} else {
console.log("room is not available")
}
}) })
}, },
}); });