public List <Book> FilterBooks(Author input, int profileId)
        {
            var seriesLinks = input.Series.Value.SelectMany(x => x.LinkItems.Value)
                              .GroupBy(x => x.Book.Value)
                              .ToDictionary(x => x.Key, y => y.ToList());

            var dbAuthor = _authorService.FindById(input.ForeignAuthorId);

            var localBooks = new List <Book>();

            if (dbAuthor != null)
            {
                localBooks = _bookService.GetBooksByAuthorMetadataId(dbAuthor.AuthorMetadataId);
                var editions = _editionService.GetEditionsByAuthor(dbAuthor.Id).GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList());

                foreach (var book in localBooks)
                {
                    if (editions.TryGetValue(book.Id, out var bookEditions))
                    {
                        book.Editions = bookEditions;
                    }
                    else
                    {
                        book.Editions = new List <Edition>();
                    }
                }
            }

            var localFiles = _mediaFileService.GetFilesByAuthor(dbAuthor?.Id ?? 0);

            return(FilterBooks(input.Books.Value, localBooks, localFiles, seriesLinks, profileId));
        }
Exemple #2
0
        public Author GetAuthorAndBooks(string foreignAuthorId, double minPopularity = 0)
        {
            var author = GetAuthorInfo(foreignAuthorId);

            var bookList = GetAuthorBooks(foreignAuthorId, minPopularity);
            var books    = bookList.Select((x, i) =>
            {
                _logger.ProgressDebug($"{author}: Fetching book {i}/{bookList.Count}");
                return(GetBookInfo(x.Editions.Value.First().ForeignEditionId).Item2);
            }).ToList();

            var existingAuthor = _authorService.FindById(foreignAuthorId);

            if (existingAuthor != null)
            {
                var existingEditions = _editionService.GetEditionsByAuthor(existingAuthor.Id);
                var extraEditionIds  = existingEditions.Select(x => x.ForeignEditionId).Except(books.Select(x => x.Editions.Value.First().ForeignEditionId));

                _logger.Debug($"Getting data for extra editions {extraEditionIds.ConcatToString()}");
                var extraEditions = extraEditionIds.Select(x => GetBookInfo(x));

                var bookDict = books.ToDictionary(x => x.ForeignBookId);
                foreach (var edition in extraEditions)
                {
                    var b = edition.Item2;

                    if (bookDict.TryGetValue(b.ForeignBookId, out var book))
                    {
                        book.Editions.Value.Add(b.Editions.Value.First());
                    }
                    else
                    {
                        bookDict.Add(b.ForeignBookId, b);
                    }
                }

                books = bookDict.Values.ToList();
            }

            books.ForEach(x => x.AuthorMetadata = author.Metadata.Value);
            author.Books = books;

            author.Series = GetAuthorSeries(foreignAuthorId, author.Books);

            return(author);
        }
Exemple #3
0
        public List <BookResource> GetBooks([FromQuery] int?authorId,
                                            [FromQuery] List <int> bookIds,
                                            [FromQuery] string titleSlug,
                                            [FromQuery] bool includeAllAuthorBooks = false)
        {
            if (!authorId.HasValue && !bookIds.Any() && titleSlug.IsNullOrWhiteSpace())
            {
                var books = _bookService.GetAllBooks();

                var authors  = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId);
                var editions = _editionService.GetAllMonitoredEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList());

                foreach (var book in books)
                {
                    book.Author = authors[book.AuthorMetadataId];
                    if (editions.TryGetValue(book.Id, out var bookEditions))
                    {
                        book.Editions = bookEditions;
                    }
                    else
                    {
                        book.Editions = new List <Edition>();
                    }
                }

                return(MapToResource(books, false));
            }

            if (authorId.HasValue)
            {
                var books = _bookService.GetBooksByAuthor(authorId.Value);

                var author   = _authorService.GetAuthor(authorId.Value);
                var editions = _editionService.GetEditionsByAuthor(authorId.Value)
                               .GroupBy(x => x.BookId)
                               .ToDictionary(x => x.Key, y => y.ToList());

                foreach (var book in books)
                {
                    book.Author = author;
                    if (editions.TryGetValue(book.Id, out var bookEditions))
                    {
                        book.Editions = bookEditions;
                    }
                    else
                    {
                        book.Editions = new List <Edition>();
                    }
                }

                return(MapToResource(books, false));
            }

            if (titleSlug.IsNotNullOrWhiteSpace())
            {
                var book = _bookService.FindBySlug(titleSlug);

                if (book == null)
                {
                    return(MapToResource(new List <Book>(), false));
                }

                if (includeAllAuthorBooks)
                {
                    return(MapToResource(_bookService.GetBooksByAuthor(book.AuthorId), false));
                }
                else
                {
                    return(MapToResource(new List <Book> {
                        book
                    }, false));
                }
            }

            return(MapToResource(_bookService.GetBooks(bookIds), false));
        }
Exemple #4
0
        private List <BookResource> GetBooks()
        {
            var authorIdQuery = Request.Query.AuthorId;
            var bookIdsQuery  = Request.Query.BookIds;
            var slugQuery     = Request.Query.TitleSlug;
            var includeAllAuthorBooksQuery = Request.Query.IncludeAllAuthorBooks;

            if (!Request.Query.AuthorId.HasValue && !bookIdsQuery.HasValue && !slugQuery.HasValue)
            {
                var books = _bookService.GetAllBooks();

                var authors  = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId);
                var editions = _editionService.GetAllEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList());

                foreach (var book in books)
                {
                    book.Author = authors[book.AuthorMetadataId];
                    if (editions.TryGetValue(book.Id, out var bookEditions))
                    {
                        book.Editions = bookEditions;
                    }
                    else
                    {
                        book.Editions = new List <Edition>();
                    }
                }

                return(MapToResource(books, false));
            }

            if (authorIdQuery.HasValue)
            {
                int authorId = Convert.ToInt32(authorIdQuery.Value);
                var books    = _bookService.GetBooksByAuthor(authorId);

                var author   = _authorService.GetAuthor(authorId);
                var editions = _editionService.GetEditionsByAuthor(authorId)
                               .GroupBy(x => x.BookId)
                               .ToDictionary(x => x.Key, y => y.ToList());

                foreach (var book in books)
                {
                    book.Author = author;
                    if (editions.TryGetValue(book.Id, out var bookEditions))
                    {
                        book.Editions = bookEditions;
                    }
                    else
                    {
                        book.Editions = new List <Edition>();
                    }
                }

                return(MapToResource(books, false));
            }

            if (slugQuery.HasValue)
            {
                string titleSlug = slugQuery.Value.ToString();

                var book = _bookService.FindBySlug(titleSlug);

                if (book == null)
                {
                    return(MapToResource(new List <Book>(), false));
                }

                if (includeAllAuthorBooksQuery.HasValue && Convert.ToBoolean(includeAllAuthorBooksQuery.Value))
                {
                    return(MapToResource(_bookService.GetBooksByAuthor(book.AuthorId), false));
                }
                else
                {
                    return(MapToResource(new List <Book> {
                        book
                    }, false));
                }
            }

            string bookIdsValue = bookIdsQuery.Value.ToString();

            var bookIds = bookIdsValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                          .Select(e => Convert.ToInt32(e))
                          .ToList();

            return(MapToResource(_bookService.GetBooks(bookIds), false));
        }