admin features
This commit is contained in:
@@ -256,6 +256,15 @@ fn hour_options() -> Vec<serde_json::Value> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// End-of-block hour options (07:00‥22:00) for the "until" select. A booking
|
||||
/// covers the hours in `[start, end)`, so the end runs one slot past the last
|
||||
/// bookable hour.
|
||||
fn end_hour_options() -> Vec<serde_json::Value> {
|
||||
(FIRST_HOUR + 1..=LAST_HOUR + 1)
|
||||
.map(|h| data!({ "v": h, "label": format!("{h:02}:00") }))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct NewBookingQuery {
|
||||
pub court: Option<i32>,
|
||||
@@ -286,6 +295,9 @@ pub async fn booking_new(
|
||||
.and_then(|c| c.name.clone())
|
||||
.unwrap_or_else(|| format!("Court {court_id}"));
|
||||
|
||||
let hour_start = q.hour.unwrap_or(FIRST_HOUR).clamp(FIRST_HOUR, LAST_HOUR);
|
||||
let hour_end = (hour_start + 1).min(LAST_HOUR + 1);
|
||||
|
||||
format::render().view(
|
||||
&v,
|
||||
"admin/booking_form.html",
|
||||
@@ -298,18 +310,22 @@ pub async fn booking_new(
|
||||
"court_id": court_id,
|
||||
"court_name": court_name,
|
||||
"date": q.date.unwrap_or_default(),
|
||||
"hour": q.hour.unwrap_or(FIRST_HOUR),
|
||||
"hour_start": hour_start,
|
||||
"hour_end": hour_end,
|
||||
"repeat_weeks": 1,
|
||||
"color": "#3b82f6",
|
||||
"name": "",
|
||||
"title": "",
|
||||
"contact": "",
|
||||
"note": "",
|
||||
"hours": hour_options(),
|
||||
"hours_end": end_hour_options(),
|
||||
"booking_id": 0,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Form fields for editing a single existing booking.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct BookingForm {
|
||||
pub court_id: i32,
|
||||
@@ -322,31 +338,86 @@ pub struct BookingForm {
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
/// Form fields for creating bookings. Unlike editing, creation books a range
|
||||
/// of hours (`[hour_start, hour_end)`) and can repeat the block weekly.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct BookingCreateForm {
|
||||
pub court_id: i32,
|
||||
pub date: String,
|
||||
pub hour_start: i32,
|
||||
pub hour_end: i32,
|
||||
pub repeat_weeks: Option<i32>,
|
||||
pub color: String,
|
||||
pub name: String,
|
||||
pub title: Option<String>,
|
||||
pub contact: Option<String>,
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
fn parse_date(s: &str) -> Result<chrono::NaiveDate> {
|
||||
chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d")
|
||||
.map_err(|_| Error::string("invalid date"))
|
||||
}
|
||||
|
||||
/// Creates one or more bookings from the admin form. The selected hour range
|
||||
/// expands into one row per hour, and — when `repeat_weeks > 1` — the whole
|
||||
/// block is duplicated on the same weekday for each following week. A slot
|
||||
/// that is already taken is skipped, so an overlap can never double-book.
|
||||
#[debug_handler]
|
||||
pub async fn booking_create(
|
||||
_auth: AdminAuth,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<BookingForm>,
|
||||
Form(form): Form<BookingCreateForm>,
|
||||
) -> Result<Response> {
|
||||
let date = parse_date(&form.date)?;
|
||||
bookings::ActiveModel {
|
||||
court_id: Set(form.court_id),
|
||||
date: Set(date),
|
||||
hour: Set(form.hour),
|
||||
color: Set(form.color),
|
||||
name: Set(form.name),
|
||||
title: Set(form.title.filter(|s| !s.is_empty())),
|
||||
contact: Set(form.contact.filter(|s| !s.is_empty())),
|
||||
note: Set(form.note.filter(|s| !s.is_empty())),
|
||||
..Default::default()
|
||||
let start_date = parse_date(&form.date)?;
|
||||
|
||||
// Normalise the hour block to `[hour_start, hour_end)`; a non-positive
|
||||
// span falls back to a single hour so the form cannot create nothing.
|
||||
let hour_start = form.hour_start.clamp(FIRST_HOUR, LAST_HOUR);
|
||||
let hour_end = form.hour_end.clamp(hour_start + 1, LAST_HOUR + 1);
|
||||
let weeks = form.repeat_weeks.unwrap_or(1).clamp(1, 52);
|
||||
|
||||
let title = form.title.filter(|s| !s.is_empty());
|
||||
let contact = form.contact.filter(|s| !s.is_empty());
|
||||
let note = form.note.filter(|s| !s.is_empty());
|
||||
|
||||
let last_date = start_date + chrono::Duration::weeks(i64::from(weeks - 1));
|
||||
|
||||
// Slots already booked on this court within the affected window, used to
|
||||
// skip conflicts rather than insert a duplicate.
|
||||
let taken: std::collections::HashSet<(chrono::NaiveDate, i32)> =
|
||||
bookings::Entity::find()
|
||||
.filter(bookings::Column::CourtId.eq(form.court_id))
|
||||
.filter(bookings::Column::Date.gte(start_date))
|
||||
.filter(bookings::Column::Date.lte(last_date))
|
||||
.all(&ctx.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|b| (b.date, b.hour))
|
||||
.collect();
|
||||
|
||||
for w in 0..weeks {
|
||||
let date = start_date + chrono::Duration::weeks(i64::from(w));
|
||||
for hour in hour_start..hour_end {
|
||||
if taken.contains(&(date, hour)) {
|
||||
continue;
|
||||
}
|
||||
bookings::ActiveModel {
|
||||
court_id: Set(form.court_id),
|
||||
date: Set(date),
|
||||
hour: Set(hour),
|
||||
color: Set(form.color.clone()),
|
||||
name: Set(form.name.clone()),
|
||||
title: Set(title.clone()),
|
||||
contact: Set(contact.clone()),
|
||||
note: Set(note.clone()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to(&format!("/admin?court={}&week={}", form.court_id, form.date)).into_response())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user