Example #1
0
        public void ExecuteOrder(int id, float stockValue)
        {
            using (StockServiceModelContainer database = new StockServiceModelContainer())
            {
                StockOrder order = database.StockOrders.Find(id);
                if (order == null)
                {
                    throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault(id));
                }

                try
                {
                    order.StockValue    = stockValue;
                    order.ExecutionDate = DateTime.UtcNow.ToString();
                    database.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new FaultException <StockServiceFault>(new StockServiceFault(e.Message));
                }

                StockServiceContracts.StockOrder updatedOrder = StockService.ToContractStockOrder(order);
                this.FireOnOrderStatusChange(updatedOrder);
            }
        }
Example #2
0
        public StockServiceContracts.StockOrder OrderStock(string company, int quantity, StockServiceContracts.StockOrder.OrderType type, string email)
        {
            Console.WriteLine("Received request!");

            using (StockServiceModelContainer database = new StockServiceModelContainer())
            {
                database.Database.Connection.Open();

                StockOrder toAdd = new StockOrder()
                {
                    Company     = company,
                    Quantity    = quantity,
                    Email       = email,
                    RequestDate = DateTime.UtcNow.ToString(),
                    Type        = this.GetOrderTypeFromEnum(type, database)
                };

                Console.WriteLine("Created Order!");

                try
                {
                    toAdd = database.StockOrders.Add(toAdd);
                    database.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new FaultException <StockServiceFault>(new StockServiceFault(e.Message));
                }

                StockServiceContracts.StockOrder createdOrder = StockService.ToContractStockOrder(toAdd);
                this.FireOnNewOrder(createdOrder);

                return(createdOrder);
            }
        }
Example #3
0
        public int RegisterOnOrderStatusChange(int id)
        {
            IStockServiceCallback callback = OperationContext.Current.GetCallbackChannel <IStockServiceCallback>();

            Console.WriteLine("Someone registered for an order status change!");

            using (StockServiceModelContainer database = new StockServiceModelContainer())
            {
                StockOrder order = database.StockOrders.Find(id);
                if (order == null)
                {
                    throw new FaultException <OrderNotFoundFault>(new OrderNotFoundFault(id));
                }

                int callbackId = Interlocked.Increment(ref this.LastCallbackId);
                this.CallbacksIds.Add(callbackId, callback);

                if (this.OnOrderStatusChangeCallbacks.ContainsKey(id) == false)
                {
                    this.OnOrderStatusChangeCallbacks[id] = new HashSet <IStockServiceCallback>();
                }

                this.OnOrderStatusChangeCallbacks[id].Add(callback);

                return(callbackId);
            }
        }
Example #4
0
 public IEnumerable <StockServiceContracts.StockOrder> GetAllOrders()
 {
     using (StockServiceModelContainer database = new StockServiceModelContainer())
     {
         return(database.StockOrders
                .Select(StockService.ToContractStockOrder)
                .ToList <StockServiceContracts.StockOrder>());
     }
 }
Example #5
0
 public IEnumerable <StockServiceContracts.StockOrder> GetClientOrders(string clientEmail)
 {
     using (StockServiceModelContainer database = new StockServiceModelContainer())
     {
         return(database.StockOrders
                .Where(s => s.Email == clientEmail)
                .Select(StockService.ToContractStockOrder)
                .ToList <StockServiceContracts.StockOrder>());
     }
 }
Example #6
0
 private OrderType GetOrderTypeFromEnum(StockServiceContracts.StockOrder.OrderType orderType, StockServiceModelContainer database)
 {
     if (orderType == StockServiceContracts.StockOrder.OrderType.Purchase)
     {
         return(database.OrderTypes.First(o => o.Name.Equals("Purchase")));
     }
     else
     {
         return(database.OrderTypes.First(o => o.Name.Equals("Sale")));
     }
 }