public static int AddCategory(ProductCategory category) { int result = 0; try { using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { database.Configuration.ProxyCreationEnabled = false; category.ModifiedDate = DateTime.Now; category.rowguid = new Guid(); database.ProductCategories.Add(category); database.SaveChanges(); ProductCategory newCategory = database.ProductCategories.FirstOrDefault(c => c.ProductCategoryID == category.ProductCategoryID); result = (newCategory == null) ? 0 : newCategory.ProductCategoryID; } } catch (Exception e) { if (e.InnerException is System.Data.SqlClient.SqlException) { DatabaseFault dbf = new DatabaseFault { DbOperation = "Connect to database", DbReason = "Exception accessing database", DbMessage = e.InnerException.Message }; throw new FaultException<DatabaseFault>(dbf); } else { SystemFault sf = new SystemFault { SystemOperation = "Adding category", SystemReason = "Exception adding category", SystemMessage = e.Message }; throw new FaultException<SystemFault>(sf); } } return result; }
public static ProductCategory UpdateCategory(ProductCategory category) { ProductCategory result = null; try { using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { database.Configuration.ProxyCreationEnabled = false; ProductCategory categoryToUpdate = database.ProductCategories.FirstOrDefault(c => c.ProductCategoryID == category.ProductCategoryID); if (categoryToUpdate == null) { categoryToUpdate = new ProductCategory() { ProductCategoryID = 0, Name = category.Name, IsActive = category.IsActive, ModifiedDate = DateTime.Now, rowguid = Guid.NewGuid() }; database.ProductCategories.Add(categoryToUpdate); } else { categoryToUpdate.Name = category.Name; categoryToUpdate.IsActive = category.IsActive; categoryToUpdate.ModifiedDate = DateTime.Now; categoryToUpdate.rowguid = Guid.NewGuid(); } database.SaveChanges(); result = database.ProductCategories.FirstOrDefault(c => c.rowguid == categoryToUpdate.rowguid); } } catch (Exception e) { if (e.InnerException is System.Data.SqlClient.SqlException) { DatabaseFault dbf = new DatabaseFault { DbOperation = "Connect to database", DbReason = "Exception accessing database", DbMessage = e.InnerException.Message }; throw new FaultException<DatabaseFault>(dbf); } else { SystemFault sf = new SystemFault { SystemOperation = "Updating category", SystemReason = "Updating category", SystemMessage = e.Message }; throw new FaultException<SystemFault>(sf); } } return result; }
public static bool RemoveCategory(int categoryID) { bool result = false; try { using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { database.Configuration.ProxyCreationEnabled = false; ProductCategory categoryToRemove = database.ProductCategories.FirstOrDefault(c => c.ProductCategoryID == categoryID); if (categoryToRemove != null) { database.ProductCategories.Remove(categoryToRemove); database.SaveChanges(); result = true; } } } catch (Exception e) { if (e.InnerException is System.Data.SqlClient.SqlException) { DatabaseFault dbf = new DatabaseFault { DbOperation = "Connect to database", DbReason = "Exception accessing database", DbMessage = e.InnerException.Message }; throw new FaultException<DatabaseFault>(dbf); } else { SystemFault sf = new SystemFault { SystemOperation = "Removing category", SystemReason = "Removing category", SystemMessage = e.Message }; throw new FaultException<SystemFault>(sf); } } return result; }
public static void SaveSalesOrder(List<ShoppingCartItem> shoppingCart, string username) { try { using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { database.Configuration.ProxyCreationEnabled = false; Customer customer = database.Customers.First(c => c.Username == username); SalesOrder salesOrder = new SalesOrder() { CustomerID = customer.CustomerID, Customer = customer, SalesOrderID = 0, SalesOrderItems = new List<SalesOrderItem>(), OrderDate = DateTime.Now }; foreach (ShoppingCartItem item in shoppingCart) { Product matchingProduct = database.Products.FirstOrDefault(p => p.ProductNumber == item.ProductNumber); if (matchingProduct == null) throw new FaultException<SystemFault>(new SystemFault { SystemOperation = "Checking out", SystemReason = "Shopping cart", SystemMessage = String.Format("Product {0} does not exist.", item.ProductNumber) }); SalesOrderItem salesOrderItem = new SalesOrderItem() { SalesOrderID = 0, ProductID = matchingProduct.ProductID, ActualCost = matchingProduct.ListPrice, OrderBy = item.OrderBy, ModifiedDate = DateTime.Now, Quantity = item.Quantity, }; salesOrder.SalesOrderItems.Add(salesOrderItem); } database.SalesOrders.Add(salesOrder); database.SaveChanges(); } } catch (Exception e) { if (e.InnerException is System.Data.SqlClient.SqlException) { DatabaseFault dbf = new DatabaseFault { DbOperation = "Connect to database", DbReason = "Exception accessing database", DbMessage = e.InnerException.Message }; throw new FaultException<DatabaseFault>(dbf); } else { SystemFault sf = new SystemFault { SystemOperation = "Checking out", SystemReason = "Exception checking out shopping cart", SystemMessage = e.Message }; throw new FaultException<SystemFault>(sf); } } }
private bool decrementStockLevel(string productNumber) { // Decrement the current stock level of the selected product // in the ProductInventory table. // If the update is successful then return true, otherwise return false. // The Product and ProductInventory tables are joined over the // ProductID column. try { // Connect to the AdventureWorks database by using the Entity Framework using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { // Find the ProductID for the specified product int productID = (from p in database.Products where String.Compare(p.ProductNumber, productNumber) == 0 select p.ProductID).First(); // Update the first row for this product in the ProductInventory table // that has a quantity value greater than zero. Product productInventory = database.Products.First( pi => pi.ProductID == productID && pi.Quantity > 0); // Update the stock level for the ProductInventory object productInventory.Quantity--; // Save the change back to the database database.SaveChanges(); } } catch { // If an exception occurs, return false to indicate failure return false; } // Return true to indicate success return true; }
//public static void CreateInitialUsers() //{ // CreateUser(new CustomerData() // { // Username = "******", // FirstName = "Fred", // LastName = "Flintstone" // }, "Pa$$w0rd"); // CreateUser(new CustomerData() // { // Username = "******", // FirstName = "Bert", // LastName = "Andernie" // }, "Pa$$w0rd"); // Authenicate("Fred", "Pa$$w0rd"); //} private static void CreateUser(CustomerData customer, string password) { try { using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer()) { byte[] salt = CreateSalt(32); byte[] passwordBytes = Encoding.UTF8.GetBytes(password); Customer customerToAdd = new Customer() { Username = customer.Username, FirstName = customer.FirstName, LastName = customer.LastName, IsAdmin = false, PasswordSalt = Convert.ToBase64String(salt), PasswordHash = Convert.ToBase64String(GeneratedSaltedHash(passwordBytes, salt)), }; database.Customers.Add(customerToAdd); database.SaveChanges(); } } catch (Exception e) { if (e.InnerException is System.Data.SqlClient.SqlException) { DatabaseFault dbf = new DatabaseFault { DbOperation = "Connect to database", DbReason = "Exception accessing database", DbMessage = e.InnerException.Message }; throw new FaultException<DatabaseFault>(dbf); } else { SystemFault sf = new SystemFault { SystemOperation = "Authenicating user", SystemReason = "Exception authenicating user", SystemMessage = e.Message }; throw new FaultException<SystemFault>(sf); } } }