private async Task <IActionResult> PerformSearch(FindNotesViewModel findNotesViewModel) { ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, HttpContext.User.GetUserId()); if (user.EvernoteCredentials == null) { return(View("MustAuthoriseEvernote")); } if (ModelState.IsValid) { findNotesViewModel.SearchPerformed = true; IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials); try { ISearchResults searchResults = evernoteService.GetNotesMetaList( findNotesViewModel.SearchField, (Evernote.EDAM.Type.NoteSortOrder)findNotesViewModel.SortOrder, findNotesViewModel.SortAscending, findNotesViewModel.CurrentResultsPage, findNotesViewModel.PageSize); findNotesViewModel.NumberUnfilteredResults = searchResults.NotesMetadata.Count; if (findNotesViewModel.ExcludeShortNotes) { searchResults.NotesMetadata.RemoveAll(metadata => metadata.ContentLength < (1024 * 3)); } // now we populate with tags and notebook name foreach (INoteMetadata noteMetadata in searchResults.NotesMetadata) { if (noteMetadata.TagGuids != null) { List <TagData> tags = _dataAccess.GetCachedTagData(user.Id, noteMetadata.TagGuids); noteMetadata.TagNames = new List <string>(); foreach (string tagGuid in noteMetadata.TagGuids) { TagData tag = tags.FirstOrDefault(t => t.Guid == tagGuid); if (tag == null) { // save tag Tag evernoteTag = evernoteService.GetTag(tagGuid); tag = new TagData { Guid = tagGuid, Name = evernoteTag.Name, UserId = user.Id }; _dataAccess.SaveTag(tag); } noteMetadata.TagNames.Add(tag.Name); } } noteMetadata.NotebookName = _dataAccess.GetCachedNotebookName(user.Id, noteMetadata.NotebookGuid); if (noteMetadata.NotebookName == null) { Notebook notebook = evernoteService.GetNotebook(noteMetadata.NotebookGuid); noteMetadata.NotebookName = notebook.Name; NotebookData notebookData = new NotebookData() { UserId = user.Id, Guid = noteMetadata.NotebookGuid, Name = notebook.Name }; _dataAccess.SaveNotebook(notebookData); } } findNotesViewModel.SearchResults = searchResults.NotesMetadata.ConvertAll(noteMeta => new EverReaderNodeMetadataFormatter(noteMeta)); findNotesViewModel.TotalResultsForSearch = searchResults.TotalResults; } catch (EvernoteServiceSDK1AuthorisationException) { return(View("EvernoteAuthorisationError")); } } return(View("FindNotes", findNotesViewModel)); }
public async Task <IActionResult> Index(FindNotesViewModel findNotesViewModel) { return(await PerformSearch(findNotesViewModel)); }