book method
Books the given slot
for the current user at the given date
.
Implementation
Future<void> book({required Slot slot, required DateTime date}) async {
if (!state.hasData) {
log('Cannot book slot ${slot.id} for current user: No data');
return;
}
if (slot.reserved) {
log('Cannot book slot ${slot.id} for current user: Already reserved');
return;
}
if (slot.reservations >= slot.size) {
log('Cannot book slot ${slot.id} for current user: No more slots available');
return;
}
log('Reserving slot ${slot.id} for current user');
try {
data(
state.requireData.map(
(s) {
if (s.id == slot.id) {
return s.copyWith(
reservations: s.reservations + 1,
reserved: true,
);
}
return s;
},
).toList(),
);
await _datasource.reserveSlot(
token: _auth.state.requireData.pick(Webservice.lb_planner_api),
slotId: slot.id,
date: date,
);
await captureEvent(
'slot_reserved',
properties: {
'reservation_date': DateTime.now().toIso8601String(),
'date': date.toIso8601String(),
},
);
log('Reserved slot ${slot.id} for current user');
await build(this);
} catch (e) {
log('Failed to reserve slot: $e');
}
}