//this one returns a single quote. //the type of quote is indicate in the options parameters //public async Task DoOperationAsync() public async Task <QuoteCube> ReturnASingleQuote(OptionsSingleQuote optionsSingleQuote) { QuoteCube quoteCube = new QuoteCube(); if (optionsSingleQuote.enumSourceOfData == Enums.EnumSourceOfData.DataBaseInMemory) { //get quote from memory. quoteCube = await ReturnQuoteFromMemory(optionsSingleQuote); } else if (optionsSingleQuote.enumSourceOfData == Enums.EnumSourceOfData.DataBaseInContext) { if (optionsSingleQuote.bloggingContext == null) { quoteCube.DetailsAboutOperation = "db context is null"; quoteCube.OperationSuccessful = false; return(quoteCube); } //get quote from database context quoteCube = await ReturnQuoteFromDatabase(optionsSingleQuote); } else { quoteCube.DetailsAboutOperation = "optionsSingleQuote contains unknown data source."; quoteCube.OperationSuccessful = false; } return(quoteCube); }
public async Task <ActionResult <QuoteCube> > GetASingleQuoteAsync() { var tempItemViewModel = new QuoteCube(); var tempQuoteHelpers = new QuoteHelpers(); //use this to return a quote from memory, when database is not there //tempItemViewModel = tempQuoteHelpers.ReturnASingleQuoteInMemory(); tempItemViewModel = await tempQuoteHelpers.ReturnASingleQuoteAsync(_context); return(tempItemViewModel); }
private async Task <List <QuoteCube> > SimpleCollectionOfQuoteCubes() { var tempList = new List <QuoteCube>(); var tempQuote1 = new QuoteCube { QuoteIdentifierString = "1", QuoteContent = "I'm in a glass case of emotion", QuoteAuthor = "Ron" }; var tempQuote2 = new QuoteCube { QuoteIdentifierString = "2", QuoteContent = "Everyone just relax, all right? Believe me, if there's one thing Ron Burgundy knows, it's women", QuoteAuthor = "Ron" }; var tempQuote3 = new QuoteCube { QuoteIdentifierString = "3", QuoteContent = "Good evening, San Diego. I'm Veronica Corningstone. T**s McGee is on vacation", QuoteAuthor = "Veronica" }; var tempQuote4 = new QuoteCube { QuoteIdentifierString = "4", QuoteContent = "I have many leather-bound books and my apartment smells of rich mahogany", QuoteAuthor = "Ron" }; var tempQuote5 = new QuoteCube { QuoteIdentifierString = "5", QuoteContent = "By the beard of Zeus!", QuoteAuthor = "Ron" }; tempList.Add(tempQuote1); tempList.Add(tempQuote2); tempList.Add(tempQuote3); tempList.Add(tempQuote4); tempList.Add(tempQuote5); //the 5 items above is important for some testing. so, if you want to modify this list, DONT. //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)); }
public async Task TestIReturnSingleQuoteRandomAsync() { IReturnSingleQuote returnSingleQuote = new ReturnSingleQuote(); OptionsSingleQuote optionsSingleQuote = new OptionsSingleQuote(); //settign options //source is memory. optionsSingleQuote.enumSourceOfData = RandomStuffGeneratorPrivate.Enums.EnumSourceOfData.DataBaseInMemory; //quote is random optionsSingleQuote.RandomQuote = true; QuoteCube quoteCube = await returnSingleQuote.ReturnASingleQuote(optionsSingleQuote); var actual = quoteCube.OperationSuccessful; var expected = true; Assert.Equal(expected, actual); }
public async Task TestIReturnSingleQuoteRandomKnownIdentifierAsync() { IReturnSingleQuote returnSingleQuote = new ReturnSingleQuote(); OptionsSingleQuote optionsSingleQuote = new OptionsSingleQuote(); //settign options //source is memory. optionsSingleQuote.enumSourceOfData = RandomStuffGeneratorPrivate.Enums.EnumSourceOfData.DataBaseInMemory; //quote is specific optionsSingleQuote.RandomQuote = false; //provide identifier optionsSingleQuote.QuoteIdentifierCompadre = "1"; QuoteCube quoteCube = await returnSingleQuote.ReturnASingleQuote(optionsSingleQuote); var actual = quoteCube.OperationSuccessful; var expected = true; Assert.Equal(expected, actual); }
public async Task <ActionResult <QuoteCube> > GetASingleQuoteAsync() { var tempQuoteCube = new QuoteCube(); IReturnSingleQuote returnSingleQuote = new ReturnSingleQuote(); OptionsSingleQuote optionsSingleQuote = new OptionsSingleQuote(); //setting options optionsSingleQuote.enumSourceOfData = EnumSourceOfData.DataBaseInContext; //quote is random optionsSingleQuote.RandomQuote = true; //set the context. optionsSingleQuote.bloggingContext = _context; tempQuoteCube = await returnSingleQuote.ReturnASingleQuote(optionsSingleQuote); //add the general response var generalAPIResponse = new GeneralAPIResponse(); generalAPIResponse.dateTimeOfResponse = DateTime.Now; tempQuoteCube.generalAPIResponse = generalAPIResponse; return(tempQuoteCube); }
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)); }
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); }