mirror of
https://github.com/Dvorinka/PPve.git
synced 2026-06-03 20:12:59 +00:00
te
This commit is contained in:
@@ -373,27 +373,41 @@ func handleCreateReservation(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleCheckAvailability(w http.ResponseWriter, r *http.Request) {
|
func handleCheckAvailability(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Get query parameters
|
||||||
vehicle := r.URL.Query().Get("vehicle")
|
vehicle := r.URL.Query().Get("vehicle")
|
||||||
startDate := r.URL.Query().Get("startDate")
|
startDate := r.URL.Query().Get("startDate")
|
||||||
startTime := r.URL.Query().Get("startTime")
|
startTime := r.URL.Query().Get("startTime")
|
||||||
endDate := r.URL.Query().Get("endDate")
|
endDate := r.URL.Query().Get("endDate")
|
||||||
endTime := r.URL.Query().Get("endTime")
|
endTime := r.URL.Query().Get("endTime")
|
||||||
|
|
||||||
// Parse combined date and time strings
|
// Validate inputs
|
||||||
|
if vehicle == "" || startDate == "" || startTime == "" || endDate == "" || endTime == "" {
|
||||||
|
http.Error(w, "Missing required parameters", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse dates in correct format (YYYY-MM-DD HH:MM)
|
||||||
startDateTime, err := time.Parse("2006-01-02 15:04",
|
startDateTime, err := time.Parse("2006-01-02 15:04",
|
||||||
fmt.Sprintf("%s %s", startDate, startTime))
|
fmt.Sprintf("%s %s", startDate, startTime))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Invalid start date/time", http.StatusBadRequest)
|
http.Error(w, "Invalid start date/time format", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
endDateTime, err := time.Parse("2006-01-02 15:04",
|
endDateTime, err := time.Parse("2006-01-02 15:04",
|
||||||
fmt.Sprintf("%s %s", endDate, endTime))
|
fmt.Sprintf("%s %s", endDate, endTime))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Invalid end date/time", http.StatusBadRequest)
|
http.Error(w, "Invalid end date/time format", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate time order
|
||||||
|
if endDateTime.Before(startDateTime) {
|
||||||
|
http.Error(w, "End time must be after start time", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check availability
|
||||||
reservations, err := loadReservations()
|
reservations, err := loadReservations()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to load reservations", http.StatusInternalServerError)
|
http.Error(w, "Failed to load reservations", http.StatusInternalServerError)
|
||||||
@@ -424,6 +438,7 @@ func handleCheckAvailability(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return response
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]bool{"available": available})
|
json.NewEncoder(w).Encode(map[string]bool{"available": available})
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-2
@@ -479,10 +479,17 @@
|
|||||||
// Check vehicle availability
|
// Check vehicle availability
|
||||||
async function checkVehicleAvailability(vehicle, startDate, endDate) {
|
async function checkVehicleAvailability(vehicle, startDate, endDate) {
|
||||||
try {
|
try {
|
||||||
|
// Format dates correctly
|
||||||
|
const formatDateTime = (date) => {
|
||||||
|
return date.toISOString().split('.')[0]; // Remove milliseconds
|
||||||
|
};
|
||||||
|
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
vehicle: vehicle,
|
vehicle: vehicle,
|
||||||
start: startDate.toISOString(),
|
startDate: startDate.toISOString().split('T')[0],
|
||||||
end: endDate.toISOString()
|
startTime: startDate.toTimeString().split(' ')[0].slice(0, 5),
|
||||||
|
endDate: endDate.toISOString().split('T')[0],
|
||||||
|
endTime: endDate.toTimeString().split(' ')[0].slice(0, 5)
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await fetch(`/api/check-availability?${params}`);
|
const response = await fetch(`/api/check-availability?${params}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user