// this method return list product need evaluated with coditions: customer was recevied the order

        public async Task <List <ProductListVM> > getListProductNeedRating()
        {
            // a query get list product when the order have status was recevied (include a result query productberated under)

            string userID = _repoUser.getUserID();

            var query = await(from od in _context.OrderDetails

                              join o in _context.Order on od.OrderId equals o.Id

                              join p in _context.Products on od.ProductId equals p.Id

                              join pm in _context.ProductImages on p.Id equals pm.ProductID

                              // status is 2 is means customer was recevied the order

                              where (o.Status == 2 && pm.IsDefault == true && o.UserId.Equals(userID))

                              select new ProductListVM
            {
                ProductId = p.Id,

                ProductName = p.ProductName,

                ImgDefault = _config["Host"] + pm.PathName,
            }).ToListAsync();


            // query  get list product be rated by cusomter before

            var productberated = await(
                from p in _context.Products

                join pm in _context.ProductImages on p.Id equals pm.ProductID

                join pr in _context.RattingProduct on p.Id equals pr.ProductId

                where (pm.IsDefault == true && pr.UserId.Equals(userID))

                select new ProductListVM
            {
                ProductId = p.Id,

                ProductName = p.ProductName,

                ImgDefault = _config["Host"] + pm.PathName,
            }).ToListAsync();

            // and list product has not been evaluated

            var NotInRecord = query.Where(p => !productberated.Any(p2 => p2.ProductId == p.ProductId)).ToList();

            return(NotInRecord);
        }
        public IActionResult Index()
        {
            string userId = _repo.getUserID();

            return(Ok(userId));
        }
        public async Task <bool> AddProductIntoCart(int id)
        {
            // Check Current User

            var Userid = _repoUser.getUserID();

            // Get list product in cart's customer

            var listItem = await _context.Carts.Where(x => x.UserId.Equals(_repoUser.getUserID())).ToListAsync();

            // Search product by product id in product list  then get some property of product

            var result = _context.Products.FirstOrDefault(x => x.Id == id);

            // Check whether products are in the shopping cart?

            var index = await FindProductByIdInCart(id);

            // if produtc not exits in product list false

            if (result == null)
            {
                return(false);
            }
            else
            {
                // if stock of product less 0 then return false

                if (result.Stock <= 0)
                {
                    return(false);
                }
                else
                {
                    // In case: product is exists in cart

                    if (index != -1)
                    {
                        // Increase quantity of product by 1

                        listItem[index].Quantity = listItem[index].Quantity + 1;

                        //  and update this product in cart

                        _context.Carts.Update(listItem[index]);
                    }

                    // In case: product is not exists in cart

                    else
                    {
                        // create new item in cart

                        var newItem = new Cart {
                            ProductId = id, Quantity = 1, UnitPrice = result.UnitPrice, UserId = Userid
                        };

                        // add item  in cart

                        _context.Carts.Add(newItem);
                    }
                }
            }
            // save

            await _context.SaveChangesAsync();

            return(true);
        }
        // this method for the customer can watch order list

        public async Task <List <OrderVm> > myOrderList()
        {
            var listOrder = await _context.Order.Include(o => o.OrderDetails).Include(o => o.OrderDetails).Where(x => x.UserId == _repoUser.getUserID()).Select(x => new OrderVm {
                Id = x.Id,

                ProductName = x.OrderDetails.Select(o => o.ProductName).ToList(),

                Quantity = x.OrderDetails.Select(o => o.Quantity).ToList(),

                Total = x.Total,

                Status = x.Status,

                UnitPrice = x.OrderDetails.Select(o => o.UnitPrice).ToList(),

                Date = x.DateOrdered,
            }).ToListAsync();

            return(listOrder);
        }