Пример #1
0
        public IActionResult Añadir(int id)
        {
            DataContextProducts db             = HttpContext.RequestServices.GetService(typeof(DataContextProducts)) as DataContextProducts;
            List <Product>      listaProductos = db.GetAllProducts();

            if (SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart") == null)
            {
                List <Item> cart = new List <Item>();
                cart.Add(new Item {
                    Product = db.find(id, listaProductos), Quantity = 1
                });
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List <Item> cart  = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item {
                        Product = db.find(id, listaProductos), Quantity = 1
                    });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return(RedirectToAction("Index"));
        }
Пример #2
0
 public async Task <Product> UpdateProductAsync(Product Product)
 {
     using (var context = new DataContextProducts())
     {
         context.Entry(Product).State = EntityState.Modified;
         await context.SaveChangesAsync();
     }
     return(Product);
 }
Пример #3
0
        public async Task DeleteProductAsync(int id)
        {
            using (var context = new DataContextProducts())
            {
                var Product = await context.Products.FirstOrDefaultAsync(f => f.ProductId == id);

                context.Entry(Product).State = EntityState.Deleted;
                await context.SaveChangesAsync();
            }
        }
Пример #4
0
        public async Task <Product> GetProductAsync(int id)
        {
            var Product = new Product();

            using (var context = new DataContextProducts())
            {
                Product = await Task.Run(() => context.Products.FirstOrDefault(f => f.ProductId == id));
            }
            return(Product);
        }
Пример #5
0
        public async Task <Product> AddProductAsync(Product Product)
        {
            Product result;

            using (var context = new DataContextProducts())
            {
                result = context.Products.Add(Product);//нужно ли делать операцию ассинхронно?
                await context.SaveChangesAsync();
            }
            return(result);
        }
Пример #6
0
        public async Task <List <Product> > GetProductsAsync()
        {
            var result = new List <Product>();

            using (var context = new DataContextProducts())
            {
                result = await context.Products.ToListAsync();
            }

            return(result);
        }
Пример #7
0
        public IActionResult Index()
        {
            DataContextProducts db             = HttpContext.RequestServices.GetService(typeof(DataContextProducts)) as DataContextProducts;
            List <Product>      listaProductos = db.GetAllProducts();
            List <Product>      perifericos    = new List <Product>();

            foreach (Product p in listaProductos)
            {
                if (p.Tipo.Contains("periferico"))
                {
                    perifericos.Add(p);
                }
            }
            return(View(model: perifericos));
        }
Пример #8
0
        public IActionResult Index()
        {
            DataContextProducts db             = HttpContext.RequestServices.GetService(typeof(DataContextProducts)) as DataContextProducts;
            List <Product>      listaProductos = db.GetAllProducts();
            List <Product>      componentes    = new List <Product>();

            foreach (Product p in listaProductos)
            {
                if (p.Tipo.Contains("componente"))
                {
                    componentes.Add(p);
                    //COMPONENTES ES UNA LISTA DE PRODUCTOS Y TU LA TRATAS COMO LISTA DE ITEMS MELON
                }
            }
            return(View(model: componentes));
        }