Add admin booking management prompt and deletion
This commit is contained in:
@@ -101,6 +101,7 @@ func main() {
|
||||
http.HandleFunc("/api/admin/dates", handleAdminDates)
|
||||
http.HandleFunc("/api/admin/dates/bulk", handleAdminBulkDates)
|
||||
http.HandleFunc("/api/admin/activities", handleAdminActivities)
|
||||
http.HandleFunc("/api/admin/bookings", handleAdminBookings)
|
||||
http.HandleFunc("/admin", handleAdminPage)
|
||||
|
||||
// Serve static files from webDir directory
|
||||
@@ -287,6 +288,65 @@ func handleAdminActivities(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleAdminBookings(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAdminRequest(r) {
|
||||
writeJSON(w, http.StatusUnauthorized, Response{Success: false, Message: "Anmeldung erforderlich."})
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
bookings, err := getBookings()
|
||||
if err != nil {
|
||||
log.Printf("Error reading bookings: %v", err)
|
||||
http.Error(w, "Bookings could not be loaded", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
sort.SliceStable(bookings, func(first, second int) bool {
|
||||
return bookings[first].BookedAt > bookings[second].BookedAt
|
||||
})
|
||||
writeJSON(w, http.StatusOK, bookings)
|
||||
case http.MethodDelete:
|
||||
var request struct {
|
||||
Date string `json:"date"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil || strings.TrimSpace(request.Date) == "" {
|
||||
writeJSON(w, http.StatusBadRequest, Response{Success: false, Message: "Bitte gib ein Datum an."})
|
||||
return
|
||||
}
|
||||
request.Date = strings.TrimSpace(request.Date)
|
||||
|
||||
bookingsMu.Lock()
|
||||
defer bookingsMu.Unlock()
|
||||
bookings, err := readBookings()
|
||||
if err != nil {
|
||||
log.Printf("Error reading bookings: %v", err)
|
||||
http.Error(w, "Bookings could not be loaded", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
filtered := make([]Booking, 0, len(bookings))
|
||||
deleted := false
|
||||
for _, booking := range bookings {
|
||||
if booking.Date == request.Date {
|
||||
deleted = true
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, booking)
|
||||
}
|
||||
if !deleted {
|
||||
writeJSON(w, http.StatusNotFound, Response{Success: false, Message: "Gebuchter Termin wurde nicht gefunden."})
|
||||
return
|
||||
}
|
||||
if err := writeJSONFile(getBookedDatesFilePath(), filtered); err != nil {
|
||||
log.Printf("Error deleting booking: %v", err)
|
||||
http.Error(w, "Booking could not be deleted", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, Response{Success: true, Message: "Gebuchter Termin gelöscht."})
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func handleAdminLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
Reference in New Issue
Block a user