private IEnumerable <Order> GetOrders(Data.NorthwindContext context)
        {
            var orders = context.Orders.ToArray();
            var result = Mapper.Map <IEnumerable <Order> >(orders);

            return(result);
        }
        private void ApproveOrder(Data.NorthwindContext context, int orderId)
        {
            var instance = context.Orders.FirstOrDefault(e => e.OrderID == orderId);

            if (instance == null)
            {
                var message = string.Format(ErrorOrderNotFound, orderId);
                throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault {
                    OrderID = orderId
                }, message, new FaultCode("ApproveOrder"));
            }
            var currentOrder = Mapper.Map <Order>(instance);

            if (currentOrder.Status != OrderStatus.Draft)
            {
                var message = string.Format(ErrorOrderHasInvalidStatusForApprove, currentOrder.OrderID, currentOrder.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = currentOrder.OrderID, Status = currentOrder.Status
                }, message, new FaultCode("ApproveOrder"));
            }
            if (currentOrder.RequiredDate < DateTime.Today)
            {
                var message = string.Format(ErrorOrderHasExpiredOn, currentOrder.OrderID, currentOrder.RequiredDate);
                throw new FaultException <OrderHasExpiredFault>(new OrderHasExpiredFault {
                    OrderID = currentOrder.OrderID, RequiredDate = currentOrder.RequiredDate
                }, message, new FaultCode("ApproveOrder"));
            }

            instance.OrderDate = DateTime.Today;
            context.SaveChanges();
        }
        private void DeleteOrder(Data.NorthwindContext context, int orderId)
        {
            var instance = context.Orders.FirstOrDefault(e => e.OrderID == orderId);

            if (instance == null)
            {
                var message = string.Format(ErrorOrderNotFound, orderId);
                throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault {
                    OrderID = orderId
                }, message, new FaultCode("DeleteOrder"));
            }
            var currentOrder = Mapper.Map <Order>(instance);

            if (currentOrder.Status == OrderStatus.Completed)
            {
                var message = string.Format(ErrorOrderHasInvalidStatusForDelete, currentOrder.OrderID, currentOrder.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = currentOrder.OrderID, Status = currentOrder.Status
                }, message, new FaultCode("DeleteOrder"));
            }

            instance.Order_Details.Clear();
            context.Orders.Remove(instance);
            context.SaveChanges();
        }
        private int CreateOrder(Data.NorthwindContext context, Order order)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }
            if (order.Status != OrderStatus.Draft)
            {
                var message = string.Format(ErrorInvalidOrderStatusForCreate, order.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = order.OrderID, Status = order.Status
                }, message, new FaultCode("CreateOrder"));
            }

            var instance = context.Orders.Create();

            Mapper.Map(order, instance);
            instance.Customer = context.Customers.First(e => e.CustomerID == order.CustomerID);
            instance.Employee = context.Employees.First(e => e.EmployeeID == order.EmployeeID);
            instance.Order_Details.ToList().ForEach(e =>
            {
                e.Product = context.Products.First(p => p.ProductID == e.ProductID);
            });
            context.Orders.Add(instance);
            context.SaveChanges();

            return(instance.OrderID);
        }
        private Order GetOrder(Data.NorthwindContext context, int orderId)
        {
            var instance = context.Orders.FirstOrDefault(e => e.OrderID == orderId);

            if (instance == null)
            {
                var message = string.Format(ErrorOrderNotFound, orderId);
                throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault {
                    OrderID = orderId
                }, message, new FaultCode("GetOrder"));
            }

            var orderDetails = instance.Order_Details.ToArray();
            var result       = Mapper.Map <Order>(instance);

            Mapper.Map(orderDetails, result.OrderDetails);
            return(result);
        }
        private void UpdateOrder(Data.NorthwindContext context, Order order)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }
            if (order.Status != OrderStatus.Draft)
            {
                var message = string.Format(ErrorInvalidOrderStatusForUpdate, order.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = order.OrderID, Status = order.Status
                }, message, new FaultCode("UpdateOrder"));
            }

            var orderId  = order.OrderID;
            var instance = context.Orders.FirstOrDefault(e => e.OrderID == orderId);

            if (instance == null)
            {
                var message = string.Format(ErrorOrderNotFound, orderId);
                throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault {
                    OrderID = orderId
                }, message, new FaultCode("UpdateOrder"));
            }
            var currentOrder = Mapper.Map <Order>(instance);

            if (currentOrder.Status != OrderStatus.Draft)
            {
                var message = string.Format(ErrorOrderHasInvalidStatusForUpdate, currentOrder.OrderID, currentOrder.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = currentOrder.OrderID, Status = currentOrder.Status
                }, message, new FaultCode("UpdateOrder"));
            }

            Mapper.Map(order, instance);
            instance.Customer = context.Customers.First(e => e.CustomerID == order.CustomerID);
            instance.Employee = context.Employees.First(e => e.EmployeeID == order.EmployeeID);
            instance.Order_Details.ToList().ForEach(e =>
            {
                e.Product = context.Products.First(p => p.ProductID == e.ProductID);
            });
            context.SaveChanges();
        }
        private void CompleteOrder(Data.NorthwindContext context, int orderId)
        {
            var instance = context.Orders.FirstOrDefault(e => e.OrderID == orderId);

            if (instance == null)
            {
                var message = string.Format(ErrorOrderNotFound, orderId);
                throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault {
                    OrderID = orderId
                }, message, new FaultCode("CompleteOrder"));
            }
            var currentOrder = Mapper.Map <Order>(instance);

            if (currentOrder.Status != OrderStatus.InProgress)
            {
                var message = string.Format(ErrorOrderHasInvalidStatusForComplete, currentOrder.OrderID, currentOrder.Status);
                throw new FaultException <InvalidOrderStatusFault>(new InvalidOrderStatusFault {
                    OrderID = currentOrder.OrderID, Status = currentOrder.Status
                }, message, new FaultCode("CompleteOrder"));
            }

            instance.ShippedDate = DateTime.Today;
            context.SaveChanges();
        }