public ActionResult Create(OrderViewModel model) { if (!ModelState.IsValid) { return(View(model)); } try { FleetCarsLogic carsLogic = new FleetCarsLogic(); // Holds the logic for the cars in the fleet. // Checks if the car is available in the specified rental dates: if (carsLogic.CheckCarAvailability(model.LicensePlate, model.StartDate, model.ReturnDate)) { // Car is available, so: // Inserts the rental to the database, and assigns the order number (ID): int id = logic.InsertRental(model.LicensePlate, model.StartDate, model.ReturnDate, User.Identity.Name); // After rental inserted, redirects to Order Receipt action, with the order number: return(RedirectToAction("OrderReceipt", new { id })); } else { // Car is not available: ViewBag.ErrorMessage = "Car is not available in the specified dates."; return(View(model)); } } catch (Exception) { ViewBag.ErrorMessage = "An error has occurred. please try again later."; return(View(model)); } }
/// <summary> /// Page displays: A form to create a new rental. /// </summary> public ActionResult Create(string licensePlate, DateTime startDate, DateTime returnDate) { try { // Gets the car object, according to the license plate: FleetCarsLogic carsLogic = new FleetCarsLogic(); FleetCar car = carsLogic.GetFleetCarByLicensePlate(licensePlate); // Checks if the car exists to order: if (car == null) { return(new HttpNotFoundResult("Car is not found.")); // If car not exists, returns error 404 not found. } // Car exists, so creates the order view model object: OrderViewModel model = new OrderViewModel(); model.LicensePlate = licensePlate; model.CarModelName = string.Format("{0} {1} {2} {3}", car.CarModel.ManufacturerModel.Manufacturer.ManufacturerName, car.CarModel.ManufacturerModel.ManufacturerModelName, car.CarModel.ProductionYear, car.CarModel.ManualGear ? "Manual" : "Automatic"); model.CarImage = car.CarImage; model.DailyPrice = car.CarModel.DailyPrice; model.DayDelayPrice = car.CarModel.DayDelayPrice; model.StartDate = startDate.Date; model.ReturnDate = returnDate.Date; return(View(model)); } catch (Exception) { ViewBag.ErrorMessage = "An error has occurred. please try again later."; return(View(new OrderViewModel())); } }