time picker changes
This commit is contained in:
@@ -413,17 +413,30 @@ func GetAvailableHours(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "start and end query params required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
start, err := time.Parse("2006-01-02", startStr)
|
||||
|
||||
// Load UK timezone
|
||||
ukLocation, err := time.LoadLocation("Europe/London")
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load timezone", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse dates in UK timezone
|
||||
start, err := time.ParseInLocation("2006-01-02", startStr, ukLocation)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid start date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
end, err := time.Parse("2006-01-02", endStr)
|
||||
end, err := time.ParseInLocation("2006-01-02", endStr, ukLocation)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid end date", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Set to start of day (00:00:00) and end of day (23:59:59)
|
||||
start = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, ukLocation)
|
||||
end = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, ukLocation)
|
||||
|
||||
// Load default hours
|
||||
defaultMap := map[int]DefaultHours{}
|
||||
defRows, _ := db.DB.Query(r.Context(), `
|
||||
@@ -480,30 +493,36 @@ func GetAvailableHours(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Load bookings with their total duration (sum of all services)
|
||||
bookingRows, _ := db.DB.Query(r.Context(), `
|
||||
SELECT
|
||||
b.start_time,
|
||||
COALESCE(SUM(s.duration_minutes), 0) as total_duration
|
||||
FROM bookings b
|
||||
LEFT JOIN booking_services bs ON b.id = bs.booking_id
|
||||
LEFT JOIN services s ON bs.service_id = s.id
|
||||
WHERE b.start_time >= $1
|
||||
AND b.start_time < $2 + INTERVAL '1 day'
|
||||
AND b.status IN ('confirmed', 'pending')
|
||||
GROUP BY b.id, b.start_time
|
||||
ORDER BY b.start_time
|
||||
`, start, end)
|
||||
bookingRows, err := db.DB.Query(r.Context(), `
|
||||
SELECT
|
||||
b.start_time,
|
||||
COALESCE(SUM(s.duration_minutes), 0) as total_duration
|
||||
FROM bookings b
|
||||
LEFT JOIN booking_services bs ON b.id = bs.booking_id
|
||||
LEFT JOIN services s ON bs.service_id = s.id
|
||||
WHERE b.start_time >= $1
|
||||
AND b.start_time <= $2
|
||||
GROUP BY b.id, b.start_time
|
||||
ORDER BY b.start_time
|
||||
`, start, end)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "failed to query bookings", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
bookings := map[string][]TimeSlot{} // date -> booked slots
|
||||
for bookingRows.Next() {
|
||||
var startTime time.Time
|
||||
var durationMinutes int
|
||||
if err := bookingRows.Scan(&startTime, &durationMinutes); err == nil {
|
||||
dateStr := startTime.Format("2006-01-02")
|
||||
endTime := startTime.Add(time.Duration(durationMinutes) * time.Minute)
|
||||
// Convert to UK timezone for date formatting
|
||||
startTimeUK := startTime.In(ukLocation)
|
||||
dateStr := startTimeUK.Format("2006-01-02")
|
||||
endTime := startTimeUK.Add(time.Duration(durationMinutes) * time.Minute)
|
||||
|
||||
bookings[dateStr] = append(bookings[dateStr], TimeSlot{
|
||||
StartTime: startTime.Format("15:04"),
|
||||
StartTime: startTimeUK.Format("15:04"),
|
||||
EndTime: endTime.Format("15:04"),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user