private IList <Cart> GetCartBy(int?orderId, int?prodId, int?quantity)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Cart> query = context.Carts;
                if (orderId != null)
                {
                    query = query.Where(o => o.orderID == orderId.Value);
                }
                if (prodId != null)
                {
                    query = query.Where(o => o.prodID == prodId.Value);
                }
                if (quantity != null)
                {
                    query = query.Where(o => o.quantity == quantity.Value);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List <Cart>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Cart(item));
                    }
                    return(convertedList);
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Order not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
 public void DeleteCart(string orderId, string prodId)
 {
     try
     {
         int orderIdToDelete = 0;
         int prodIdToDelete  = 0;
         if (int.TryParse(orderId, out orderIdToDelete) && int.TryParse(prodId, out prodIdToDelete))
         {
             using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
             {
                 var fetchedCartToDelete = context.Carts.Where(o => o.orderID == orderIdToDelete && o.prodID == prodIdToDelete).FirstOrDefault();
                 if (fetchedCartToDelete != null)
                 {
                     context.Carts.Remove(fetchedCartToDelete);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new WebFaultException <Error>(new Error("Cart with order ID " + orderId + " and product ID " + prodId + " not found.", ""), System.Net.HttpStatusCode.NotFound);
                 }
             }
         }
     }
     catch (WebFaultException <Error> ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new WebFaultException <Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
        public Product GetProducts(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Product> productQuery = null;
                // Try parsing search string as every property of customer.
                int prodId = 0;
                if (int.TryParse(search, out prodId))
                {
                    productQuery = context.Products.Where(o => o.prodID == prodId);
                }
                else
                {
                    productQuery = context.Products.Where(o => o.prodName == search);
                }

                var fetchedProduct = productQuery.FirstOrDefault();
                if (fetchedProduct != null)
                {
                    return(new Product(fetchedProduct));
                }
                else
                {
                    return(null);
                }
            }
        }
        public Order GetOrder(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Order> orderQuery = null;
                // Try parsing search string as every property of customer.
                int prodId = 0;
                if (int.TryParse(search, out prodId))
                {
                    orderQuery = context.Orders.Where(o => o.orderID == prodId || o.custID == prodId);
                }
                else
                {
                    // TODO: orderQuery = context.Orders.Where(o => o. == search);
                }

                var fetchedOrder = orderQuery.FirstOrDefault();
                if (fetchedOrder != null)
                {
                    return(new Order(fetchedOrder));
                }
                else
                {
                    return(null);
                }
            }
        }
 public void DeleteOrder(string id)
 {
     try
     {
         int orderWithIdToDelete = 0;
         if (int.TryParse(id, out orderWithIdToDelete))
         {
             using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
             {
                 var fetchedOrderToDelete = context.Orders.Where(o => o.orderID == orderWithIdToDelete).FirstOrDefault();
                 if (fetchedOrderToDelete != null)
                 {
                     context.Orders.Remove(fetchedOrderToDelete);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new WebFaultException <Error>(new Error("Order with ID " + id + " not found.", ""), System.Net.HttpStatusCode.NotFound);
                 }
             }
         }
     }
     catch (WebFaultException <Error> ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new WebFaultException <Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
 public void UpdateCustomer(Customer customer)
 {
     try
     {
         using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
         {
             var fetchedCustomerToUpdate = context.Customers.Where(o => o.custID == customer.custID).FirstOrDefault();
             if (fetchedCustomerToUpdate != null)
             {
                 fetchedCustomerToUpdate.firstName   = customer.firstName;
                 fetchedCustomerToUpdate.lastName    = customer.lastName;
                 fetchedCustomerToUpdate.phoneNumber = customer.phoneNumber;
                 context.SaveChanges();
             }
             else
             {
                 throw new WebFaultException <Error>(new Error("Customer with ID " + customer.custID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
             }
         }
     }
     catch (Exception ex)
     {
         throw new WebFaultException <Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
 public void AddOrder(Order order)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         context.Orders.Add(order.GenerateDbModel());
         context.SaveChanges();
     }
 }
 public void AddOrder(Order order)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         context.Orders.Add(order.GenerateDbModel());
         context.SaveChanges();
     }
 }
 public void AddProduct(Product product)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         context.Products.Add(product.GenerateDbModel());
         context.SaveChanges();
     }
 }
Exemplo n.º 10
0
        public IList <Product> GetProductList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedProducts = context.Products;
                var productList     = new List <Product>();
                foreach (var c in fetchedProducts)
                {
                    productList.Add(new Product(c));
                }

                return(productList);
            }
        }
Exemplo n.º 11
0
        public IList <Cart> GetCartList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedCarts = context.Carts;
                var cartList     = new List <Cart>();
                foreach (var c in fetchedCarts)
                {
                    cartList.Add(new Cart(c));
                }

                return(cartList);
            }
        }
Exemplo n.º 12
0
        public IList <Order> GetOrderList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedOrders = context.Orders;
                var orderList     = new List <Order>();
                foreach (var o in fetchedOrders)
                {
                    orderList.Add(new Order(o));
                }

                return(orderList);
            }
        }
Exemplo n.º 13
0
 public void AddCustomer(Customer customer)
 {
     try
     {
         using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
         {
             context.Customers.Add(customer.GenerateDbModel());
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw new WebFaultException<Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 14
0
 public void AddCustomer(Customer customer)
 {
     try
     {
         using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
         {
             context.Customers.Add(customer.GenerateDbModel());
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw new WebFaultException <Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 15
0
 public void UpdateCart(Cart cart)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedCartToUpdate = context.Carts.Where(o => o.orderID == cart.orderID && o.prodID == cart.prodID).FirstOrDefault();
         if (fetchedCartToUpdate != null)
         {
             fetchedCartToUpdate.quantity = cart.quantity;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException <Error>(new Error("Cart with order ID " + cart.orderID + " and product ID " + cart.prodID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 16
0
 public void UpdateOrder(Order order)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedOrderToUpdate = context.Orders.Where(o => o.orderID == order.orderID).FirstOrDefault();
         if (fetchedOrderToUpdate != null)
         {
             fetchedOrderToUpdate.custID    = order.custID;
             fetchedOrderToUpdate.orderDate = order.orderDate;
             fetchedOrderToUpdate.poNumber  = order.poNumber;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException <Error>(new Error("Order with ID " + order.orderID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 17
0
 public void UpdateProduct(Product product)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedProductToUpdate = context.Products.Where(o => o.prodID == product.prodID).FirstOrDefault();
         if (fetchedProductToUpdate != null)
         {
             fetchedProductToUpdate.prodName   = product.prodName;
             fetchedProductToUpdate.price      = product.price;
             fetchedProductToUpdate.prodWeight = product.prodWeight;
             fetchedProductToUpdate.inStock    = product.inStock;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException <Error>(new Error("Product with ID " + product.prodID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 18
0
        private IList <Product> GetProductsBy(int?prodId, string prodName, double?price, double?prodWeight, bool?inStock)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Product> productQuery = context.Products;
                if (prodId != null)
                {
                    productQuery = productQuery.Where(o => o.prodID == prodId.Value);
                }
                if (prodName != null)
                {
                    productQuery = productQuery.Where(o => o.prodName == prodName);
                }
                if (price != null)
                {
                    productQuery = productQuery.Where(o => o.price == price.Value);
                }
                if (prodWeight != null)
                {
                    productQuery = productQuery.Where(o => o.prodWeight == prodWeight.Value);
                }
                if (inStock != null)
                {
                    productQuery = productQuery.Where(o => o.inStock == inStock.Value);
                }

                var fetchedList = productQuery.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List <Product>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Product(item));
                    }
                    return(convertedList);
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Product not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 19
0
 public void AddCart(Cart cart)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var product = context.Products.Where(o => o.prodID == cart.prodID).FirstOrDefault();
         if (product == null)
         {
             throw new WebFaultException <Error>(new Error("Product with ID " + cart.prodID + " does not exist.", ""), System.Net.HttpStatusCode.BadRequest);
         }
         else if (product.inStock == false)
         {
             throw new WebFaultException <Error>(new Error("Product with ID " + cart.prodID + " not in stock.", ""), System.Net.HttpStatusCode.BadRequest);
         }
         else
         {
             context.Carts.Add(cart.GenerateDbModel());
             context.SaveChanges();
         }
     }
 }
Exemplo n.º 20
0
        public IList <Customer> GetCustomerList()
        {
            try
            {
                using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
                {
                    var customerList      = context.Customers;
                    var customersToReturn = new List <Customer>();
                    foreach (var c in customerList)
                    {
                        customersToReturn.Add(new Customer(c));
                    }

                    return(customersToReturn);
                }
            }
            catch (Exception ex)
            {
                throw new WebFaultException <Error>(new Error("Unexpected Exception", ex.Message), System.Net.HttpStatusCode.InternalServerError);
            }
        }
Exemplo n.º 21
0
        public void AddCart(Cart cart)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var product = context.Products.Where(o => o.prodID == cart.prodID).FirstOrDefault();
                if (product == null)
                {
                    throw new WebFaultException<Error>(new Error("Product with ID " + cart.prodID + " does not exist.",""), System.Net.HttpStatusCode.BadRequest);
                }
                else if (product.inStock == false)
                {
                    throw new WebFaultException<Error>(new Error("Product with ID " + cart.prodID + " not in stock.", ""), System.Net.HttpStatusCode.BadRequest);
                }
                else
                {

                    context.Carts.Add(cart.GenerateDbModel());
                    context.SaveChanges();
                }

            }
        }
Exemplo n.º 22
0
        public IList <Customer> GetCustomerBy(int?custID, string firstName, string lastName, string phoneNumber)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Customer> query = context.Customers;
                if (custID != null)
                {
                    query = query.Where(o => o.custID == custID.Value);
                }
                if (firstName != null)
                {
                    query = query.Where(o => o.firstName == firstName);
                }
                if (lastName != null)
                {
                    query = query.Where(o => o.lastName == lastName);
                }
                if (phoneNumber != null)
                {
                    query = query.Where(o => o.phoneNumber == phoneNumber);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List <Customer>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Customer(item));
                    }
                    return(convertedList);
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Customer not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 23
0
        private IList <Order> GetOrderBy(int?orderId, int?custId, string poNumber, DateTime?orderDate)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Order> query = context.Orders;
                if (orderId != null)
                {
                    query = query.Where(o => o.orderID == orderId.Value);
                }
                if (custId != null)
                {
                    query = query.Where(o => o.custID == custId.Value);
                }
                if (poNumber != null)
                {
                    query = query.Where(o => o.poNumber == poNumber);
                }
                if (orderDate != null)
                {
                    query = query.Where(o => o.orderDate == orderDate.Value);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List <Order>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Order(item));
                    }
                    return(convertedList);
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Product not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 24
0
        public Customer GetCustomer(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable <Models.Customer> customerQuery = null;
                // Try parsing search string as every property of customer.
                int custId = 0;
                if (int.TryParse(search, out custId))
                {
                    customerQuery = context.Customers.Where(o => o.custID == custId);
                }

                var fetchedCustomer = customerQuery.FirstOrDefault();
                if (fetchedCustomer != null)
                {
                    return(new Customer(fetchedCustomer));
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Customer not found", "Customer with ID " + search + " not found."), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 25
0
        public IList<Product> GetProductList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedProducts = context.Products;
                var productList = new List<Product>();
                foreach (var c in fetchedProducts)
                {
                    productList.Add(new Product(c));
                }

                return productList;
            }
        }
Exemplo n.º 26
0
        public Product GetProducts(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Product> productQuery = null;
                // Try parsing search string as every property of customer.
                int prodId = 0;
                if (int.TryParse(search, out prodId))
                {
                    productQuery = context.Products.Where(o => o.prodID == prodId);
                }
                else
                {
                    productQuery = context.Products.Where(o => o.prodName == search);
                }

                var fetchedProduct = productQuery.FirstOrDefault();
                if (fetchedProduct != null)
                {
                    return new Product(fetchedProduct);
                }
                else
                {
                    return null;
                }
            }
        }
Exemplo n.º 27
0
        public PurchaseOrder GetPurchaseOrder(string parameters)
        {
            var arguments = this.ParseArguments(parameters);
            if (arguments != null)
            {
                // Check if we can find at least 1 customer and order argument and does not have any other arguments that are not properties of customer and order.

                // arguments only contain customer and order arguments.
                var argumentsThatAreForCustomerOrOrder = arguments.Keys.Where(
                    key => key == Customer_custID ||
                    key == Customer_firstName ||
                    key == Customer_lastName ||
                    key == Order_custID ||
                    key == Order_orderDate ||
                    key == Order_orderID ||
                    key == Order_poNumber);
                if (argumentsThatAreForCustomerOrOrder.Count() == arguments.Count)
                {
                    // Check if we have at least one of both
                    if ((arguments.ContainsKey(Order_orderID) ||
                        arguments.ContainsKey(Order_custID) ||
                        arguments.ContainsKey(Order_poNumber) ||
                        arguments.ContainsKey(Order_orderDate))
                        &&
                        (arguments.ContainsKey(Customer_custID) ||
                        arguments.ContainsKey(Customer_firstName) ||
                        arguments.ContainsKey(Customer_lastName)))
                    {

                        var matchingCustomers = this.GetCustomerBy(
                            arguments.ContainsKey(Customer_custID) ? int.Parse(arguments[Customer_custID]) as int? : null,
                            arguments.ContainsKey(Customer_firstName) ? arguments[Customer_firstName] : null,
                            arguments.ContainsKey(Customer_lastName) ? arguments[Customer_lastName] : null,
                            arguments.ContainsKey(Customer_phoneNumber) ? arguments[Customer_phoneNumber] : null);

                        if (matchingCustomers.Count > 1)
                        {
                            throw new WebFaultException<Error>(new Error("Cannot get order for more then 1 customer.", ""), System.Net.HttpStatusCode.BadRequest);
                        }
                        else
                        {
                            var matchingCustomer = matchingCustomers.First();
                            var ordersMadeByCustomer = this.GetOrderBy(
                                arguments.ContainsKey(Order_orderID) ? int.Parse(arguments[Order_orderID]) as int? : null,
                                matchingCustomer.custID,
                                arguments.ContainsKey(Order_poNumber) ? arguments[Order_poNumber] : null,
                                arguments.ContainsKey(Order_orderDate) ? DateTime.Parse(arguments[Order_orderDate]) as DateTime? : null);

                            if (ordersMadeByCustomer == null && ordersMadeByCustomer.Count == 0)
                            {
                                throw new WebFaultException<Error>(new Error("No such order.", ""), System.Net.HttpStatusCode.BadRequest);
                            }
                            else
                            {
                                var po = new PurchaseOrder()
                                {
                                    Customer = matchingCustomer,
                                    Order = ordersMadeByCustomer.First(),
                                    OrderedProducts = new List<OrderedProduct>()
                                };

                                // We need to get EF Model again because above order was not tracked by EF.
                                using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
                                {
                                    var order = context.Orders.Where(o => o.orderID == po.Order.orderID).First();
                                    foreach (var cart in order.Carts)
                                    {
                                        po.OrderedProducts.Add(new OrderedProduct()
                                        {
                                            Product = new Product(cart.Product),
                                            quantity = cart.quantity
                                        });
                                    }
                                }

                                return po;
                            }
                        }
                    }
                    else
                    {
                        throw new WebFaultException<Error>(new Error("Minimum 1 customer and 1 order argument required!", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Only customer and order arguments allowed.", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest);
                }
            }
            else
            {
                throw new WebFaultException<Error>(new Error("Argument formatting invalid!", ""), System.Net.HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 28
0
 public void UpdateCart(Cart cart)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedCartToUpdate = context.Carts.Where(o => o.orderID == cart.orderID && o.prodID == cart.prodID).FirstOrDefault();
         if (fetchedCartToUpdate != null)
         {
             fetchedCartToUpdate.quantity = cart.quantity;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException<Error>(new Error("Cart with order ID " + cart.orderID + " and product ID " + cart.prodID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 29
0
 public void DeleteCart(string orderId, string prodId)
 {
     try
     {
         int orderIdToDelete = 0;
         int prodIdToDelete = 0;
         if (int.TryParse(orderId, out orderIdToDelete) && int.TryParse(prodId, out prodIdToDelete))
         {
             using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
             {
                 var fetchedCartToDelete = context.Carts.Where(o => o.orderID == orderIdToDelete && o.prodID == prodIdToDelete).FirstOrDefault();
                 if (fetchedCartToDelete != null)
                 {
                     context.Carts.Remove(fetchedCartToDelete);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new WebFaultException<Error>(new Error("Cart with order ID " + orderId + " and product ID " + prodId + " not found.", ""), System.Net.HttpStatusCode.NotFound);
                 }
             }
         }
     }
     catch (WebFaultException<Error> ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new WebFaultException<Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 30
0
        public Customer GetCustomer(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Customer> customerQuery = null;
                // Try parsing search string as every property of customer.
                int custId = 0;
                if (int.TryParse(search, out custId))
                {
                    customerQuery = context.Customers.Where(o => o.custID == custId);
                }

                var fetchedCustomer = customerQuery.FirstOrDefault();
                if (fetchedCustomer != null)
                {
                    return new Customer(fetchedCustomer);
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Customer not found", "Customer with ID " + search + " not found."), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 31
0
        public IList<Customer> GetCustomerList()
        {
            try
            {
                using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
                {
                    var customerList = context.Customers;
                    var customersToReturn = new List<Customer>();
                    foreach (var c in customerList)
                    {
                        customersToReturn.Add(new Customer(c));
                    }

                    return customersToReturn;
                }
            }
            catch (Exception ex)
            {
                throw new WebFaultException<Error>(new Error("Unexpected Exception", ex.Message), System.Net.HttpStatusCode.InternalServerError);
            }
        }
Exemplo n.º 32
0
        private IList<Cart> GetCartBy(int? orderId, int? prodId, int? quantity)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Cart> query = context.Carts;
                if (orderId != null)
                {
                    query = query.Where(o => o.orderID == orderId.Value);
                }
                if (prodId != null)
                {
                    query = query.Where(o => o.prodID == prodId.Value);
                }
                if (quantity != null)
                {
                    query = query.Where(o => o.quantity == quantity.Value);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List<Cart>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Cart(item));
                    }
                    return convertedList;
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Order not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 33
0
        private IList<Order> GetOrderBy(int? orderId, int? custId, string poNumber, DateTime? orderDate)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Order> query = context.Orders;
                if (orderId != null)
                {
                    query = query.Where(o => o.orderID == orderId.Value);
                }
                if (custId != null)
                {
                    query = query.Where(o => o.custID == custId.Value);
                }
                if (poNumber != null)
                {
                    query = query.Where(o => o.poNumber == poNumber);
                }
                if (orderDate != null)
                {
                    query = query.Where(o => o.orderDate == orderDate.Value);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List<Order>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Order(item));
                    }
                    return convertedList;
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Product not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 34
0
        private IList<Product> GetProductsBy(int? prodId, string prodName, double? price, double? prodWeight, bool? inStock)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Product> productQuery = context.Products;
                if (prodId != null)
                {
                    productQuery = productQuery.Where(o => o.prodID == prodId.Value);
                }
                if (prodName != null)
                {
                    productQuery = productQuery.Where(o => o.prodName == prodName);
                }
                if (price != null)
                {
                    productQuery = productQuery.Where(o => o.price == price.Value);
                }
                if (prodWeight != null)
                {
                    productQuery = productQuery.Where(o => o.prodWeight == prodWeight.Value);
                }
                if (inStock != null)
                {
                    productQuery = productQuery.Where(o => o.inStock == inStock.Value);
                }

                var fetchedList = productQuery.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List<Product>();
                    foreach(var item in fetchedList)
                    {
                        convertedList.Add(new Product(item));
                    }
                    return convertedList;
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Product not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 35
0
        public PurchaseOrder GetPurchaseOrder(string parameters)
        {
            var arguments = this.ParseArguments(parameters);

            if (arguments != null)
            {
                // Check if we can find at least 1 customer and order argument and does not have any other arguments that are not properties of customer and order.

                // arguments only contain customer and order arguments.
                var argumentsThatAreForCustomerOrOrder = arguments.Keys.Where(
                    key => key == Customer_custID ||
                    key == Customer_firstName ||
                    key == Customer_lastName ||
                    key == Order_custID ||
                    key == Order_orderDate ||
                    key == Order_orderID ||
                    key == Order_poNumber);
                if (argumentsThatAreForCustomerOrOrder.Count() == arguments.Count)
                {
                    // Check if we have at least one of both
                    if ((arguments.ContainsKey(Order_orderID) ||
                         arguments.ContainsKey(Order_custID) ||
                         arguments.ContainsKey(Order_poNumber) ||
                         arguments.ContainsKey(Order_orderDate))
                        &&
                        (arguments.ContainsKey(Customer_custID) ||
                         arguments.ContainsKey(Customer_firstName) ||
                         arguments.ContainsKey(Customer_lastName)))
                    {
                        var matchingCustomers = this.GetCustomerBy(
                            arguments.ContainsKey(Customer_custID) ? int.Parse(arguments[Customer_custID]) as int? : null,
                            arguments.ContainsKey(Customer_firstName) ? arguments[Customer_firstName] : null,
                            arguments.ContainsKey(Customer_lastName) ? arguments[Customer_lastName] : null,
                            arguments.ContainsKey(Customer_phoneNumber) ? arguments[Customer_phoneNumber] : null);

                        if (matchingCustomers.Count > 1)
                        {
                            throw new WebFaultException <Error>(new Error("Cannot get order for more then 1 customer.", ""), System.Net.HttpStatusCode.BadRequest);
                        }
                        else
                        {
                            var matchingCustomer     = matchingCustomers.First();
                            var ordersMadeByCustomer = this.GetOrderBy(
                                arguments.ContainsKey(Order_orderID) ? int.Parse(arguments[Order_orderID]) as int? : null,
                                matchingCustomer.custID,
                                arguments.ContainsKey(Order_poNumber) ? arguments[Order_poNumber] : null,
                                arguments.ContainsKey(Order_orderDate) ? DateTime.Parse(arguments[Order_orderDate]) as DateTime? : null);

                            if (ordersMadeByCustomer == null && ordersMadeByCustomer.Count == 0)
                            {
                                throw new WebFaultException <Error>(new Error("No such order.", ""), System.Net.HttpStatusCode.BadRequest);
                            }
                            else
                            {
                                var po = new PurchaseOrder()
                                {
                                    Customer        = matchingCustomer,
                                    Order           = ordersMadeByCustomer.First(),
                                    OrderedProducts = new List <OrderedProduct>()
                                };

                                // We need to get EF Model again because above order was not tracked by EF.
                                using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
                                {
                                    var order = context.Orders.Where(o => o.orderID == po.Order.orderID).First();
                                    foreach (var cart in order.Carts)
                                    {
                                        po.OrderedProducts.Add(new OrderedProduct()
                                        {
                                            Product  = new Product(cart.Product),
                                            quantity = cart.quantity
                                        });
                                    }
                                }

                                return(po);
                            }
                        }
                    }
                    else
                    {
                        throw new WebFaultException <Error>(new Error("Minimum 1 customer and 1 order argument required!", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <Error>(new Error("Only customer and order arguments allowed.", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest);
                }
            }
            else
            {
                throw new WebFaultException <Error>(new Error("Argument formatting invalid!", ""), System.Net.HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 36
0
 public void AddProduct(Product product)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         context.Products.Add(product.GenerateDbModel());
         context.SaveChanges();
     }
 }
Exemplo n.º 37
0
        public IList<Order> GetOrderList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedOrders = context.Orders;
                var orderList = new List<Order>();
                foreach (var o in fetchedOrders)
                {
                    orderList.Add(new Order(o));
                }

                return orderList;
            }
        }
Exemplo n.º 38
0
 public void UpdateProduct(Product product)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedProductToUpdate = context.Products.Where(o => o.prodID == product.prodID).FirstOrDefault();
         if (fetchedProductToUpdate != null)
         {
             fetchedProductToUpdate.prodName = product.prodName;
             fetchedProductToUpdate.price = product.price;
             fetchedProductToUpdate.prodWeight = product.prodWeight;
             fetchedProductToUpdate.inStock = product.inStock;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException<Error>(new Error("Product with ID " + product.prodID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 39
0
        public Order GetOrder(string search)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Order> orderQuery = null;
                // Try parsing search string as every property of customer.
                int prodId = 0;
                if (int.TryParse(search, out prodId))
                {
                    orderQuery = context.Orders.Where(o => o.orderID == prodId || o.custID == prodId);
                }
                else
                {
                    // TODO: orderQuery = context.Orders.Where(o => o. == search);
                }

                var fetchedOrder = orderQuery.FirstOrDefault();
                if (fetchedOrder != null)
                {
                    return new Order(fetchedOrder);
                }
                else
                {
                    return null;
                }
            }
        }
Exemplo n.º 40
0
 public void UpdateOrder(Order order)
 {
     using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
     {
         var fetchedOrderToUpdate = context.Orders.Where(o => o.orderID == order.orderID).FirstOrDefault();
         if (fetchedOrderToUpdate != null)
         {
             fetchedOrderToUpdate.custID = order.custID;
             fetchedOrderToUpdate.orderDate = order.orderDate;
             fetchedOrderToUpdate.poNumber = order.poNumber;
             context.SaveChanges();
         }
         else
         {
             throw new WebFaultException<Error>(new Error("Order with ID " + order.orderID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
         }
     }
 }
Exemplo n.º 41
0
        public IList<Customer> GetCustomerBy(int? custID, string firstName, string lastName, string phoneNumber)
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                IQueryable<Models.Customer> query = context.Customers;
                if (custID != null)
                {
                    query = query.Where(o => o.custID == custID.Value);
                }
                if (firstName != null)
                {
                    query = query.Where(o => o.firstName == firstName);
                }
                if (lastName != null)
                {
                    query = query.Where(o => o.lastName == lastName);
                }
                if (phoneNumber != null)
                {
                    query = query.Where(o => o.phoneNumber == phoneNumber);
                }

                var fetchedList = query.ToList();
                if (fetchedList != null && fetchedList.Count > 0)
                {
                    var convertedList = new List<Customer>();
                    foreach (var item in fetchedList)
                    {
                        convertedList.Add(new Customer(item));
                    }
                    return convertedList;
                }
                else
                {
                    throw new WebFaultException<Error>(new Error("Customer not found", ""), System.Net.HttpStatusCode.NotFound);
                }
            }
        }
Exemplo n.º 42
0
 public void UpdateCustomer(Customer customer)
 {
     try
     {
         using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
         {
             var fetchedCustomerToUpdate = context.Customers.Where(o => o.custID == customer.custID).FirstOrDefault();
             if (fetchedCustomerToUpdate != null)
             {
                 fetchedCustomerToUpdate.firstName = customer.firstName;
                 fetchedCustomerToUpdate.lastName = customer.lastName;
                 fetchedCustomerToUpdate.phoneNumber = customer.phoneNumber;
                 context.SaveChanges();
             }
             else
             {
                 throw new WebFaultException<Error>(new Error("Customer with ID " + customer.custID + " not found.", ""), System.Net.HttpStatusCode.NotFound);
             }
         }
     }
     catch (Exception ex)
     {
         throw new WebFaultException<Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 43
0
 public void DeleteOrder(string id)
 {
     try
     {
         int orderWithIdToDelete = 0;
         if (int.TryParse(id, out orderWithIdToDelete))
         {
             using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
             {
                 var fetchedOrderToDelete = context.Orders.Where(o => o.orderID == orderWithIdToDelete).FirstOrDefault();
                 if (fetchedOrderToDelete != null)
                 {
                     context.Orders.Remove(fetchedOrderToDelete);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new WebFaultException<Error>(new Error("Order with ID " + id + " not found.", ""), System.Net.HttpStatusCode.NotFound);
                 }
             }
         }
     }
     catch (WebFaultException<Error> ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         throw new WebFaultException<Error>(new Error("Unexpected exception!", ex.Message), System.Net.HttpStatusCode.InternalServerError);
     }
 }
Exemplo n.º 44
0
        public IList<Cart> GetCartList()
        {
            using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities())
            {
                var fetchedCarts = context.Carts;
                var cartList = new List<Cart>();
                foreach (var c in fetchedCarts)
                {
                    cartList.Add(new Cart(c));
                }

                return cartList;
            }
        }