public ActionResult Purchase(PurchaseVM model) { var listingRepo = VehicleListingsFactory.GetRepository(); var purchaseRepo = PurchasesFactory.GetRepository(); var customerRepo = CustomersFactory.GetRepository(); if (ModelState.IsValid) { try { var customer = customerRepo.GetByEmail(model.Buyer.Email); if (customer == null) { customerRepo.Insert(model.Buyer); var customerId = customerRepo.GetByEmail(model.Buyer.Email).CustomerId; model.NewPurchase.CustomerId = customerId; } else { model.NewPurchase.CustomerId = customer.CustomerId; } var vehicle = listingRepo.GetById(model.VehicleDetails.VehicleListingId); vehicle.Sold = true; listingRepo.Update(vehicle); model.NewPurchase.VehicleListingId = model.VehicleDetails.VehicleListingId; var userId = User.Identity.GetUserId(); model.NewPurchase.UserId = userId; purchaseRepo.Insert(model.NewPurchase); return(RedirectToAction("Index")); } catch (Exception ex) { throw ex; } } else { var purchaseTypeRepo = PurchaseTypesFactory.GetRepository(); model.PurchaseTypes = purchaseTypeRepo.GetAll(); model.VehicleDetails = listingRepo.GetDetails(model.VehicleDetails.VehicleListingId); return(View("Purchase", model)); } }
public ActionResult AjaxResult(int?Page, string type) { const int pageSize = 3; var collection = CustomersFactory.GetAll(); int rowCount = collection.Count(); int lastPage = rowCount / pageSize; ViewBag.PageCount = lastPage; int noOfTake = Page != null ? Page.Value : 0; if (type == "Next") { if (noOfTake < lastPage) { noOfTake = noOfTake + 1; } } else if (type == "Previous") { noOfTake = noOfTake - 1 <= 0 ? 0 : noOfTake - 1; } else if (type == "First") { noOfTake = 0; } else if (type == "Last") { noOfTake = lastPage; } else { noOfTake = 0; } ViewBag.CurrentPage = noOfTake; collection = collection.OrderBy(c => c.CustomerId).Skip(noOfTake * pageSize).Take(pageSize).ToList(); return(Json(new { data = collection })); }