From 8c352ad045c60a9f318b4e0beea29ad7f89a07af Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Fri, 20 Dec 2024 10:46:16 +1100 Subject: [PATCH] fix(place/demo/lockers): locker_id optional --- drivers/place/demo/lockers.cr | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/place/demo/lockers.cr b/drivers/place/demo/lockers.cr index 47b6cbed4e..c918180d63 100644 --- a/drivers/place/demo/lockers.cr +++ b/drivers/place/demo/lockers.cr @@ -50,6 +50,24 @@ class Place::Demo::Lockers < PlaceOS::Driver @allocated_until = nil @shared_with = [] of String end + + def allocated? : Bool + if time = self.allocated_until + if time > Time.utc + true + else + false + end + elsif allocated_to = self.allocated_to + true + else + false + end + end + + def not_allocated? : Bool + !allocated? + end end class LockerBank @@ -124,10 +142,14 @@ class Place::Demo::Lockers < PlaceOS::Driver # attempts to create a booking that expires at the time specified expires_at : Int64? = nil ) : PlaceLocker - locker = locker_banks[bank_id.to_s].locker_hash[locker_id.to_s] + bank = locker_banks[bank_id.to_s] + locker_id = locker_id ? locker_id : bank.locker_hash.values.select(&.not_allocated?).sample.id + locker = bank.locker_hash[locker_id.to_s] locker.allocated_to = user_id locker.allocated_until = Time.unix(expires_at) if expires_at PlaceLocker.new(bank_id, locker, building_id) + rescue + raise "no available lockers" end # return the locker to the pool