This commit is contained in:
Tomas Dvorak
2025-06-11 14:54:57 +02:00
parent 03f0e472dd
commit 6cc7f2865c
2 changed files with 90 additions and 9 deletions
+16 -5
View File
@@ -144,6 +144,16 @@ func main() {
r.HandleFunc("/api/banner", GetBannerHandler).Methods("GET", "OPTIONS") r.HandleFunc("/api/banner", GetBannerHandler).Methods("GET", "OPTIONS")
r.HandleFunc("/submit", handleSubmit).Methods("POST", "OPTIONS") // Public submit endpoint for evidence-aut.html r.HandleFunc("/submit", handleSubmit).Methods("POST", "OPTIONS") // Public submit endpoint for evidence-aut.html
// Add redirect for /rezervace-aut to /rezervace-aut.html
r.HandleFunc("/rezervace-aut", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "rezervace-aut.html")
}).Methods("GET")
// Make reservation endpoints public by moving them outside of the protected API routes
r.HandleFunc("/api/reservations", handleGetReservations).Methods("GET")
r.HandleFunc("/api/reservations", handleCreateReservation).Methods("POST")
r.HandleFunc("/api/check-availability", handleCheckAvailability).Methods("GET")
// Protected API routes with auth middleware // Protected API routes with auth middleware
api := r.PathPrefix("/api").Subrouter() api := r.PathPrefix("/api").Subrouter()
api.Use(authMiddleware) api.Use(authMiddleware)
@@ -159,11 +169,6 @@ func main() {
api.HandleFunc("/apps/{id}", UpdateAppHandler).Methods("PUT") api.HandleFunc("/apps/{id}", UpdateAppHandler).Methods("PUT")
api.HandleFunc("/apps/{id}", DeleteAppHandler).Methods("DELETE") api.HandleFunc("/apps/{id}", DeleteAppHandler).Methods("DELETE")
// Reservation system routes
api.HandleFunc("/reservations", handleGetReservations).Methods("GET")
api.HandleFunc("/reservations", handleCreateReservation).Methods("POST")
api.HandleFunc("/check-availability", handleCheckAvailability).Methods("GET")
// Admin routes - defined before the catch-all static file server // Admin routes - defined before the catch-all static file server
r.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "admin.html") http.ServeFile(w, r, "admin.html")
@@ -315,6 +320,12 @@ func handleCreateReservation(w http.ResponseWriter, r *http.Request) {
return return
} }
// Only validate required fields (purpose is now optional)
if reservation.DriverName == "" || reservation.Vehicle == "" {
http.Error(w, "Driver name and vehicle are required", http.StatusBadRequest)
return
}
reservation.ID = fmt.Sprintf("res_%d", time.Now().UnixNano()) reservation.ID = fmt.Sprintf("res_%d", time.Now().UnixNano())
reservations, err := loadReservations() reservations, err := loadReservations()
+74 -4
View File
@@ -309,10 +309,15 @@
</div> </div>
<!-- Purpose --> <!-- Purpose -->
<div class="form-group"> <div class="space-y-2">
<label for="purpose">Účel jízdy</label> <label for="purpose" class="block text-sm font-medium text-gray-700">Účel jízdy (nepovinné)</label>
<textarea id="purpose" name="purpose" rows="3" required <div class="relative">
class="w-full p-2 border border-gray-300 rounded-md"></textarea> <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
<i class="fas fa-briefcase text-gray-400"></i>
</div>
<input type="text" id="purpose" name="purpose"
class="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-brand-light-blue focus:border-brand-light-blue">
</div>
</div> </div>
<!-- Submit Button --> <!-- Submit Button -->
@@ -485,6 +490,71 @@
}, 5000); }, 5000);
} }
}); });
function showMessage(text, type) {
const message = document.getElementById('message');
const messageText = document.getElementById('messageText');
const messageIcon = document.getElementById('messageIcon');
if (!message || !messageText || !messageIcon) {
console.error('Message elements not found');
return;
}
message.classList.remove('hidden', 'bg-green-50', 'bg-red-50', 'bg-blue-50', 'text-green-800', 'text-red-800', 'text-blue-800');
messageText.textContent = text;
switch(type) {
case 'success':
message.classList.add('bg-green-50', 'text-green-800');
messageIcon.className = 'fas fa-check-circle text-green-600 mr-2';
break;
case 'error':
message.classList.add('bg-red-50', 'text-red-800');
messageIcon.className = 'fas fa-exclamation-circle text-red-600 mr-2';
break;
case 'info':
message.classList.add('bg-blue-50', 'text-blue-800');
messageIcon.className = 'fas fa-info-circle text-blue-600 mr-2';
break;
}
message.classList.remove('hidden');
}
// Update form submission to handle API errors better
form.addEventListener('submit', async (e) => {
e.preventDefault();
try {
showMessage('Odesílání rezervace...', 'info');
const response = await fetch('/api/reservations', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
driverName: document.getElementById('driverName').value,
vehicle: document.getElementById('vehicle').value,
startDateTime: new Date(dateStart.value + 'T' + timeStart.value).toISOString(),
endDateTime: new Date(dateEnd.value + 'T' + timeEnd.value).toISOString(),
purpose: document.getElementById('purpose').value || undefined // Make purpose optional
})
});
if (!response.ok) {
throw new Error(response.statusText || 'Došlo k chybě při vytváření rezervace');
}
showMessage('Rezervace byla úspěšně vytvořena', 'success');
form.reset();
dateStart.value = todayStr;
dateEnd.value = todayStr;
} catch (error) {
console.error('Error:', error);
showMessage(error.message || 'Nepodařilo se vytvořit rezervaci', 'error');
}
});
</script> </script>
</body> </body>
</html> </html>