/// <summary> /// returns list of products for give manufacturer and customer where the product is active and has more than 2 options. /// TODO: Personally would have it ID based, as manufacturers and customers could have similar names. Probably would bring this up if requirements or design stated this to understand reasoning. /// </summary> /// <param name="manufacturerName">name of the manufacturer</param> /// <param name="customerName">name of customer</param> /// <returns>list of product dto for give manufacturer and customer where the product is active and has more than 2 options</returns> public async Task <List <DTO> > InvokeAsync(string manufacturerName, string customerName) { var tasks = new List <Task>(); //will be used to loop through and get prices var products = new List <DTO>(); try { using (var context = new CodingTestContext()) { products = await(from p in context.ProductTables from CustTable in p.ManufacturerTables.ManufacturerCustomerTable where CustTable.CustomerTable.Cust_Name == customerName && //for given customer p.ManufacturerTables.Name == manufacturerName && //for the given manufacturer p.Product_Active && //where product is active p.ProductOptionsTables.Count >= 2 //and there's more than 2 options select new DTO { ProductId = p.Product_ID, Description = p.Product_Description, ProductOptions = p.ProductOptionsTables.ToList(), ProductNumberCustomer = p.Product_Number_Custom, Price = p.Cost_Price //will be multiplied in next step. Maybe I could have made an anon type instead and a method to switch from anon to actual DTO. But at the same time that felt somewhat redundent? Certainly a case where a code review is appreciated. }).ToListAsync(); //populate the list of tasks to be run. foreach (var prod in products) { tasks.Add(Task.Run(async() => { await getPrice(prod); })); } tasks.Add(Task.Run(() => { products = products.OrderByDescending(dto => dto.Price).ToList(); })); //run tasks and return the final products (Now with pricing!) var t = Task.WhenAll(tasks); await t; } } catch (Exception) { throw; } return(products); }
public DataManager() { Context = Builder.Builder.BuildTestContext(); }