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")); }
public async Task <Product> UpdateProductAsync(Product Product) { using (var context = new DataContextProducts()) { context.Entry(Product).State = EntityState.Modified; await context.SaveChangesAsync(); } return(Product); }
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(); } }
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); }
public async Task <Product> AddProductAsync(Product Product) { Product result; using (var context = new DataContextProducts()) { result = context.Products.Add(Product);//нужно ли делать операцию ассинхронно? await context.SaveChangesAsync(); } return(result); }
public async Task <List <Product> > GetProductsAsync() { var result = new List <Product>(); using (var context = new DataContextProducts()) { result = await context.Products.ToListAsync(); } return(result); }
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)); }
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)); }