public ResourceIdViewModel Post(RentalBindingModel model) { var key = new ResourceIdViewModel { Id = _rentals.Keys.Count + 1 }; _rentals.Add(key.Id, new RentalViewModel { Id = key.Id, Units = model.Units }); return(key); }
public ResourceIdViewModel Post(BookingBindingModel model) { var key = new ResourceIdViewModel { Id = _bookings.Keys.Count + 1 }; _bookings.Add(key.Id, new BookingViewModel { Id = key.Id, Nights = model.Nights, RentalId = model.RentalId, Start = model.Start.Date, PreparationTimeInDays = _rentals[model.RentalId].PreparationTimeInDays }); return(key); }
public ResourceIdViewModel Post(BookingBindingModel model) { if (model.Nights <= 0) { throw new ApplicationException("Nigts must be positive"); } if (!_rentals.ContainsKey(model.RentalId)) { throw new ApplicationException("Rental not found"); } for (var i = 0; i < model.Nights; i++) { var count = 0; foreach (var booking in _bookings.Values) { if (booking.RentalId == model.RentalId && (booking.Start <= model.Start.Date && booking.Start.AddDays(booking.Nights) > model.Start.Date) || (booking.Start < model.Start.AddDays(model.Nights) && booking.Start.AddDays(booking.Nights) >= model.Start.AddDays(model.Nights)) || (booking.Start > model.Start && booking.Start.AddDays(booking.Nights) < model.Start.AddDays(model.Nights))) { count++; } } if (count >= _rentals[model.RentalId].Units) { throw new ApplicationException("Not available"); } } var key = new ResourceIdViewModel { Id = _bookings.Keys.Count + 1 }; _bookings.Add(key.Id, new BookingViewModel { Id = key.Id, Nights = model.Nights, RentalId = model.RentalId, Start = model.Start.Date }); return(key); }