private void MakeIsbnLookup() { _lookupIsbn = new Dictionary <string, BookInfo>(); _originalBooks .Where(b => !string.IsNullOrEmpty(b.Isbn)) .ToList() .ForEach(b => { string isbn = Isbn.Normalise(b.Isbn); if (!_lookupIsbn.ContainsKey(isbn)) { _lookupIsbn.Add(isbn, b); } }); }
async Task SaveBook(BookFilesInfo book) { BookInfo info = book.Book; if (!string.IsNullOrEmpty(info.Isbn)) { info.Isbn = Isbn.Normalise(info.Isbn); } await _originalDataSource.SaveBook(info); _messages.Write("Added: {0} - {1} [{2}]", book.Book.Title, book.Book.Author, book.Book.Isbn); foreach (var file in book.Files) { await SaveBookFile(file); } }
async Task <FindResultInfo> FindBook(BookInfo incoming) { if (incoming == null) { throw new ArgumentException("incoming not set"); } // 1. Find by ISBN if (!string.IsNullOrEmpty(incoming.Isbn)) { string isbn = Isbn.Normalise(incoming.Isbn); BookInfo book = await _originalDataSource.GetBookByIsbn(isbn); if (book != null) { return new FindResultInfo { Status = MatchStatus.UpToDate, Book = book } } ; } // 2. Title + Author if (!string.IsNullOrEmpty(incoming.Title) && !string.IsNullOrEmpty(incoming.Author)) { BookInfo book = await _originalDataSource.GetBookByTitleAuthor(incoming.Title, incoming.Author); if (book != null) { return new FindResultInfo { Status = MatchStatus.UpToDate, Book = book } } ; } return(new FindResultInfo { Book = null, Status = MatchStatus.NewBook }); } } }
BookInfo FindBook(BookInfo match, out MatchStatus status) { // Look by Isbn if (!string.IsNullOrEmpty(match.Isbn)) { string isbn = Isbn.Normalise(match.Isbn); if (_lookupIsbn.ContainsKey(isbn)) { status = MatchStatus.UpToDate; return(_lookupIsbn[isbn]); } } // Look by Title and then Author if (!string.IsNullOrEmpty(match.Title)) { string title = match.Title.ToLower(); if (_lookupTitle.ContainsKey(title)) { var group = _lookupTitle[title]; var books = group.ToArray(); foreach (var book in books) { if (string.Equals(title, book.Title, StringComparison.CurrentCultureIgnoreCase)) { status = MatchStatus.UpToDate; return(book); } } } } // Nothing matched it status = MatchStatus.NewBook; return(null); }