예제 #1
0
        public IEnumerable <BookDto> Build(int numberOfBooks)
        {
            var fixture = new Fixture();

            if (Author == null && !_authors.Any())
            {
                _authors = _authorBuilder.WithLibrary(_libraryId).Build(_numberOfAuthors > 0 ? _numberOfAuthors : numberOfBooks).ToList();
            }

            Func <bool> isPublic = () => _isPublic ?? RandomData.Bool;

            _books = fixture.Build <BookDto>()
                     .With(b => b.LibraryId, _libraryId)
                     .With(b => b.ImageId, _hasImage ? RandomData.Number : 0)
                     .With(b => b.IsPublic, isPublic)
                     .With(b => b.Language, RandomData.Locale)
                     .With(b => b.SeriesIndex, _hasSeries ? RandomData.Number : (int?)null)
                     .With(b => b.DateAdded, RandomData.Date)
                     .With(b => b.DateUpdated, RandomData.Date)
                     .With(b => b.Status, Domain.Models.BookStatuses.Published)
                     .CreateMany(numberOfBooks)
                     .ToList();

            IEnumerable <CategoryDto> categories;

            if (_categoriesCount > 0 && !_categories.Any())
            {
                categories = _categoriesBuilder.WithLibrary(_libraryId).Build(_categoriesCount);
            }
            else
            {
                categories = _categories;
            }

            foreach (var book in _books)
            {
                if (_hasSeries && _series == null)
                {
                    var series = _seriesBuilder.WithLibrary(_libraryId).Build();
                    book.SeriesId = series.Id;
                }
                else
                {
                    book.SeriesId = _series?.Id;
                }

                FileDto bookImage = null;
                if (_hasImage)
                {
                    bookImage = fixture.Build <FileDto>()
                                .With(a => a.FilePath, RandomData.BlobUrl)
                                .With(a => a.IsPublic, true)
                                .Create();
                    _connection.AddFile(bookImage);

                    _files.Add(bookImage);
                    _fileStorage.SetupFileContents(bookImage.FilePath, RandomData.Bytes);
                    _connection.AddFile(bookImage);

                    book.ImageId = bookImage.Id;
                }
                else
                {
                    book.ImageId = null;
                }

                List <FileDto> files = null;
                if (_contentCount > 0)
                {
                    var mimeType = _contentMimeType ?? RandomData.MimeType;
                    files = fixture.Build <FileDto>()
                            .With(f => f.FilePath, RandomData.BlobUrl)
                            .With(f => f.IsPublic, false)
                            .With(f => f.MimeType, mimeType)
                            .With(f => f.FilePath, RandomData.BlobUrl)
                            .CreateMany(_contentCount)
                            .ToList();
                    _files.AddRange(files);
                    files.ForEach(f => _fileStorage.SetupFileContents(f.FilePath, RandomData.Bytes));
                    _connection.AddFiles(files);
                }

                _connection.AddBook(book);

                if (Author != null)
                {
                    _connection.AddBookAuthor(book.Id, Author.Id);
                }
                else
                {
                    foreach (var author in _authors)
                    {
                        _connection.AddBookAuthor(book.Id, author.Id);
                    }
                }

                if (categories != null && categories.Any())
                {
                    _connection.AddBookToCategories(book.Id, categories);
                }

                if (files != null)
                {
                    var contents = files.Select(f => new BookContentDto
                    {
                        BookId   = book.Id,
                        Language = _language ?? RandomData.NextLocale(),
                        FileId   = f.Id,
                        MimeType = f.MimeType,
                        FilePath = f.FilePath
                    }).ToList();
                    _connection.AddBookFiles(book.Id, contents);
                    _contents.AddRange(contents);
                }

                if (_pageCount > 0)
                {
                    var pages = new List <BookPageDto>();

                    for (int i = 0; i < _pageCount; i++)
                    {
                        FileDto pageImage = null;
                        if (_addPageImage)
                        {
                            pageImage = fixture.Build <FileDto>()
                                        .With(a => a.FilePath, RandomData.BlobUrl)
                                        .With(a => a.IsPublic, true)
                                        .Create();
                            _connection.AddFile(pageImage);

                            _files.Add(pageImage);
                            _fileStorage.SetupFileContents(pageImage.FilePath, RandomData.Bytes);
                            _connection.AddFile(pageImage);
                        }

                        pages.Add(fixture.Build <BookPageDto>()
                                  .With(p => p.BookId, book.Id)
                                  .With(p => p.SequenceNumber, i + 1)
                                  .With(p => p.Text, RandomData.Text)
                                  .With(p => p.ImageId, pageImage?.Id)
                                  .With(p => p.AccountId, (int?)null)
                                  .With(p => p.Status, EditingStatus.All)
                                  .Create());
                    }

                    if (_assignments.Any())
                    {
                        foreach (var assignment in _assignments)
                        {
                            var pagesToAssign = RandomData.PickRandom(pages.Where(p => p.AccountId == null), assignment.Value);
                            foreach (var pageToAssign in pagesToAssign)
                            {
                                pageToAssign.AccountId = assignment.Key;
                            }
                        }
                    }

                    if (_pageStatuses.Any())
                    {
                        foreach (var pageStatus in _pageStatuses)
                        {
                            var pagesToSetStatus = RandomData.PickRandom(pages.Where(p => p.Status == EditingStatus.All), pageStatus.Value);
                            foreach (var pageToSetStatus in pagesToSetStatus)
                            {
                                pageToSetStatus.Status = pageStatus.Key;
                            }
                        }
                    }

                    _connection.AddBookPages(pages);
                    _pages.AddRange(pages);
                }
            }

            if (_favoriteBooks.Any())
            {
                foreach (var f in _favoriteBooks)
                {
                    var booksToAddToFavorite = f.Count.HasValue ? _books.PickRandom(f.Count.Value) : _books;
                    _connection.AddBooksToFavorites(_libraryId, booksToAddToFavorite.Select(b => b.Id), f.AccountId);
                }
            }

            if (_readBooks.Any())
            {
                foreach (var r in _readBooks)
                {
                    var booksToAddToRecent = r.Count.HasValue ? _books.PickRandom(r.Count.Value) : _books;
                    foreach (var recentBook in booksToAddToRecent)
                    {
                        RecentBookDto recent = new RecentBookDto {
                            LibraryId = _libraryId, BookId = recentBook.Id, AccountId = r.AccountId, DateRead = RandomData.Date
                        };
                        _connection.AddBookToRecentReads(recent);
                        _recentBooks.Add(recent);
                    }
                }
            }

            return(_books);
        }