public async Task<ActionResult> Index(string year = "", string make = "", string model = "", string style = "") { HttpContext ctx = System.Web.HttpContext.Current; Settings settings = ViewBag.settings; int cust_id = 0; try { cust_id = Convert.ToInt32(settings.Get("CURTAccount")); } catch (Exception) { } style = style.Replace('!', '/'); var pcats = CURTAPI.GetParentCategoriesAsync(); var vtask = CURTAPI.getVehicleAsync(year, make, model, style); var parttask = CURTAPI.GetVehiclePartsAsync(year.Trim(), make.Trim(), model.Trim(), style.Trim(), cust_id); await Task.WhenAll(new Task[] { pcats, vtask, parttask }); ViewBag.parent_cats = await pcats; FullVehicle vehicle = await vtask; UDF.SetCookies(ctx, year, make, model, style, vehicle.vehicleID); ViewBag.year = year; ViewBag.make = make; ViewBag.model = model; ViewBag.style = style; ViewBag.vehicleID = vehicle.vehicleID; // Get all the parts that match our search criteria List<APIPart> parts = await parttask; // Get the unique classes of the returned parts IEnumerable<IGrouping<string,APIPart>> distinct = parts.GroupBy(x => x.pClass.Trim()); // Loop through the unique groups and get the color codes for each pClass foreach (IGrouping<string,APIPart> d in distinct) { APIPart p = d.FirstOrDefault<APIPart>(); // Get the color code from the API APIColorCode color_code = CURTAPI.GetColorCode(p.partID); // Set up default code if something went wrong if (color_code == null || color_code.code == null) { color_code = new APIColorCode(); color_code.code = "ffffff"; } // Get the parts that match this pClass and bind our colorCode to them List<APIPart> codedParts = parts.Where(x => x.pClass.Equals(p.pClass)).ToList<APIPart>(); foreach (APIPart codedPart in codedParts) { codedPart.colorCode = color_code.code; } } int part_count = parts.Count; Dictionary<string, List<APIPart>> ordered_parts = new Dictionary<string, List<APIPart>>(); foreach (APIPart part in parts) { if (part.pClass.Length > 0) { if (ordered_parts.Keys.Contains(part.pClass)) { // Already added to dictionary List<APIPart> existing_parts = ordered_parts.Where(x => x.Key == part.pClass).Select(x => x.Value).FirstOrDefault<List<APIPart>>(); existing_parts.Add(part); ordered_parts[part.pClass] = existing_parts; } else { // New Color Code List<APIPart> new_parts = new List<APIPart>(); new_parts.Add(part); ordered_parts.Add(part.pClass, new_parts); } } else { if (ordered_parts.Keys.Contains("Wiring")) { // Already added to dictionary List<APIPart> existing_parts = ordered_parts.Where(x => x.Key == "Wiring").Select(x => x.Value).FirstOrDefault<List<APIPart>>(); existing_parts.Add(part); ordered_parts["Wiring"] = existing_parts; } else { // New Color Code List<APIPart> new_parts = new List<APIPart>(); new_parts.Add(part); ordered_parts.Add("Wiring", new_parts); } } } ViewBag.parts = ordered_parts; ViewBag.partCount = part_count; ViewBag.year = year; ViewBag.make = make; ViewBag.model = model; ViewBag.style = style; return View(); } // End Index
public ActionResult Index(string year = "", string make = "", string model = "", string style = "") { Settings settings = ViewBag.settings; style = style.Replace('!', '/'); Session["year"] = year.Trim(); Session["make"] = make.Trim(); Session["model"] = model.Trim(); Session["style"] = style.Trim(); Session.Timeout = 30; int cust_id = 0; try { cust_id = Convert.ToInt32(settings.Get("CURTAccount")); } catch (Exception) { } // Get all the parts that match our search criteria List<APIPart> parts = CURTAPI.GetVehicleParts(year.Trim(), make.Trim(), model.Trim(), style.Trim(), cust_id); // Get the unique classes of the returned parts IEnumerable<IGrouping<string,APIPart>> distinct = parts.GroupBy(x => x.pClass.Trim()); // Loop through the unique groups and get the color codes for each pClass foreach (IGrouping<string,APIPart> d in distinct) { APIPart p = d.FirstOrDefault<APIPart>(); // Get the color code from the API APIColorCode color_code = CURTAPI.GetColorCode(p.partID); // Set up default code if something went wrong if (color_code == null || color_code.code == null) { color_code = new APIColorCode(); color_code.code = "ffffff"; } // Get the parts that match this pClass and bind our colorCode to them List<APIPart> codedParts = parts.Where(x => x.pClass.Equals(p.pClass)).ToList<APIPart>(); foreach (APIPart codedPart in codedParts) { codedPart.colorCode = color_code.code; } } int part_count = parts.Count; Dictionary<string, List<APIPart>> ordered_parts = new Dictionary<string, List<APIPart>>(); foreach (APIPart part in parts) { if (part.pClass.Length > 0) { if (ordered_parts.Keys.Contains(part.pClass)) { // Already added to dictionary List<APIPart> existing_parts = ordered_parts.Where(x => x.Key == part.pClass).Select(x => x.Value).FirstOrDefault<List<APIPart>>(); existing_parts.Add(part); ordered_parts[part.pClass] = existing_parts; } else { // New Color Code List<APIPart> new_parts = new List<APIPart>(); new_parts.Add(part); ordered_parts.Add(part.pClass, new_parts); } } else { if (ordered_parts.Keys.Contains("Wiring")) { // Already added to dictionary List<APIPart> existing_parts = ordered_parts.Where(x => x.Key == "Wiring").Select(x => x.Value).FirstOrDefault<List<APIPart>>(); existing_parts.Add(part); ordered_parts["Wiring"] = existing_parts; } else { // New Color Code List<APIPart> new_parts = new List<APIPart>(); new_parts.Add(part); ordered_parts.Add("Wiring", new_parts); } } } ViewBag.parts = ordered_parts; ViewBag.partCount = part_count; ViewBag.year = year; ViewBag.make = make; ViewBag.model = model; ViewBag.style = style; return View(); }
public async Task<ActionResult> Index(int catID = 0, int page = 1, int per_page = 10) { HttpContext ctx = System.Web.HttpContext.Current; APICategory category = new APICategory(); APIColorCode color_code = new APIColorCode(); List<APIPart> catparts = new List<APIPart>(); List<APIPart> moreparts = new List<APIPart>(); Task<List<APICategory>> pcats = CURTAPI.GetParentCategoriesAsync(); Task<List<APICategory>> crumbs = null; Task<APICategory> cat = null; Task<List<APIPart>> parts = null; Task<List<APIPart>> moreTask = null; Task<APIColorCode> codetask = null; List<APICategory> breadcrumbs = new List<APICategory>(); List<Task> tasks = new List<Task> { pcats }; if (catID > 0) { UDF.SetCategoryCookie(ctx, catID); cat = CURTAPI.GetCategoryAsync(catID); parts = CURTAPI.GetCategoryPartsAsync(catID, page, per_page); moreTask = CURTAPI.GetCategoryPartsAsync(catID, page + 1, per_page); codetask = CURTAPI.GetCategoryColorCodeAsync(catID); crumbs = CURTAPI.GetBreadcrumbsAsync(catID); tasks.Add(cat); tasks.Add(parts); tasks.Add(moreTask); tasks.Add(codetask); tasks.Add(crumbs); } else { category.catTitle = "Product Categories"; category.catID = 0; } await Task.WhenAll( tasks.ToArray() ); ViewBag.parent_cats = await pcats; if (catID > 0) { category = await cat; color_code = await codetask; } else { category.SubCategories = ViewBag.parent_cats; } ViewBag.category = category; if (crumbs != null) { breadcrumbs = await crumbs; } ViewBag.breadcrumbs = breadcrumbs; if (parts != null) { catparts = await parts; moreparts = await moreTask; if (catparts.Count > 0) { ViewBag.parts = catparts; int more_count = moreparts.Count; ViewBag.more_count = more_count; ViewBag.page = page; ViewBag.per_page = per_page; return View("Parts"); } } return View(); }