feat: persist booked dates in JSON file and exclude booked dates from selection
This commit is contained in:
@@ -46,6 +46,7 @@ func main() {
|
||||
}
|
||||
|
||||
http.HandleFunc("/api/submit", handleSubmit)
|
||||
http.HandleFunc("/api/booked-dates", handleBookedDates)
|
||||
|
||||
// Serve static files from webDir directory
|
||||
fs := http.FileServer(http.Dir(webDir))
|
||||
@@ -81,6 +82,10 @@ func handleSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Printf("Received survey submission from %s for date %s (%s)", payload.Name, payload.Date, payload.Activity)
|
||||
|
||||
if err := recordBookedDate(payload.Date); err != nil {
|
||||
log.Printf("Error recording booked date: %v", err)
|
||||
}
|
||||
|
||||
err = sendEmail(payload)
|
||||
if err != nil {
|
||||
log.Printf("Error sending email: %v", err)
|
||||
@@ -101,6 +106,80 @@ func handleSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func handleBookedDates(w http.ResponseWriter, r *http.Request) {
|
||||
if enableCORS(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
dates, err := getBookedDates()
|
||||
if err != nil {
|
||||
log.Printf("Error reading booked dates: %v", err)
|
||||
dates = []string{}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(dates)
|
||||
}
|
||||
|
||||
func getBookedDatesFilePath() string {
|
||||
path := os.Getenv("BOOKED_DATES_FILE")
|
||||
if path == "" {
|
||||
path = "booked_dates.json"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func getBookedDates() ([]string, error) {
|
||||
filePath := getBookedDatesFilePath()
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var dates []string
|
||||
if err := json.NewDecoder(file).Decode(&dates); err != nil {
|
||||
return []string{}, nil
|
||||
}
|
||||
return dates, nil
|
||||
}
|
||||
|
||||
func recordBookedDate(dateStr string) error {
|
||||
if strings.TrimSpace(dateStr) == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
dates, err := getBookedDates()
|
||||
if err != nil {
|
||||
dates = []string{}
|
||||
}
|
||||
|
||||
for _, d := range dates {
|
||||
if d == dateStr {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
dates = append(dates, dateStr)
|
||||
|
||||
filePath := getBookedDatesFilePath()
|
||||
data, err := json.MarshalIndent(dates, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(filePath, data, 0644)
|
||||
}
|
||||
|
||||
func enableCORS(w http.ResponseWriter, r *http.Request) bool {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
||||
|
||||
Reference in New Issue
Block a user