Ejemplo n.º 1
0
        public List <PriceModel> GetLastPrices()
        {
            var lastPrices = _priceRepository.FindPrices()
                             .GroupBy(x => x.SymbolId)
                             .Select(x => x.OrderByDescending(y => y.Date).Last())
                             .Select(x => new PriceModel()
            {
                SymbolId = x.SymbolId,
                Price    = x.Price,
                Date     = x.Date
            })
                             .ToList();

            var symbols = _symbolManager.GetSymbols();

            foreach (var symbol in symbols)
            {
                var lastPrice = lastPrices.FirstOrDefault(x => x.SymbolId == symbol.Id);

                if (lastPrice == null)
                {
                    string   symbolId = symbol.Id;
                    decimal  price    = GetLatestPrice(symbol);
                    DateTime date     = DateTime.Now;

                    lastPrices.Add(new PriceModel()
                    {
                        SymbolId = symbolId,
                        Price    = price,
                        Date     = date
                    });

                    SaveLastPrice(symbolId, price, 0, DateTime.Now, date);
                }
            }

            return(lastPrices);
        }
Ejemplo n.º 2
0
        public async Task ExecuteAsync(BotUpdateInput input)
        {
            string message = input.Message.Text.Trim();

            message = message.Replace((message + " ").Split(' ')[0], string.Empty).Trim();

            List <string> values = message.Contains(" ") ? message.Split(' ').ToList() : new List <string>()
            {
                message
            };

            UserEntity user = _userRepository.FindUser(input.Message.Chat.Id.ToString());

            if (user == null)
            {
                await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Unauthorized!");

                return;
            }

            if (values[0] == "get")
            {
                if (values.Count < 3)
                {
                    await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Invalid arguments!");

                    return;
                }

                DateTime startDate = DateTime.Now;

                if (values[2].EndsWith("h"))
                {
                    int interval;

                    if (!int.TryParse(values[2].Replace("h", string.Empty), out interval))
                    {
                        await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Invalid arguments!");

                        return;
                    }

                    startDate = DateTime.Now.AddHours(-1 * interval);
                }
                else if (values[2].EndsWith("m"))
                {
                    int interval;

                    if (!int.TryParse(values[2].Replace("m", string.Empty), out interval))
                    {
                        await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Invalid arguments!");

                        return;
                    }

                    startDate = DateTime.Now.AddMinutes(-1 * interval);
                }

                IEnumerable <PriceEntity> prices;

                if (values[1] == "all")
                {
                    prices = _priceRepository.FindPrices(startDate).OrderBy(x => x.Date);
                }
                else
                {
                    var symbol = _symbolRepository.FindSymbols().FirstOrDefault(x => x.Name == values[1].ToUpperInvariant() || x.FriendlyName == values[1].ToUpperInvariant());

                    if (symbol == null)
                    {
                        await _messageService.SendMessageAsync(user.ChatId, "Symbol is not found!");

                        return;
                    }

                    prices = _priceRepository.FindPrices(symbol.Id, startDate).OrderBy(x => x.Date);
                }

                List <string> lines = new List <string>();

                foreach (var item in prices)
                {
                    var sym = _symbolRepository.FindSymbols().FirstOrDefault(x => x.Id == item.SymbolId);

                    lines.Add($"{item.Date.AddHours(3).ToString("dd.MM.yyyy HH:mm:ss")} | {sym.FriendlyName}: {item.Price} {string.Format("[{0}%{1}]", (item.Change * 100).ToString("+0.00;-0.00;0"), GetTimeSpanString(DateTime.Now - item.LastChangeDate))}");
                }

                if (!lines.Any())
                {
                    await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "There are no prices!");

                    return;
                }

                lines = lines.OrderBy(x => x).ToList();

                await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), string.Join("\r\n", lines));

                return;
            }
            else
            {
                await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Invalid arguments!");

                return;
            }
        }