public bool InsertProduct(ProductModel product) { if (product == null) return false; using (Db.Database.BeginTransaction()) { var matches = (from p in Db.Products where p.ProductCode == product.ProductCode select p.ProductCode).Count(); if (matches > 0) return false; Db.Products.Add(new Product { ProductCode = product.ProductCode, Title = product.Title, Active = true, Description = product.Description, Price = product.Price, Quantity = product.Quantity, Released = product.Released }); Db.SaveChanges(); } return true; }
public bool InsertProduct(ProductModel product) { return UpsertProduct(product); }
public bool UpsertProduct(ProductModel product) { if (AdoContext == null || product == null || string.IsNullOrEmpty(product.ProductCode)) return false; try { using (var command = new SqlCommand("demosp_UpsertProduct", AdoContext.Connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@productCode", product.ProductCode)); command.Parameters.Add(new SqlParameter("@title", product.Title)); command.Parameters.Add(new SqlParameter("@description", product.Description)); command.Parameters.Add(new SqlParameter("@price", product.Price)); command.Parameters.Add(new SqlParameter("@released", product.Released)); // If there was no processResultRow Action provided, then we execute a non-query command.ExecuteNonQuery(); } } catch (Exception ex) { Log.Error("Exception occurred adding Account", ex); } return true; }
public bool UpdateProduct(ProductModel product) { return UpsertProduct(product); }
public IEnumerable<ProductModel> ReadProducts(int userRole) { if (AdoContext == null) yield break; using (var command = new SqlCommand("demosp_ReadProducts", AdoContext.Connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@userRole", userRole)); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var product = new ProductModel { ProductCode = Convert.ToString(reader["ProductCode"]), Title = Convert.ToString(reader["Title"]), Price = Convert.ToDecimal(reader["Price"]), Quantity = Convert.ToInt32(reader["Quantity"]), Description = Convert.ToString(reader["Description"]), Active = Convert.ToBoolean(reader["Active"]), Released = Convert.ToBoolean(reader["Released"]), }; yield return product; } } } }
public bool UpdateProduct(ProductModel productDto) { if (productDto == null) return false; using (Db.Database.BeginTransaction()) { var products = from p in Db.Products where p.ProductCode == productDto.ProductCode select p; if (products.Count() != 1) return false; var product = products.First(); product.Title = productDto.Title; product.Active = productDto.Active; product.Description = productDto.Title; product.Price = productDto.Price; product.Quantity = productDto.Quantity; product.Released = productDto.Released; Db.Entry(product).State = EntityState.Modified; Db.SaveChanges(); return true; } }