admin features
This commit is contained in:
@@ -53,3 +53,7 @@ settings = Settings
|
||||
settings-language = Language
|
||||
settings-theme = Theme
|
||||
view-details = Details
|
||||
hour-from = From
|
||||
hour-to = Until
|
||||
repeat-weeks = Repeat for (weeks)
|
||||
repeat-hint = 1 books a single week. A higher number repeats the same hours every following week.
|
||||
|
||||
@@ -53,3 +53,7 @@ settings = Nastavenia
|
||||
settings-language = Jazyk
|
||||
settings-theme = Téma
|
||||
view-details = Detaily
|
||||
hour-from = Od
|
||||
hour-to = Do
|
||||
repeat-weeks = Opakovať (počet týždňov)
|
||||
repeat-hint = 1 rezervuje jeden týždeň. Vyššie číslo opakuje rovnaké hodiny každý ďalší týždeň.
|
||||
|
||||
@@ -24,6 +24,34 @@
|
||||
<label class="label"><span class="label-text">{{ t(key="date", lang=lang) }}</span></label>
|
||||
<input type="date" name="date" value="{{ date }}" required class="input input-bordered w-full">
|
||||
</div>
|
||||
{% if mode == "new" %}
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour-from", lang=lang) }}</span></label>
|
||||
<select name="hour_start" id="hour_start" class="select select-bordered w-full">
|
||||
{% for h in hours %}
|
||||
<option value="{{ h.v }}" {% if h.v == hour_start %}selected{% endif %}>{{ h.label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour-to", lang=lang) }}</span></label>
|
||||
<select name="hour_end" id="hour_end" class="select select-bordered w-full">
|
||||
{% for h in hours_end %}
|
||||
<option value="{{ h.v }}" {% if h.v == hour_end %}selected{% endif %}>{{ h.label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="repeat-weeks", lang=lang) }}</span></label>
|
||||
<input type="number" name="repeat_weeks" value="{{ repeat_weeks }}" min="1" max="52" required
|
||||
class="input input-bordered w-full">
|
||||
<label class="label">
|
||||
<span class="label-text-alt opacity-60">{{ t(key="repeat-hint", lang=lang) }}</span>
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="hour", lang=lang) }}</span></label>
|
||||
<select name="hour" class="select select-bordered w-full">
|
||||
@@ -32,6 +60,7 @@
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">{{ t(key="booking-color", lang=lang) }}</span></label>
|
||||
<input type="color" name="color" value="{{ color }}"
|
||||
@@ -69,3 +98,21 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
{% block js %}
|
||||
{% if mode == "new" %}
|
||||
<script>
|
||||
// Keep the "until" time after the "from" time as the start hour changes.
|
||||
(function () {
|
||||
var s = document.getElementById('hour_start');
|
||||
var e = document.getElementById('hour_end');
|
||||
if (!s || !e) return;
|
||||
s.addEventListener('change', function () {
|
||||
if (parseInt(e.value, 10) <= parseInt(s.value, 10)) {
|
||||
e.value = String(parseInt(s.value, 10) + 1);
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% endif %}
|
||||
{% endblock js %}
|
||||
|
||||
@@ -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