public ActionResult NewBooking(Vehicle vehicle, Employee employee) { var viewModel = new BookVehicleViewModel { Employees = _dbcontext.Employees.ToList(), Vehicles = _dbcontext.Vehicles.ToList(), Vehicle = vehicle, Employee = employee }; return(View(viewModel)); }
public bool ValidateBooking(Object bookingViewModel) { bool valid = true; if (bookingViewModel is BookVehicleViewModel) { BookVehicleViewModel bookVehicleViewModel = (BookVehicleViewModel)bookingViewModel; double dateRangeInDays = (bookVehicleViewModel.StartDate.Date - bookVehicleViewModel.EndDate.Date).TotalDays; if (bookVehicleViewModel.StartDate.Date < DateTime.Today.Date) { ModelState.AddModelError("", "Booking cannot begin in the past."); valid = false; } if (dateRangeInDays > 14) { ModelState.AddModelError("", "Booking cannot be for longer than 2 weeks."); valid = false; } if (dateRangeInDays == 0 && bookVehicleViewModel.EndHalfDay != true) { ModelState.AddModelError("", "Minimum booking length is half a day."); valid = false; } } else if (bookingViewModel is EditBookingViewModel) { EditBookingViewModel editBookingViewModel = (EditBookingViewModel)bookingViewModel; double dateRangeInDays = (editBookingViewModel.StartDate.Date - editBookingViewModel.EndDate.Date).TotalDays; if (dateRangeInDays > 14) { ModelState.AddModelError("", "Booking cannot be for longer than 2 weeks."); valid = false; } if (dateRangeInDays == 0 && editBookingViewModel.EndHalfDay != true) { ModelState.AddModelError("", "Minimum booking length is half a day."); valid = false; } return(valid); } return(valid); }
public Booking ConvertViewModelToBooking(Object bookingViewModel) { if (bookingViewModel is BookVehicleViewModel) { BookVehicleViewModel bookVehicleViewModel = (BookVehicleViewModel)bookingViewModel; Booking booking = new Booking(); ApplicationUser userBooking = new ApplicationUser(); Vehicle chosenVehicle = new Vehicle(); userBooking.Id = bookVehicleViewModel.UserId; chosenVehicle.Id = bookVehicleViewModel.ChosenVehicleId; booking.StartDate = bookVehicleViewModel.StartDate; booking.EndDate = bookVehicleViewModel.EndDate; booking.StartHalfDay = bookVehicleViewModel.StartHalfDay; booking.EndHalfDay = bookVehicleViewModel.EndHalfDay; booking.Price = bookVehicleViewModel.TotalCost; booking.Vehicle = chosenVehicle; booking.User = userBooking; return(booking); } else if (bookingViewModel is EditBookingViewModel) { EditBookingViewModel editBookingViewModel = (EditBookingViewModel)bookingViewModel; Booking booking = new Booking(); booking.Id = editBookingViewModel.BookingId; booking.StartDate = editBookingViewModel.StartDate; booking.EndDate = editBookingViewModel.EndDate; booking.StartHalfDay = editBookingViewModel.StartHalfDay; booking.EndHalfDay = editBookingViewModel.EndHalfDay; booking.Price = editBookingViewModel.Price; booking.Collected = editBookingViewModel.Collected; return(booking); } return(null); }
public async Task <IActionResult> BookVehicle(int?id) { IActionResult authenticationResult = await AuthenticateUserLogin(false); if (authenticationResult != null) { return(authenticationResult); } ApplicationUser CurrentUser = await _userManager.GetUserAsync(HttpContext.User); if (CurrentUser.Blacklisted == true) { TempData["errormessage"] = "You are unable to book a vehicle due to a prior failure to collect a booked vehicle. Please contact us directly if you wish you rectify this issue."; return(RedirectToAction("Index", "Home")); } BookVehicleViewModel = new BookVehicleViewModel(); EquipmentCheckboxSelect = new List <EquipmentCheckboxSelectViewModel>(); BookVehicleViewModel.UserId = CurrentUser.Id; if (id > 0) { VehicleId = (int)id; ViewData["ChosenVehicle"] = await GetChosenVehicle(VehicleId); List <Equipment> equipmentList = (List <Equipment>) await CommandFactory.CreateCommand(CommandFactory.GET_ALL_EQUIPMENT, DbContext).Execute(); BookVehicleViewModel.ChosenEquipmentList = CreateEquipmentCheckboxSelectList(equipmentList); return(View(BookVehicleViewModel)); } else { TempData["errormessage"] = "Something went wrong! If this error persists, please contact us directly."; return(View(BookVehicleViewModel)); } }
public async Task <IActionResult> BookVehicle(BookVehicleViewModel bookVehicleViewModel) { IActionResult authenticationResult = await AuthenticateUserLogin(false); if (authenticationResult != null) { return(authenticationResult); } bool bookingValid = ValidateBooking(bookVehicleViewModel); if (bookingValid) { List <Equipment> chosenEquipmentList = new List <Equipment>(); List <Booking> coincidingBookings = (List <Booking>) await CommandFactory.CreateCommand(CommandFactory.CHECK_FOR_COINCIDING_BOOKINGS, bookVehicleViewModel.StartDate, bookVehicleViewModel.EndDate, DbContext).Execute(); if (coincidingBookings.Count > 0) { foreach (Booking coincidingBooking in coincidingBookings) { if (bookVehicleViewModel.ChosenVehicleId == coincidingBooking.Vehicle.Id) { ViewData["ChosenVehicle"] = await GetChosenVehicle(bookVehicleViewModel.ChosenVehicleId); ModelState.AddModelError("", "The vehicle is not available for the period of your booking. It is booked between " + coincidingBooking.StartDate.AddDays(-1).ToString("dd/MM/yyyy") + " and " + coincidingBooking.EndDate.AddDays(-1).ToString("dd/MM/yyyy")); return(View(bookVehicleViewModel)); } } foreach (EquipmentCheckboxSelectViewModel equipmentCheckbox in bookVehicleViewModel.ChosenEquipmentList) { if (equipmentCheckbox.CheckboxAnswer == true) { Equipment tempEquipment = new Equipment(); tempEquipment.Id = equipmentCheckbox.Id; tempEquipment.Name = equipmentCheckbox.Name; bool equipmentAvailable = (bool)await CommandFactory.CreateCommand(CommandFactory.CHECK_EQUIPMENT_AVAILABILITY, coincidingBookings, tempEquipment, DbContext).Execute(); if (equipmentAvailable == true) { chosenEquipmentList.Add(tempEquipment); } else { ViewData["ChosenVehicle"] = await GetChosenVehicle(bookVehicleViewModel.ChosenVehicleId); errorString = equipmentCheckbox.Name + " is not available for the period of your booking. Please select a different booking period, or continue without this equipment item."; ModelState.AddModelError("", errorString); return(View(bookVehicleViewModel)); } } } } Booking newBooking = ConvertViewModelToBooking(bookVehicleViewModel); int bookingAdded = (int)await CommandFactory.CreateCommand(CommandFactory.ADD_BOOKING, newBooking, chosenEquipmentList, DbContext).Execute(); if (bookingAdded >= 1) { TempData["successmessage"] = "Booking successfully created!"; return(RedirectToAction("Index", "Home")); } else { ViewData["ChosenVehicle"] = await GetChosenVehicle(VehicleId); errorString = "Something went wrong, please try again."; ModelState.AddModelError("", errorString); return(View(bookVehicleViewModel)); } } ViewData["ChosenVehicle"] = await GetChosenVehicle(VehicleId); return(View(bookVehicleViewModel)); }