public static void EditProduct(ItemMVCModel item) { using (IDbConnection cnn = new SqlConnection(GlobalConfig.CnnString())) { if (item != null) { List <ItemMVCModel> products = LoadProducts(); if (products.Where(x => x.Id == item.Id).FirstOrDefault() != null) { var obj = new { Cost = item.Cost, ItemName = item.ItemName, Description = item.Description, ItemUnit = item.ItemUnit, Id = item.Id }; cnn.Execute("UPDATE Products SET " + "Cost = @Cost, ItemName = @ItemName, " + "Description = @Description, ItemUnit = @ItemUnit " + "WHERE Id = @Id", obj); } } } }
// GET: Item/Delete/5 public ActionResult Delete(int id) { ItemMVCModel model = SQLiteDataAccess.LoadProducts().Where(x => x.Id == id).FirstOrDefault(); if (model != null) { return(View(model)); } return(RedirectToAction("Index")); }
public static void RemoveProduct(ItemMVCModel item) { using (IDbConnection cnn = new SqlConnection(GlobalConfig.CnnString())) { if (item != null) { List <ItemMVCModel> products = LoadProducts(); if (products.Where(x => x.Id == item.Id).FirstOrDefault() != null) { cnn.Execute($"DELETE FROM Products WHERE Id = {item.Id}"); } } } }
public ActionResult Edit(int id, ItemMVCModel model) { try { if (ModelState.IsValid) { SQLiteDataAccess.EditProduct(model); } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Delete(int id, ItemMVCModel model) { try { if (model != null) { SQLiteDataAccess.RemoveProduct(model); } return(RedirectToAction("Index")); } catch { return(View()); } }
public static void SaveProduct(ItemMVCModel item) { using (IDbConnection cnn = new SqlConnection(GlobalConfig.CnnString())) { if (item != null) { var obj = new { Cost = item.Cost, ItemName = item.ItemName, Description = item.Description, ItemUnit = item.ItemUnit }; cnn.Execute("insert into Products (Cost, ItemName, Description, ItemUnit)" + " values (@Cost, @ItemName, @Description, @ItemUnit)", obj); item.Id = cnn.Query <int>("select Id from Products").OrderBy(x => x).Last(); } } }
private List <ItemMVCModel> GetProductsInformation(List <int> selectedProducts, List <decimal> productPrices, List <int> productQuantities) { List <ItemMVCModel> products = SQLiteDataAccess.LoadProducts(); List <ItemMVCModel> result = new List <ItemMVCModel>(); foreach (int id in selectedProducts) { ItemMVCModel product = products.Where(x => x.Id == id).FirstOrDefault(); product.Quantity = productQuantities[products.IndexOf(products.Where(x => x.Id == id).FirstOrDefault())]; //If product price loaded from application differs from price loaded from DB, change the price for this document if (productPrices[products.IndexOf(products.Where(x => x.Id == id).FirstOrDefault())] != products.Where(x => x.Id == id).First().Cost) { product.Cost = productPrices[products.IndexOf(products.Where(x => x.Id == id).FirstOrDefault())]; } result.Add(product); } return(result); }
// GET: Item/Create public ActionResult Create() { ItemMVCModel model = new ItemMVCModel(); return(View(model)); }