private void CheckIfTripIsComplete(int tripId) { bool complete = true; var trip = _repo.GetTripByID(tripId); DateTime start = trip.StartDate; var legs = _repo.GetLegsByTripID(tripId).OrderBy(l => l.StartDate).ToList(); while (start < trip.EndDate && legs.Count > 0) { Leg l = legs.First(); if (start == l.StartDate) { start = l.EndDate.AddDays(1); legs.Remove(l); if (legs.Count == 0) { if (start == trip.EndDate.AddDays(1)) { break; } else { complete = false; break; } } } else { complete = false; break; } } _repo.UpdateTripComplete(tripId, complete); }
private bool LegDontClash(Leg leg) { var legsOfTrip = _repo.GetLegsByTripID(leg.TripId).ToList(); //if trip does not have any legs it fits if (!legsOfTrip.Any()) { return(true); } //if new leg ends before the first starts, it fits if (leg.EndDate < legsOfTrip.First().StartDate) { return(true); } //if new leg starts after the last ends, it fits if (leg.StartDate > legsOfTrip.Last().EndDate) { return(true); } bool valid = true; bool verifyNext = false; while (valid) { foreach (var item in legsOfTrip) { //***method that test the explantion on the buttom**** //(continuation of the explination on the button) //so, if the new leg starts before the leg(item) seletec starts //verify if the new leg ends before the next leg starts if (verifyNext) { if (leg.StartDate < item.StartDate) { if (leg.EndDate > item.StartDate) { valid = false; break; } else { verifyNext = false; } } else { verifyNext = false; } } //if new leg starts or ends on the same day of another legs, it fails if (leg.StartDate.Equals(item.StartDate)) { valid = false; break; } if (leg.EndDate.Equals(item.EndDate)) { valid = false; break; } //if new leg dates are in the midle of another existing leg, it fails if (leg.StartDate > item.StartDate && leg.EndDate < item.EndDate) { valid = false; break; } //if new leg starts after this(item) leg ends and //this leg(item) is not the last, need to verify if new leg is //between this leg(item) and the next leg(item), //****method on the top of forearch loop**** if (leg.StartDate > item.EndDate) { if (item != legsOfTrip.Last()) { verifyNext = true; } } if (item == legsOfTrip.Last()) { return(true); } } } return(valid); }