public ActionResult Index() { if (Session["UserId"] == null) return RedirectToAction("LogIn", "Account"); var model = new LogViewModel {CustomerId = (int) Session["UserId"]}; IEnumerable<IOrder> orders; IEnumerable<IProduct> products; IEnumerable<IProductsCustomers> productsCustomers; IEnumerable<ICustomer> cutomers; GetAllDataFromDb(out orders, out products, out productsCustomers, out cutomers); if (orders == null || products == null || productsCustomers == null) return View(model); IEnumerable<IProduct> myProducts; List<IOrder> mySoldOrders, myBoughtOrders; List<IProduct> soldProducts, boughtProducts; List<ICustomer> soldCustomers, boughtCustomers; CreateInfoLists(products, productsCustomers, orders, cutomers, out myProducts, out mySoldOrders, out myBoughtOrders, out soldProducts, out soldCustomers, out boughtProducts, out boughtCustomers); FillModel(model, mySoldOrders, soldCustomers, soldProducts, boughtProducts, boughtCustomers, myBoughtOrders, productsCustomers, myProducts); Session.Add("Log", model); return View(model); }
private void FillModel(LogViewModel model, List<IOrder> mySoldOrders, List<ICustomer> soldCustomers, List<IProduct> soldProducts, List<IProduct> boughtProducts, List<ICustomer> boughtCustomers, List<IOrder> myBoughtOrders, IEnumerable<IProductsCustomers> productsCustomers, IEnumerable<IProduct> myProducts) { model.ItemsSold = (from order in mySoldOrders join customer in soldCustomers on order.CustomerId equals customer.Id join product in soldProducts on order.ProductId equals product.Id select new LogItem { ProductId = product.Id, ProductName = product.Name, ProductImage = product.Image, CustomerId = customer.Id, CustomerName = customer.UserName, Count = order.Count, OrderDate = order.OrderDateTime }).Distinct().OrderByDescending(x => x.OrderDate).ToList(); model.ItemsBought = (from product in boughtProducts from customer in boughtCustomers from order in myBoughtOrders from productCustomer in productsCustomers where ((order.ProductId == productCustomer.ProductId) && (customer.Id == productCustomer.CustomerId) && (order.ProductId == product.Id)) orderby order.OrderDateTime descending select new LogItem { ProductId = product.Id, ProductName = product.Name, ProductImage = product.Image, CustomerId = customer.Id, CustomerName = customer.UserName, Count = order.Count, OrderDate = order.OrderDateTime, IsMine = myProducts.FirstOrDefault(x => x.Id == product.Id) != null }).Distinct().ToList(); var approximateAmount = soldProducts.Sum(x => Convert.ToInt32(x.Cost)); model.ApproximateAmount = approximateAmount; model.CustomerId = (int) Session["UserId"]; }