/// <summary> /// Search for a bible passage in the cache /// </summary> /// <param name="searchCriteria"></param> /// <returns></returns> public async Task <SystemResponse <BiblePassage> > GetPassageFromCache(string searchCriteria) { // create an equality filter FilterDefinition <BiblePassage> filter = Builders <BiblePassage> .Filter.Eq(b => b.PassageRef, searchCriteria); // look in the DB IAsyncCursor <BiblePassage> result = await _biblePassageCollection.FindAsync(filter); BiblePassage passage = result.FirstOrDefault(); if (passage == null || passage == default(BiblePassage)) { return(new SystemResponse <BiblePassage>(true, $"Unable to find passage with PassageRef {searchCriteria}.")); } return(new SystemResponse <BiblePassage>(passage, "Success!")); }
/// <summary> /// Insert a new cache value for a specified passage /// </summary> /// <param name="searchCriteria"></param> /// <returns></returns> public async Task <BiblePassage> SetPassageForCache(BiblePassage passage) { // create an equality filter FilterDefinition <BiblePassage> filter = Builders <BiblePassage> .Filter.Eq(b => b.PassageRef, passage.PassageRef); // look in the DB IAsyncCursor <BiblePassage> result = await _biblePassageCollection.FindAsync(filter); BiblePassage found = result.FirstOrDefault(); // we should not find one if (found == null || found == default(BiblePassage)) { // save the item in the collection, then return once it's been assigned an _id await _biblePassageCollection.InsertOneAsync(passage); return(passage); } // however if we found one, return that one return(found); }
/// <summary> /// returns a list of all Passage Objets /// </summary> public async Task <SystemResponse <SermonPassageResponse> > GetSinglePassageForSearch(string searchCriteria) { if (string.IsNullOrEmpty(searchCriteria)) { return(new SystemResponse <SermonPassageResponse>(true, string.Format(SystemMessages.NullProperty, "searchCriteria"))); } if (searchCriteria.Length < 3 || searchCriteria.Length > 200) { return(new SystemResponse <SermonPassageResponse>(true, string.Format(SystemMessages.PropertyNameCharactersLengthRange, 3, 200))); } var response = new SermonPassageResponse(); // check the cache first and see if it's in there, before going to the ESV API var cacheResponse = await _passagesRepository.GetPassageFromCache(searchCriteria); if (!cacheResponse.HasErrors && cacheResponse.Result != null) { // if there were no errors then apply the cache result response.Passage = cacheResponse.Result.PassageText; return(new SystemResponse <SermonPassageResponse>(response, "Success!")); } // this just means that we didn't find it in the cache, so keep going normally // since ESV returns everything as one massive string, I need to convert everything to objects // Then to strings if I wish var getPassagesResponse = await _passagesRepository.GetPassagesForSearch(searchCriteria); if (getPassagesResponse == null) { return(new SystemResponse <SermonPassageResponse>(true, SystemMessages.ErrorWithESVApi)); } var passageResponse = getPassagesResponse.passages.FirstOrDefault(); if (passageResponse == null) { return(new SystemResponse <SermonPassageResponse>(true, SystemMessages.ErrorWithESVApi)); } var footerRemovalResponse = RemoveFooterFromResponse(passageResponse); var finalPassage = RemoveFooterTagsAndFormatVerseNumbers(footerRemovalResponse); // replace the canonical with what was requested finalPassage = finalPassage.Replace(string.Format("{0}\n\n", getPassagesResponse.canonical), ""); response.Passage = finalPassage; // store the result in the cache, but use a discard for the result, because we just need it to execute var biblePassage = new BiblePassage { CreateDate = DateTime.UtcNow, PassageRef = searchCriteria, PassageText = finalPassage }; _ = _passagesRepository.SetPassageForCache(biblePassage); return(new SystemResponse <SermonPassageResponse>(response, "Success!")); }