feat: add bulk date management functionality and update admin interface

This commit is contained in:
troogs
2026-10-01 17:16:02 +02:00
parent 3605a42099
commit 84ed5a7a28
5 changed files with 371 additions and 141 deletions
+56
View File
@@ -82,6 +82,7 @@ func main() {
http.HandleFunc("/api/admin/login", handleAdminLogin)
http.HandleFunc("/api/admin/logout", handleAdminLogout)
http.HandleFunc("/api/admin/dates", handleAdminDates)
http.HandleFunc("/api/admin/dates/bulk", handleAdminBulkDates)
http.HandleFunc("/admin", handleAdminPage)
// Serve static files from webDir directory
@@ -281,6 +282,61 @@ func handleAdminDates(w http.ResponseWriter, r *http.Request) {
}
}
func handleAdminBulkDates(w http.ResponseWriter, r *http.Request) {
if !isAdminRequest(r) {
writeJSON(w, http.StatusUnauthorized, Response{Success: false, Message: "Anmeldung erforderlich."})
return
}
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var requestedDates []AvailableDate
if err := json.NewDecoder(r.Body).Decode(&requestedDates); err != nil {
writeJSON(w, http.StatusBadRequest, Response{Success: false, Message: "Ungültige Terminauswahl."})
return
}
seen := make(map[string]bool, len(requestedDates))
for _, date := range requestedDates {
if !isValidAvailableDate(date) || !isTodayOrFuture(date.Date) || seen[date.Date] {
writeJSON(w, http.StatusBadRequest, Response{Success: false, Message: "Bitte wähle gültige, eindeutige Daten ab heute."})
return
}
seen[date.Date] = true
}
bookedDates, err := getBookedDates()
if err != nil {
log.Printf("Error reading booked dates: %v", err)
http.Error(w, "Booked dates could not be loaded", http.StatusInternalServerError)
return
}
bookedDateSet := make(map[string]bool, len(bookedDates))
for _, date := range bookedDates {
bookedDateSet[date] = true
}
if err := updateAvailableDates(func(dates []AvailableDate) ([]AvailableDate, error) {
preserved := make([]AvailableDate, 0, len(dates)+len(requestedDates))
for _, date := range dates {
if !isTodayOrFuture(date.Date) || bookedDateSet[date.Date] {
preserved = append(preserved, date)
}
}
for _, date := range requestedDates {
if !bookedDateSet[date.Date] {
preserved = append(preserved, date)
}
}
return preserved, nil
}); err != nil {
log.Printf("Error saving available dates: %v", err)
http.Error(w, "Dates could not be saved", http.StatusInternalServerError)
return
}
writeJSON(w, http.StatusOK, Response{Success: true, Message: "Änderungen gespeichert."})
}
func getSettingsFilePath() string {
path := os.Getenv("SETTINGS_FILE")
if path == "" {