public void CanGetDatesBetweenCheckInAndCheckOut() { Accommodation acc1 = new Accommodation() { CheckIn = new DateTime(2018, 2, 3, 9, 0, 0), CheckOut = new DateTime(2018, 2, 5, 14, 0, 0) }; var dates = acc1.GetDatesBetweenInAndOut(); var expected = new List <DateTime>(); expected.Add(new DateTime(2018, 2, 3, 0, 0, 0)); expected.Add(new DateTime(2018, 2, 4, 0, 0, 0)); Assert.AreEqual(2, dates.Count); CollectionAssert.AreEqual(expected, dates); }
public static void AssignAccommodationToStep(Accommodation acc, Trip trip, bool insert) { //check that a step exists for each day covered by accommodation and assign accommodation id List <DateTime> dates = acc.GetDatesBetweenInAndOut(); var stepsInRange = trip.Steps.Where(s => s.Date.Date >= acc.CheckIn.Date && s.Date.Date < acc.CheckOut.Date); foreach (var date in dates) { Step step = stepsInRange.FirstOrDefault(s => s.Date.Date == date.Date); if (step == null) { throw new ArgumentException("There is no existing step for date " + date.ToShortDateString() + ". Please first create the step."); } //if new accommodation, check that there is no other accommodation already on step else if (insert && step.AccommodationId != null && step.AccommodationId != acc.AccommodationId) //to allow for updates { throw new ArgumentException("An accommodation already exists for Step " + step.SequenceNo); } step.AccommodationId = acc.AccommodationId; } }