Ejemplo n.º 1
0
        /// <summary>
        /// Asycronously downloads and returns quotes for a given company <paramref name="tickerSymbol"/>, up to a max range.
        /// </summary>
        /// <param name="tickerSymbol">The symbol of the company to store the quotes for.</param>
        /// <param name="startDate">The beginning date of the day quotes that are returned.</param>
        /// <returns>Returns the downloaded quotes for the given company <paramref name="tickerSymbol"/>.</returns>
        public async Task <IEnumerable <Q> > GetDayQuotesForCompanyAsync(string tickerSymbol, DateTime startDate)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("MarketService", "The service has been disposed.");
            }

            if (string.IsNullOrEmpty(tickerSymbol))
            {
                throw new ArgumentNullException(nameof(tickerSymbol));
            }

            var currentDate = SystemTime.Now().Date;

            if (startDate > currentDate)
            {
                throw new ArgumentException("Invalid start date supplied. Start date cannot be greater than the current date.");
            }

            // Retrieve the existing company using the ticker symbol so each quote can be updated with it.
            var existingCompany = _companyService.FindCompany(tickerSymbol);

            return(await Task.Run(() =>
            {
                // Download quotes in chunks based off the date difference between the last stored quote date and today's date.
                var downloadedQuotes = startDate.AddDays(1) >= currentDate ? new[] { _downloader.DownloadPreviousDayQuote(tickerSymbol) } :
                startDate.AddDays(5) >= currentDate ? _downloader.DownloadQuotesFiveDays(tickerSymbol) :
                startDate.AddMonths(1) >= currentDate ? _downloader.DownloadQuotesOneMonth(tickerSymbol) :
                startDate.AddMonths(3) >= currentDate ? _downloader.DownloadQuotesThreeMonths(tickerSymbol) :
                startDate.AddMonths(5) >= currentDate ? _downloader.DownloadQuotesFiveMonths(tickerSymbol) :
                startDate.AddYears(1) >= currentDate ? _downloader.DownloadQuotesOneYear(tickerSymbol) :
                startDate.AddYears(2) >= currentDate ? _downloader.DownloadQuotesTwoYears(tickerSymbol) :
                _downloader.DownloadQuotesTwoYears(tickerSymbol);

                // Attach the company to the newly downloaded quotes if it exists.
                if (existingCompany != null)
                {
                    downloadedQuotes = downloadedQuotes.ToList().ForEach <Q>(q => { q.Company = existingCompany; q.CompanyId = existingCompany.Id; });
                }

                return downloadedQuotes.OrderBy(q => q.Date);
            }));
        }
 public void DownloadQuotesOneMonth_WithInvalidSymbol_ThrowsException()
 {
     // Act
     _downloader.DownloadQuotesOneMonth(null);
 }