From 61264051fa0049a2b5302922ec664dec743e4425 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Wed, 11 Jun 2025 23:19:17 +0200 Subject: [PATCH] test --- main.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/main.go b/main.go index d9baefa..39a6f6e 100644 --- a/main.go +++ b/main.go @@ -156,6 +156,10 @@ func main() { r.HandleFunc("/api/reservations", handleCreateReservation).Methods("POST") r.HandleFunc("/api/check-availability", handleCheckAvailability).Methods("GET") + // Add these new routes after existing reservation endpoints + r.HandleFunc("/api/reservations/{id}", handleUpdateReservation).Methods("PUT") + r.HandleFunc("/api/reservations/{id}", handleDeleteReservation).Methods("DELETE") + // Protected API routes with auth middleware api := r.PathPrefix("/api").Subrouter() api.Use(authMiddleware) @@ -1238,3 +1242,84 @@ func sendEmail(entry TripEntry, parsedDateStart, parsedDateEnd time.Time, czechM return d.DialAndSend(m) } + +// Add these new handler functions before the existing banner handlers + +func handleUpdateReservation(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + reservationID := vars["id"] + + var updatedReservation Reservation + if err := json.NewDecoder(r.Body).Decode(&updatedReservation); err != nil { + 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", http.StatusInternalServerError) + return + } + + // Find and update the reservation + found := false + for i, res := range reservations { + if res.ID == reservationID { + updatedReservation.ID = reservationID // Ensure ID remains unchanged + reservations[i] = updatedReservation + found = true + break + } + } + + if !found { + http.Error(w, "Reservation not found", http.StatusNotFound) + return + } + + // Save updated reservations + if err := saveReservations(reservations); err != nil { + http.Error(w, "Failed to save reservation", http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(updatedReservation) +} + +func handleDeleteReservation(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + reservationID := vars["id"] + + // Load existing reservations + reservations, err := loadReservations() + if err != nil { + http.Error(w, "Failed to load reservations", http.StatusInternalServerError) + return + } + + // Find and remove the reservation + found := false + var updatedReservations []Reservation + for _, res := range reservations { + if res.ID == reservationID { + found = true + continue + } + updatedReservations = append(updatedReservations, res) + } + + if !found { + http.Error(w, "Reservation not found", http.StatusNotFound) + return + } + + // Save updated reservations + if err := saveReservations(updatedReservations); err != nil { + http.Error(w, "Failed to save reservations", http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) +}