public void AreDatesValid_False() { // Arrange var startDate = DateTime.Now; var endDate = DateTime.Now.AddDays(3); // Act var areDatesValid = HelperMethods.AreDatesValid(endDate, startDate); // Assert Assert.False(areDatesValid); }
public async Task <IList <Spot> > GetAvailableSpots(int marinaId, int boatId, DateTime startDate, DateTime endDate) { IList <Spot> availableSpots = new List <Spot>(); var marina = await _marinaService.GetSingle(marinaId); var boat = await _context.Boats.FindAsync(boatId); // Dates are valid if endDate is later, or on the same day, as startDate and if they are // today or later if (HelperMethods.AreDatesValid(startDate, endDate)) { foreach (Spot spot in marina.Spots) { if (spot.Available) { if (HelperMethods.DoesSpotFitBoat(boat, spot)) { // Only go through Booking Lines that end later than "Now" - does not go // through past bookings var booked = false; foreach (BookingLine bookingLine in spot.BookingLines.Where <BookingLine>(bL => bL.EndDate > DateTime.Now)) { if (HelperMethods.AreDatesIntersecting(bookingLine.StartDate, bookingLine.EndDate, startDate, endDate)) { // Basically returns all spots that // 1. Fit the boat // 2. Have NO date intersects with any existing bookings with no // optimizations in mind whatsoever 🙂 booked = true; break; } } if (!booked) { availableSpots.Add(spot); } } } } } return(availableSpots); }
public async Task <ActionResult <Booking> > CreateBookingLocally(int boatId, int spotId, string start, string end) { var startDate = DateTime.Parse(start); var endDate = DateTime.Parse(end); // Find boat & spot objects in db var boat = await _boatService.GetSingle(boatId); var spot = await _spotService.GetSingle(spotId); // Check whether the logged user owns the boat var isAuthorized = await _authorizationService.AuthorizeAsync(User, boat, Operation.Book); if (!isAuthorized.Succeeded) { return(Unauthorized()); } // get booking from session if created before var booking = HttpContext.Session.Get <Booking>("Booking"); // Check whether booking is consistent, and if not, reinitialize if (booking is null || booking.BookingReferenceNo == 0 || booking.BoatId != boatId) { booking = new Booking { BoatId = boatId }; await _bookingService.Create(booking); } // If the spot fits the boat if (HelperMethods.DoesSpotFitBoat(boat, spot)) { // And the selected dates are valid if (HelperMethods.AreDatesValid(startDate, endDate)) { // Next 5 lines make sure that no dates overlap in the // booking's booking lines You cannot physically be in two // places at the same time bool areBookingLinesDatesValid = true; foreach (BookingLine bookingLine in booking.BookingLines) { if (HelperMethods.AreDatesIntersecting(bookingLine.StartDate, bookingLine.EndDate, startDate, endDate)) { areBookingLinesDatesValid = false; } } // Finally, if all conditions are met if (areBookingLinesDatesValid) { // Add bookingLine to the booking lines inside the booking booking = _bookingService.CreateBookingLine(booking, startDate, endDate, spot); } } } // store booking object in the session // don't yet know whether you rewrite value if you add it with the same key or if it needs to be removed first //HttpContext.Session.Remove("Booking"); HttpContext.Session.Set("Booking", booking); // hopefully serialization is not needed and returns booking in json format return(Ok(booking)); }