private async Task <QuoteCubeCollection> GetCollectionOfQuotesFromContext(OptionsCollectionOfQuotes optionsCollectionOfQuotes)
        {
            QuoteCubeCollection quoteCubeCollection = new QuoteCubeCollection
            {
                quoteCubes = await SimpleCollectionOfQuoteCubesFromContext(optionsCollectionOfQuotes)
            };

            quoteCubeCollection.numberOfQuotes = quoteCubeCollection.quoteCubes.Count;

            return(quoteCubeCollection);
        }
        public async Task <ActionResult <QuoteCubeCollection> > GetAllQuotes()
        {
            OptionsCollectionOfQuotes optionsCollectionOfQuotes = new OptionsCollectionOfQuotes();
            IQuoteCubeCollection      quoteCubeCollection       = new ReturnQuoteCubeCollection();

            //set up collection options.
            optionsCollectionOfQuotes.enumSourceOfData = EnumSourceOfData.DataBaseInContext;
            optionsCollectionOfQuotes.bloggingContext  = _context;

            var tempCubeCollection = await quoteCubeCollection.GetQuoteCubeCollection(optionsCollectionOfQuotes);

            var generalAPIResponse = new GeneralAPIResponse
            {
                dateTimeOfResponse = DateTime.Now
            };

            tempCubeCollection.generalAPIResponse = generalAPIResponse;

            return(tempCubeCollection);
        }
        public async Task <QuoteCubeCollection> GetQuoteCubeCollection(OptionsCollectionOfQuotes optionsCollectionOfQuotes)
        {
            QuoteCubeCollection quoteCubeCollection = new QuoteCubeCollection();

            if (optionsCollectionOfQuotes.enumSourceOfData == Enums.EnumSourceOfData.DataBaseInMemory)
            {
                quoteCubeCollection = await GetCollectionOfQuotesFromMemory();
            }
            else if (optionsCollectionOfQuotes.enumSourceOfData == Enums.EnumSourceOfData.DataBaseInContext)
            {
                quoteCubeCollection = await GetCollectionOfQuotesFromContext(optionsCollectionOfQuotes);
            }
            else
            {
                quoteCubeCollection.DetailsAboutOperation = "optionsSingleQuote contains unknown data source.";
                quoteCubeCollection.OperationSuccessful   = false;
            }

            return(quoteCubeCollection);
        }
        private async Task <QuoteCube> ReturnQuoteFromDatabase(OptionsSingleQuote optionsSingleQuote)
        {
            QuoteCube quoteCube = new QuoteCube();
            OptionsCollectionOfQuotes optionsCollectionOfQuotes = new OptionsCollectionOfQuotes();
            IQuoteCubeCollection      quoteCubeCollection       = new ReturnQuoteCubeCollection();

            //now, look at options, if it is random, pick any one.
            //TODO - both the if and else have some common statements.
            //perhaps we can merge and keep only the unique things.
            if (optionsSingleQuote.RandomQuote == true)
            {
                //set up collection options.

                optionsCollectionOfQuotes.enumSourceOfData = optionsSingleQuote.enumSourceOfData;
                optionsCollectionOfQuotes.bloggingContext  = optionsSingleQuote.bloggingContext;

                var TempCubeCollection = await quoteCubeCollection.GetQuoteCubeCollection(optionsCollectionOfQuotes);

                if (TempCubeCollection.OperationSuccessful == false)
                {
                    quoteCube.DetailsAboutOperation = "There was a problem getting the source collection";
                    quoteCube.OperationSuccessful   = false;
                }
                else
                {
                    //our collection is good.
                    try
                    {
                        //lets pick a random quote.
                        // Instantiate random number generator using system-supplied value as seed.
                        var rand = new Random();
                        var quoteRandomNumber = rand.Next(TempCubeCollection.numberOfQuotes);
                        quoteCube = TempCubeCollection.quoteCubes[quoteRandomNumber];
                    }
                    catch (Exception e)
                    {
                        quoteCube.DetailsAboutOperation = "Exception Error " + e.ToString();
                        quoteCube.OperationSuccessful   = false;
                    }
                }
            }
            //if it is specific, see if it is there in the list.
            else
            {
                if (String.IsNullOrEmpty(optionsSingleQuote.QuoteIdentifierCompadre) == true)
                {
                    quoteCube.DetailsAboutOperation = "QuoteIdentifierCompadre is missing or empty";
                    quoteCube.OperationSuccessful   = false;
                }
                else
                {
                    //set up collection options.

                    optionsCollectionOfQuotes.enumSourceOfData = optionsSingleQuote.enumSourceOfData;
                    optionsCollectionOfQuotes.bloggingContext  = optionsSingleQuote.bloggingContext;

                    var TempCubeCollection = await quoteCubeCollection.GetQuoteCubeCollection(optionsCollectionOfQuotes);

                    if (TempCubeCollection.OperationSuccessful == false)
                    {
                        quoteCube.DetailsAboutOperation = "There was a problem getting the source collection";
                        quoteCube.OperationSuccessful   = false;
                    }
                    else
                    {
                        //our collection is good.
                        try
                        {
                            //lets pick a specific quote
                            var tempquoteCube = TempCubeCollection.quoteCubes.Select(x => x).Where(x => x.QuoteIdentifierCompadre == optionsSingleQuote.QuoteIdentifierCompadre).First();
                            if (tempquoteCube == null)
                            {
                                quoteCube.DetailsAboutOperation = "No quote with " + optionsSingleQuote.QuoteIdentifierCompadre + "exists in our system";
                                quoteCube.OperationSuccessful   = false;
                            }
                            else
                            {
                                quoteCube = tempquoteCube;
                            }
                        }
                        catch (Exception e)
                        {
                            quoteCube.DetailsAboutOperation = "Exception Error " + e.ToString();
                            quoteCube.OperationSuccessful   = false;
                        }
                    }
                }
            }

            return(quoteCube);
        }
        private async Task <List <QuoteCube> > SimpleCollectionOfQuoteCubesFromContext(OptionsCollectionOfQuotes optionsCollectionOfQuotes)
        {
            //at this point, I am assuming that the context is already checked for
            //dude, we cannot just send the entire 1000s of quotes.
            var limitOfQuotes    = 100;
            var tempListOriginal = optionsCollectionOfQuotes.bloggingContext.QuoteModels.ToList().Take(limitOfQuotes);

            var tempList = new List <QuoteCube>();

            //the database tables use the schema tables
            //my API uses JSON classes that are separate from the schema classes
            //this is the conversion happening
            //TODO - can we move this to a interface + class that does the conversion?
            foreach (var x in tempListOriginal)
            {
                var tempQuoteCube = new QuoteCube
                {
                    QuoteContent            = x.QuoteContent,
                    QuoteIdentifierCompadre = x.QuoteId.ToString(),
                    QuoteAuthor             = x.QuoteAuthor,
                    QuoteIdentifierString   = x.QuoteIdentifierString
                };

                var generalAPIResponse = new GeneralAPIResponse
                {
                    dateTimeOfResponse = DateTime.Now
                };
                tempQuoteCube.generalAPIResponse = generalAPIResponse;

                tempList.Add(tempQuoteCube);
            }

            //here, adding this await because, I am using an async Task based interface.
            //this being a memory based implementation, it does not actually have to wait for anything.
            //so, just wrapping this simple result in a task to avoid getting 'lack of async' error.
            return(await Task.FromResult(tempList));
        }