Example #1
0
        public OperationResult <Tuple <string, SubscriberOrders[]> > GetSubscriberOpenedOrders(long subscriberId)
        {
            Logger.Trace("Get subscriber {0} opened orders", subscriberId);
            return(InvokeOperations.InvokeOperation(() =>
            {
                var ordersDictionary = new Dictionary <long, List <Trade> >();
                var providers = signalServiceRepository.GetProvidersBySubscriber(subscriberId);
                var subscriber = signalServiceRepository.GetSubscriber(subscriberId);

                var orders = accService.GetOpenOrders(subscriberId);
                if (!orders.IsSuccess)
                {
                    throw new OperationException(orders.Error, orders.Code);
                }

                foreach (var order in orders.Result)
                {
                    var providerId = TradeProvider(order.Comment);

                    if (ordersDictionary.ContainsKey(providerId))
                    {
                        ordersDictionary[providerId].Add(order);
                    }
                    else
                    {
                        ordersDictionary.Add(providerId, new List <Trade> {
                            order
                        });
                    }
                }

                return new Tuple <string, SubscriberOrders[]>(subscriber.currency, ordersDictionary.Select(order => new SubscriberOrders
                {
                    MasterId = order.Key != 0 ? order.Key : subscriber.id,
                    MasterAvatar = order.Key != 0 ? providers.First(x => x.id == order.Key).avatar : subscriber.avatar,
                    MasterNickname = order.Key != 0 ? providers.First(x => x.id == order.Key).nickname : subscriber.nickname,
                    TotalProfit = MathHelper.FairRound(order.Value.Sum(x => x.CurrentProfit)),
                    OpenedOrders = order.Value
                }).ToArray());
            }));
        }