public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request) { using (var db = new _DatabseContextShop()) { return(Json(Product.GetProducts(db).ToDataSourceResult(request))); } }
public DefaultController() { ViewBag.CartTotalPrice = CartTotalPrice; ViewBag.Cart = Cart; ViewBag.CartUnits = Cart.Count; Database = new _DatabseContextShop(); }
public ActionResult Products_Destroy([DataSourceRequest] DataSourceRequest request, ProductTable productTable) { if (ModelState.IsValid) { using (var db = new _DatabseContextShop()) { var entity = new ProductTable { Id = productTable.Id, ProductName = productTable.ProductName, Description = productTable.Description, UnitPrice = productTable.UnitPrice, UnitsInStock = productTable.UnitsInStock, UnitsOnOrder = productTable.UnitsOnOrder, Discontinued = productTable.Discontinued, LastSupply = productTable.LastSupply }; db.Products.Attach(entity); db.Products.Remove(entity); db.SaveChanges(); } } return(Json(new[] { productTable }.ToDataSourceResult(request, ModelState))); }
public ActionResult List() { using (var db = new _DatabseContextShop()) { List <ProductData> model = Product.GetProducts(db); return(View(model)); } }
public ActionResult ShowProduct(int id) { using (var db = new _DatabseContextShop()) { var model = Product.GetOneProductModel(id, db); return(View(model)); } }
public ActionResult AddProduct(ProductData model) { if (ModelState.IsValid) { using (var db = new _DatabseContextShop()) { Product.Add(model, db); } return(RedirectToAction("ListAdmin")); } return(View(model)); }
public ActionResult AddProduct() { var model = new ProductData(); int i; //New product id 1 bigger than current biggest using (var db = new _DatabseContextShop()) { i = db.Products.OrderByDescending(x => x.Id).FirstOrDefault().Id; } model.ProductID = i + 1; return(View(model)); }
public JsonResult UpdateTotal() { using (var db = new _DatabseContextShop()) { decimal?total; total = db.ShoppingCartDatas.Select(p => p.UnitPrice * p.Quantity).Sum(); if (total == null) { return(Json(new { d = "" }, JsonRequestBehavior.AllowGet)); } return(Json(new { d = String.Format("{0:c}", total) }, JsonRequestBehavior.AllowGet)); } }
public void ClearCart() { using (var db = new _DatabseContextShop()) { List <ShoppingCartTable> carts = Database.ShoppingCartDatas.ToList(); carts.ForEach(a => { ProductTable product = Database.Products.FirstOrDefault(p => p.Id == a.PID); product.UnitsInStock += a.Quantity; }); Database.ShoppingCartDatas.RemoveRange(carts); Database.SaveChanges(); } }
public JsonResult QuanityChange(int type, int pId) { using (var db = new _DatabseContextShop()) { ShoppingCartTable product = db.ShoppingCartDatas.FirstOrDefault(p => p.PID == pId); if (product == null) { return(Json(new { d = "0" })); } ProductTable actualProduct = db.Products.FirstOrDefault(p => p.Id == pId); int? quantity; // if type 0, decrease quantity // if type 1, increase quanity switch (type) { case 0: product.Quantity--; actualProduct.UnitsInStock++; break; case 1: product.Quantity++; actualProduct.UnitsInStock--; break; case -1: actualProduct.UnitsInStock += product.Quantity; product.Quantity = 0; break; default: return(Json(new { d = "0" })); } if (product.Quantity == 0) { db.ShoppingCartDatas.Remove(product); quantity = 0; } else { quantity = product.Quantity; } db.SaveChanges(); return(Json(new { d = quantity })); } }
public ActionResult Clear() { using (var db = new _DatabseContextShop()) { List <ShoppingCartTable> carts = Database.ShoppingCartDatas.ToList(); carts.ForEach(a => { ProductTable product = Database.Products.FirstOrDefault(p => p.Id == a.PID); product.UnitsInStock += a.Quantity; }); Database.ShoppingCartDatas.RemoveRange(carts); Database.SaveChanges(); return(RedirectToAction("Index", "Home", null)); } }
public static void Add(ProductData product, _DatabseContextShop db) { var entity = new ProductTable(); entity.ProductName = product.ProductName; entity.UnitPrice = product.UnitPrice; entity.Description = product.Description; entity.UnitsInStock = (short)product.UnitsInStock; entity.Discontinued = product.Discontinued; entity.Id = product.ProductID; entity.LastSupply = DateTime.Now; entity.UnitsOnOrder = product.UnitsOnOrder; db.Products.Add(entity); db.SaveChanges(); }
public ActionResult Products_ReadAdmin([DataSourceRequest] DataSourceRequest request) { using (var db = new _DatabseContextShop()) { IQueryable <ProductTable> products = db.Products; DataSourceResult result = products.ToDataSourceResult(request, productTable => new { Id = productTable.Id, ProductName = productTable.ProductName, Description = productTable.Description, UnitPrice = productTable.UnitPrice, UnitsInStock = productTable.UnitsInStock, UnitsOnOrder = productTable.UnitsOnOrder, Discontinued = productTable.Discontinued, LastSupply = productTable.LastSupply }); return(Json(result)); } }
public static IQueryable <ProductTable> GetAdminProducts(_DatabseContextShop db) { IQueryable <ProductTable> result; { result = db.Products .Select(product => new ProductTable { Id = product.Id, ProductName = product.ProductName, UnitPrice = product.UnitPrice ?? 0, UnitsInStock = product.UnitsInStock ?? 0, UnitsOnOrder = product.UnitsOnOrder ?? 0, Discontinued = product.Discontinued, LastSupply = product.LastSupply, Description = product.Description }); return(result); } }
public static ProductData GetOneProductModel(int id, _DatabseContextShop db) { //using (var db = new _DatabseContextShop()) //{ var result = db.Products.Where(x => x.Id == id) .Select(x => new ProductData { Discontinued = x.Discontinued, Description = x.Description, LastSupply = x.LastSupply, ProductID = x.Id, ProductName = x.ProductName, UnitPrice = x.UnitPrice, UnitsInStock = x.UnitsInStock, UnitsOnOrder = x.UnitsOnOrder }) .FirstOrDefault(); return(result); //} }
public static List <ProductData> GetProducts(_DatabseContextShop db) { List <ProductData> result; { result = db.Products .Where(product => product.Discontinued != true && product.UnitsInStock > 0 ) .Select(product => new ProductData { ProductID = product.Id, ProductName = product.ProductName, Description = product.Description, UnitPrice = product.UnitPrice ?? 0, UnitsInStock = product.UnitsInStock ?? 0, UnitsOnOrder = product.UnitsOnOrder ?? 0, Discontinued = product.Discontinued, LastSupply = product.LastSupply }).ToList(); return(result); } }
public ProductTable GetOneProduct(int id, _DatabseContextShop db) { ProductTable result = db.Products.FirstOrDefault(x => x.Id == id); return(result); }