public ActionResult Purchase(int id) { var repo = CarMasterRepoFactory.Create(); var model = new PurchaseViewModel(); model.Vehicle = repo.GetSingleVehicle(id); model.SetPurchaseTypes(repo.GetAllPurchaseTypes()); model.SetStates(repo.GetAllStates()); return(View(model)); }
public ActionResult Purchase(PurchaseViewModel model) { var LookupValueRepo = LookupValueFactory.GetRepository(); var ADORep = RepositoryFactory.GetRepository(); //Apply Rule purchase price must be no less than 95% of Sale price decimal?salePrice = model.VehicleDetail.SalePrice; if (model.PurchasePrice < (salePrice * 0.95m)) { ModelState.AddModelError("PurchasePrice", "Purchase Price must be no less than 95% of sale price"); } decimal?MSRP = model.VehicleDetail.MSRP; //MSRP Rule. Purchase must not exceed MSRP if (model.PurchasePrice > MSRP) { ModelState.AddModelError("PurchasePrice", "Purchase Price must not exceed MSRP"); } if (!ModelState.IsValid) //Reload vehicle and set states and purchase types { model.SetPurchaseTypes(LookupValueRepo.GetPurchaseTypes()); model.SetStates(LookupValueRepo.GetStates()); model.VehicleDetail = ADORep.GetVehicleDetail(model.VehicleDetail.VehicleId); return(View(model)); } //Set sales model Sales sales = new Sales(); sales.CustomerName = model.CustomerName; sales.Phone = model.Phone; sales.Street1 = model.Street1; sales.Street2 = model.Street2; sales.City = model.City; sales.State = model.State; sales.ZipCode = model.ZipCode; sales.VehicleId = model.VehicleDetail.VehicleId; sales.PurchaseType = model.PurchaseType; sales.PurchasePrice = model.PurchasePrice; //Find id of logged in user var userName = User.Identity.GetUserName(); GuildCars.UI.Models.Identity.GuildCarsDbContext context = new GuildCars.UI.Models.Identity.GuildCarsDbContext(); var userId = context.Users.FirstOrDefault(u => u.UserName == userName).Id; sales.UserId = userId; ADORep.PurchaseVehicle(sales); //update/Make purchase return(View(model)); }
public ActionResult Purchase(int id) { var LookupValueRepo = LookupValueFactory.GetRepository(); var ADORep = RepositoryFactory.GetRepository(); PurchaseViewModel model = new PurchaseViewModel(); model.VehicleDetail = ADORep.GetVehicleDetail(id); model.SetPurchaseTypes(LookupValueRepo.GetPurchaseTypes()); model.SetStates(LookupValueRepo.GetStates()); return(View(model)); }
public ActionResult Purchase(PurchaseViewModel model) { var repo = CarMasterRepoFactory.Create(); var vehicle = repo.GetSingleVehicle(model.Vehicle.VehicleId); var user = repo.GetUserById(1); if (string.IsNullOrWhiteSpace(model.Purchase.Name)) { ModelState.AddModelError("Purchase.Name", "Name is required."); } if (string.IsNullOrWhiteSpace(model.Purchase.Phone) && string.IsNullOrWhiteSpace(model.Purchase.Email)) { ModelState.AddModelError("Purchase.Phone", "Phone or email required."); } if (string.IsNullOrWhiteSpace(model.Purchase.StreetAddress1)) { ModelState.AddModelError("Purchase.StreetAddress1", "Street Address 1 is required."); } if (string.IsNullOrWhiteSpace(model.Purchase.City)) { ModelState.AddModelError("Purchase.City", "City is required."); } if (model.Purchase.ZipCode.ToString().Length < 5 || model.Purchase.ZipCode.ToString().Length > 5) { ModelState.AddModelError("Purchase.ZipCode", "Zipcode must be 5 digits long."); } if (model.Purchase.PurchasePrice < vehicle.SalePrice * 95 / 100) { ModelState.AddModelError("Purchase.PurchasePrice", "Purchase price cannot be less than 95% of the sales price."); } if (model.Purchase.PurchasePrice > vehicle.MSRP) { ModelState.AddModelError("Purchase.PurchasePrice", "Purchase price may not exceed MSRP"); } if (ModelState.IsValid) { Purchase purchase = new Purchase(); purchase.Name = model.Purchase.Name; purchase.Phone = model.Purchase.Phone; purchase.Email = model.Purchase.Email; purchase.StreetAddress1 = model.Purchase.StreetAddress1; purchase.StreetAddress2 = model.Purchase.StreetAddress2; purchase.City = model.Purchase.City; purchase.ZipCode = model.Purchase.ZipCode; purchase.PurchasePrice = model.Purchase.PurchasePrice; purchase.DatePurchased = DateTime.Now; purchase.PurchaseTypeId = model.Purchase.PurchaseTypeId; purchase.StateId = model.Purchase.StateId; purchase.Vehicle = vehicle; purchase.VehicleId = model.Vehicle.VehicleId; purchase.UserId = user.UserId; purchase.Vehicle.SoldOut = true; purchase.Vehicle.Featured = false; //model.Purchase.Vehicle.SoldOut = true; //model.Purchase.Vehicle.Featured = false; vehicle.SoldOut = true; vehicle.Featured = false; repo.UpdateVehicle(model.Vehicle); repo.AddPurchase(purchase); return(RedirectToAction("Index")); } else { //var repo = CarMasterRepoFactory.Create(); var newModel = new PurchaseViewModel(); newModel.Vehicle = repo.GetSingleVehicle(model.Vehicle.VehicleId); newModel.SetPurchaseTypes(repo.GetAllPurchaseTypes()); newModel.SetStates(repo.GetAllStates()); return(View(newModel)); } }