From cd194a754471a8f60c8df8251185c98e848780bc Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Tue, 17 Jun 2025 08:18:19 +0200 Subject: [PATCH] test --- main.go | 58 ++++++++++++--------------------------------------------- 1 file changed, 12 insertions(+), 46 deletions(-) diff --git a/main.go b/main.go index f9e7c3a..27caec6 100644 --- a/main.go +++ b/main.go @@ -1271,44 +1271,27 @@ func sendEmail(entry TripEntry, parsedDateStart, parsedDateEnd time.Time, czechM // Add these new handler functions before the existing banner handlers func handleUpdateReservation(w http.ResponseWriter, r *http.Request) { - // Set CORS headers - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Access-Control-Allow-Methods", "PUT, OPTIONS") - w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - - // Handle preflight - if r.Method == "OPTIONS" { - w.WriteHeader(http.StatusOK) - return - } - vars := mux.Vars(r) - id := vars["id"] - if id == "" { - http.Error(w, "Missing reservation ID", http.StatusBadRequest) - return - } + reservationID := vars["id"] - // Parse the updated reservation from request body var updatedReservation Reservation if err := json.NewDecoder(r.Body).Decode(&updatedReservation); err != nil { - http.Error(w, "Invalid reservation data: "+err.Error(), http.StatusBadRequest) + http.Error(w, "Invalid reservation data", http.StatusBadRequest) return } // Load existing reservations reservations, err := loadReservations() if err != nil { - http.Error(w, "Failed to load reservations: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Failed to load reservations", http.StatusInternalServerError) return } // Find and update the reservation found := false for i, res := range reservations { - if res.ID == id { - updatedReservation.ID = id // Ensure ID remains the same + if res.ID == reservationID { + updatedReservation.ID = reservationID // Ensure ID remains unchanged reservations[i] = updatedReservation found = true break @@ -1322,38 +1305,22 @@ func handleUpdateReservation(w http.ResponseWriter, r *http.Request) { // Save updated reservations if err := saveReservations(reservations); err != nil { - http.Error(w, "Failed to save reservations: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Failed to save reservation", http.StatusInternalServerError) return } - // Return the updated reservation + w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(updatedReservation) } func handleDeleteReservation(w http.ResponseWriter, r *http.Request) { - // Set CORS headers - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Access-Control-Allow-Methods", "DELETE, OPTIONS") - w.Header().Set("Access-Control-Allow-Headers", "Content-Type") - - // Handle preflight - if r.Method == "OPTIONS" { - w.WriteHeader(http.StatusOK) - return - } - vars := mux.Vars(r) - id := vars["id"] - if id == "" { - http.Error(w, "Missing reservation ID", http.StatusBadRequest) - return - } + reservationID := vars["id"] // Load existing reservations reservations, err := loadReservations() if err != nil { - http.Error(w, "Failed to load reservations: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Failed to load reservations", http.StatusInternalServerError) return } @@ -1361,9 +1328,9 @@ func handleDeleteReservation(w http.ResponseWriter, r *http.Request) { found := false var updatedReservations []Reservation for _, res := range reservations { - if res.ID == id { + if res.ID == reservationID { found = true - continue // Skip this reservation to remove it + continue } updatedReservations = append(updatedReservations, res) } @@ -1375,10 +1342,9 @@ func handleDeleteReservation(w http.ResponseWriter, r *http.Request) { // Save updated reservations if err := saveReservations(updatedReservations); err != nil { - http.Error(w, "Failed to save reservations: "+err.Error(), http.StatusInternalServerError) + http.Error(w, "Failed to save reservations", http.StatusInternalServerError) return } - // Return success with no content w.WriteHeader(http.StatusNoContent) }