public async Task Upload_NewBooksNotSelected_NoCalls() { var fileSource = Substitute.For <ISimpleDataSource>(); var bookfiles = new BookFilesInfo(MakeBook(null, "Titlt", "Author", "MOBI"), new BookFileInfo[] { new BookFileInfo { FileName = "filepath1.mobi" } }); fileSource.GetBookContent(Arg.Any <BookInfo>()).Returns(Task.FromResult(bookfiles)); var bookSource = Substitute.For <IFullDataSource>(); var messages = Substitute.For <IOutputMessage>(); var uploader = new Uploader(bookSource, fileSource, messages); MatchInfo[] books = new MatchInfo[] { new MatchInfo(MakeBook(null, "Titlt", "Author", "MOBI")) { IsSelected = false, Status = MatchStatus.NewBook, MatchedBook = MakeBook(null, "Titlt", "Author", "MOBI") } }; await uploader.Upload(books); ICall[] calls = bookSource.ReceivedCalls().ToArray(); Assert.AreEqual(0, calls.Length); }
async Task <UploadResults> UploadNewFiles(IEnumerable <MatchInfo> incoming) { IEnumerable <MatchInfo> toUpload = incoming .Where(b => b.Status == MatchStatus.NewFiles); int count = 0; int errors = 0; foreach (var match in toUpload) { BookFilesInfo content = await _incomingDataSource.GetBookContent(match.Book); bool ok = await SaveNewFiles(content, match); if (ok) { count++; } else { errors++; } } return(new UploadResults { Uploaded = count, Errors = errors }); }
void AssertBookFiles(BookFilesInfo files, string[] fileIds) { Assert.IsNull(files.Files); Assert.AreEqual(fileIds.Length, files.FileIds.Count()); var arrFileIds = files.FileIds.ToArray(); for (int i = 0; i < fileIds.Length; i++) { Assert.AreEqual(fileIds[i], arrFileIds[i]); } }
public void SetDateTimeAdded(BookFilesInfo book) { DateTime date; if (TryGetDateFromFilePath(book, out date)) { book.Book.DateAdded = date; } else { book.Book.DateAdded = _added; } }
bool TryGetDateFromFilePath(BookFilesInfo book, out DateTime date) { date = DateTime.MinValue; foreach (BookFileInfo file in book.Files) { if (TryGetDateFromFilePath(file, out date)) { return(true); } } return(false); }
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 <bool> UploadNewBook(MatchInfo match, DateAddedProvider dateAddedProvider) { try { BookFilesInfo content = await _incomingDataSource.GetBookContent(match.Book); dateAddedProvider.SetDateTimeAdded(content); await SaveBook(content); return(true); } catch (Exception ex) { _messages.WriteError(ex, "uploading {0}", match.Book.Title); return(false); } }
public async Task <BookFilesInfo> GetBookContent(BookInfo book) { try { IBookSqlDal repo = GetBookSqlDal(); BookInfo info = await repo.BookSelById(new Guid(book.Id)); IEnumerable <BookFileInfo> files = await repo.BookFileSelByBookId(new Guid(book.Id)); var content = new BookFilesInfo(info, files); return(content); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public async Task <BookFilesInfo> GetBookContent(BookInfo book) { if (!_lookup.ContainsKey(book.Id)) { return(null); } BookFilesInfo content = _lookup[book.Id]; var tasks = content.FileIds.Select(async id => await ReadBookFile(book, id)); BookFileInfo[] files = await Task.WhenAll(tasks); var html = new HtmlToText(); book.Description = html.Convert(book.Description); var contentWithBytes = new BookFilesInfo(book, files); return(contentWithBytes); }
async Task <bool> SaveNewFiles(BookFilesInfo book, MatchInfo match) { try { foreach (BookFileInfo file in book.Files) { if (match.NewTypes.Contains(file.FileType)) { file.BookId = match.MatchedBook.Id; await SaveBookFile(file); } } _messages.Write("Uploaded: {0}", book.Book.Title); return(true); } catch (Exception ex) { _messages.WriteError(ex, "uploading {0}", book.Book.Title); return(false); } }