/// <summary> /// Register - add Customer Row row from serialized dictionary /// </summary> /// <param name="dictionaryCust">packaged customer info</param> /// <returns>int representing newly updated id for customer or -1 if error</returns> public int Register(byte[] bytCustomer) { int custId = -1; Customer cust = new Customer(); eStoreDBEntities dbContext = new eStoreDBEntities(); try { Dictionary<string, Object> dictionaryCustomer = (Dictionary<string, Object>)Deserializer(bytCustomer); dbContext = new eStoreDBEntities(); // Change to retrieve and update based on Username when SimpleMembership is added (next 2 lines) String username = Convert.ToString(dictionaryCustomer["username"]); cust = dbContext.Customers.FirstOrDefault(c => c.Username == username); cust.Username = Convert.ToString(dictionaryCustomer["username"]); cust.FirstName = Convert.ToString(dictionaryCustomer["firstname"]); cust.LastName = Convert.ToString(dictionaryCustomer["lastname"]); cust.Email = Convert.ToString(dictionaryCustomer["email"]); cust.Age = Convert.ToInt32(dictionaryCustomer["age"]); cust.Address1 = Convert.ToString(dictionaryCustomer["address1"]); cust.City = Convert.ToString(dictionaryCustomer["city"]); cust.Mailcode = Convert.ToString(dictionaryCustomer["mailcode"]); cust.Region = Convert.ToString(dictionaryCustomer["region"]); cust.Country = Convert.ToString(dictionaryCustomer["country"]); cust.Creditcardtype = Convert.ToString(dictionaryCustomer["creditcardtype"]); dbContext.SaveChanges(); custId = cust.CustomerID; } catch (Exception ex) { ErrorRoutine(ex, "CustomerModel", "Register"); } return custId; }
/// <summary> /// GetCurrentProfile - obtain Customer Row row from db /// </summary> /// <returns>packaged customer info in a dictionary and streamed to byte array</returns> public byte[] GetCurrentProfile(string username) { Customer cust = new Customer(); eStoreDBEntities dbContext = new eStoreDBEntities(); Dictionary<string, Object> dictionaryCustomer = new Dictionary<string, Object>(); try { dbContext = new eStoreDBEntities(); cust = dbContext.Customers.FirstOrDefault(c => c.Username == username); if (cust != null) { dictionaryCustomer["username"] = cust.Username; dictionaryCustomer["firstname"] = cust.FirstName; dictionaryCustomer["lastname"] = cust.LastName; dictionaryCustomer["email"] = cust.Email; dictionaryCustomer["age"] = cust.Age; dictionaryCustomer["address1"] = cust.Address1; dictionaryCustomer["city"] = cust.City; dictionaryCustomer["mailcode"] = cust.Mailcode; dictionaryCustomer["region"] = cust.Region; dictionaryCustomer["country"] = cust.Country; dictionaryCustomer["creditcardtype"] = cust.Creditcardtype; dictionaryCustomer["customerid"] = cust.CustomerID; } } catch (Exception ex) { ErrorRoutine(ex, "CustomerModel", "GetCurrentProfile"); } return Serializer(dictionaryCustomer); }
/// <summary> /// Grab the product information to display in the catalogue /// </summary> /// <returns>List of Product classes defined from EF</returns> public List<Product> GetAll() { eStoreDBEntities dbContext; List<Product> allProducts = null; try { dbContext = new eStoreDBEntities(); allProducts = dbContext.Products.ToList(); } catch (Exception ex) { ErrorRoutine(ex, "ProductData", "GetAll"); } return allProducts; }
/// <summary> /// Get Closest Branches /// </summary> /// <param name="dictionaryAddresses">latitude and longitude coordinates determined by google</param> /// <returns>List of details of 3 closet branches</returns> public List<ClosestBranches> GetClosetBranches(Dictionary<string, object> dictionaryAddresses) { eStoreDBEntities dbContext; List<ClosestBranches> branchDetails = new List<ClosestBranches>(); try { dbContext = new eStoreDBEntities(); float? latitude = Convert.ToSingle(dictionaryAddresses["lat"]); float? longitude = Convert.ToSingle(dictionaryAddresses["long"]); // executes stored proc via EF function branchDetails = dbContext.GetThreeClosestBranches(latitude, longitude).ToList(); } catch (Exception e) { ErrorRoutine(e, "OrderModel", "GetClosestBranches"); } return branchDetails; }
/// <summary> /// Add Order Method - Adds order row, line item rows via trans /// </summary> /// <param name="HashtableOrder"></param> /// <returns>populated dictionary with new order #, bo flag or error</returns> public byte[] AddOrder(byte[] bytOrder) { Dictionary<string, Object> dictionaryOrder = (Dictionary<string, Object>)Deserializer(bytOrder); Dictionary<string, Object> dictionaryReturnValues = new Dictionary<string, Object>(); // deserialize dictionary contents into local variables int[] qty = (int[])dictionaryOrder["qty"]; string[] prodcd = (string[])dictionaryOrder["prodcd"]; Decimal[] sellPrice = (Decimal[])dictionaryOrder["msrp"]; int cid = Convert.ToInt32(dictionaryOrder["cid"]); bool boFlg = false; Decimal oTotal = Convert.ToDecimal(dictionaryOrder["amt"]); eStoreDBEntities dbContext; // Define a transaction scope for the operations. using (TransactionScope transaction = new TransactionScope()) { try { dbContext = new eStoreDBEntities(); Order myOrder = new Order(); // back to the db to get the right Customer based on session customer id var selectedCusts = dbContext.Customers.Where(c => c.CustomerID == cid); myOrder.Customer = selectedCusts.First(); // build the order myOrder.OrderDate = DateTime.Now; myOrder.OrderAmount = oTotal; dbContext.Orders.Add(myOrder); // Add order and get OrderID for line Item for (int idx = 0; idx < qty.Length; idx++) { if (qty[idx] > 0) { OrderLineitem item = new OrderLineitem(); string pcd = prodcd[idx]; var selectedProds = dbContext.Products.Where(p => p.ProductID == pcd); item.Product = selectedProds.First(); // got product for item if (item.Product.QtyOnHand > qty[idx]) // enough stock { item.Product.QtyOnHand = item.Product.QtyOnHand - qty[idx]; item.QtySold = qty[idx]; item.QtyOrdered = qty[idx]; item.QtyBackOrdered = 0; item.SellingPrice = sellPrice[idx]; } else // not enough stock { item.QtyBackOrdered = qty[idx] - item.Product.QtyOnHand; item.Product.QtyOnBackOrder = item.Product.QtyOnBackOrder + (qty[idx] - item.Product.QtyOnHand); item.Product.QtyOnHand = 0; item.QtyOrdered = qty[idx]; item.QtySold = item.QtyOrdered - item.QtyBackOrdered; item.SellingPrice = sellPrice[idx]; boFlg = true; // something backordered } myOrder.OrderLineitems.Add(item); } } dbContext.SaveChanges(); // made it this far, persist changes // throw new Exception("Rollback"); // test trans by uncommenting out this line // Mark the transaction as complete. transaction.Complete(); dictionaryReturnValues.Add("orderid", myOrder.OrderID); dictionaryReturnValues.Add("boflag", boFlg); dictionaryReturnValues.Add("message", ""); } catch (Exception e) // if the catch is hit, the trans will be rolled back by the framework { ErrorRoutine(e, "OrderData", "AddOrder"); dictionaryReturnValues.Add("message", "Problem with Order"); } return Serializer(dictionaryReturnValues); } }
/// <summary> /// Retrieve Order information for a single customer /// </summary> /// <param name="cid"></param> /// <returns>List of Order instances for a particular customer</returns> public List<Order> GetAllForCust(int custid) { eStoreDBEntities dbContext; List<Order> allOrders = null; try { dbContext = new eStoreDBEntities(); allOrders = dbContext.Orders.Where(o => o.Customer.CustomerID == custid).ToList(); } catch (Exception ex) { ErrorRoutine(ex, "OrderData", "GetAllForCust"); } return allOrders; }
/// <summary> /// Get All details for all orders for a customer /// </summary> /// <param name="custid"> customerid</param> /// <returns>List of details for all orders customer owns </returns> public List<OrderDetailsModel> GetAllDetailsForAllOrders(int custid) { List<OrderDetailsModel> allDetails = new List<OrderDetailsModel>(); try { eStoreDBEntities db = new eStoreDBEntities(); // LINQ way of doing INNER JOINS var results = from o in db.Orders join l in db.OrderLineitems on o.OrderID equals l.OrderID join p in db.Products on l.ProductID equals p.ProductID where (o.CustomerID == custid) select new OrderDetailsModel { OrderID = o.OrderID, ProductName = p.ProductName, SellingPrice = l.SellingPrice, QtySold = l.QtySold, QtyOrdered = l.QtyOrdered, QtyBackOrdered = l.QtyBackOrdered, OrderDate = o.OrderDate }; allDetails = results.ToList<OrderDetailsModel>(); } catch (Exception e) { ErrorRoutine(e, "OrderData", "GetAllDetailsForAllOrders"); } return allDetails; }