コード例 #1
0
        public async Task <IActionResult> CustHome()
        {
            //get cur user
            var u = await mUserManager.GetUserAsync(HttpContext.User);

            //get all order the user has made
            IEnumerable <Order> orders = context.Order.Where(o => o.CustomerUsername == u.Id);

            if (orders.Count() != 0)
            {
                //for simplicity get the first order
                int first_order_num            = orders.First().OrderNumber;
                IEnumerable <ProductOrder> pos = context.ProductOrder.Where(p => p.OrderNumber == first_order_num);

                List <Product> products      = new List <Product>();
                List <string>  product_names = new List <string>();
                foreach (var po in pos)
                {
                    //for every product order in the product orders, we want to get the corresponding product.
                    //it's okay to use first beceause the product ID is unique, so there will only ever be one product.
                    products.Add(context.Product.Where(p => p.ProductID == po.ProductID).First());
                    product_names.Add(context.Product.Where(p => p.ProductID == po.ProductID).First().ProductName);
                }
                //string s=products.First().ProductName;
                var products_string    = string.Join(", ", products);
                Models.productModel pm = new Models.productModel
                {
                    cust          = u,
                    products      = products,
                    productorders = pos
                };
                //Here is the order number;
                // return Content(products_string, "text/html");

                return(View(pm));
            }
            else
            {
                return(Content("you don't have any orders", "text/html"));
            }
        }
コード例 #2
0
        public IActionResult AdminHome()
        {
            Console.Write("hi");
            //get cur user
            IEnumerable <User> UserList = context.Users;

            Console.Write("users", UserList);
            List <Models.productModel> pmList = new List <Models.productModel>();

            foreach (User u in UserList)
            {
                IEnumerable <Order> ordersList = context.Order.Where(o => o.CustomerUsername == u.Id);

                //if user u has no orders, we are basically done.
                if (ordersList.Count() == 0)
                {
                    List <Product> pro = new List <Product>();
                    pro.Add(new Product {
                        ProductName = "no products coz no order",
                        ProductID   = "null",
                        Price       = 1000
                    });

                    List <ProductOrder> pos = new List <ProductOrder>();
                    pos.Add(new ProductOrder
                    {
                        ProductID   = "no orders",
                        Product     = new Product(),
                        Order       = new Order(),
                        OrderNumber = -1
                    });
                    Models.productModel p = new Models.productModel
                    {
                        cust          = u,
                        products      = pro,
                        productorders = pos
                    };
                    pmList.Add(p);
                }
                else //user has made an order
                {
                    IEnumerable <Order> orders = context.Order.Where(o => o.CustomerUsername == u.Id);

                    //for simplicity get the first order
                    int first_order_num            = orders.First().OrderNumber;
                    IEnumerable <ProductOrder> pos = context.ProductOrder.Where(p => p.OrderNumber == first_order_num);

                    List <Product> products      = new List <Product>();
                    List <string>  product_names = new List <string>();
                    foreach (var po in pos)
                    {
                        //for every product order in the product orders, we want to get the corresponding product.
                        //it's okay to use first beceause the product ID is unique, so there will only ever be one product.
                        products.Add(context.Product.Where(p => p.ProductID == po.ProductID).First());
                        product_names.Add(context.Product.Where(p => p.ProductID == po.ProductID).First().ProductName);
                    }
                    //string s=products.First().ProductName;
                    var products_string    = string.Join(", ", products);
                    Models.productModel pm = new Models.productModel
                    {
                        cust          = u,
                        products      = products,
                        productorders = pos
                    };
                    pmList.Add(pm);
                }
            }

            return(View(pmList));
        }