Skip to content

Instantly share code, notes, and snippets.

@Theoistic
Last active August 26, 2018 21:36
Show Gist options
  • Save Theoistic/cc4587db4ae9684ff3943749350e052a to your computer and use it in GitHub Desktop.
Save Theoistic/cc4587db4ae9684ff3943749350e052a to your computer and use it in GitHub Desktop.
Booking Sketch
public static List<TimeSlot> GetAvailableTimeSlots(this ServiceEntry service, DateTime start, DateTime end, TimeSpan duration, Func<ResourceEntry, bool> resourcePredicate)
{
var Unavailable = service.Bookings.Where(x => resourcePredicate(x.Resource)).Select(y => y.Period);
List<TimeSlot> OriginalAvail = new List<TimeSlot>();
DateTime setTime = start;
while (setTime < end)
{
if ((setTime.Hour < start.Hour) || (setTime.Hour > end.Hour))
{
setTime = setTime.Add(duration);
continue;
}
TimeSlot foundEntry = Unavailable.FirstOrDefault(x => x.Start <= setTime && x.End >= (setTime.Add(duration)));
OriginalAvail.Add(new TimeSlot
{
Start = setTime,
End = setTime.Add(duration),
IsFree = foundEntry == null,
Description = foundEntry != null ? foundEntry.Description : "",
});
setTime = setTime.Add(duration);
}
return OriginalAvail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment