示例#1
0
        private Book EnsureBookAdded(List <ImportDecision <LocalBook> > decisions, List <Book> addedBooks)
        {
            var book = decisions.First().Item.Book;

            if (book.Id == 0)
            {
                var dbBook = _bookService.FindById(book.ForeignBookId);

                if (dbBook == null)
                {
                    _logger.Debug($"Adding remote book {book}");

                    if (book.AuthorMetadataId == 0)
                    {
                        throw new InvalidOperationException("Cannot insert book with AuthorMetadataId = 0");
                    }

                    try
                    {
                        book.Monitored = book.Author.Value.Monitored;
                        book.Added     = DateTime.UtcNow;
                        _bookService.InsertMany(new List <Book> {
                            book
                        });
                        addedBooks.Add(book);

                        book.Editions.Value.ForEach(x => x.BookId = book.Id);
                        _editionService.InsertMany(book.Editions.Value);

                        dbBook = _bookService.FindById(book.ForeignBookId);
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "Failed to add book {0}", book);
                        RejectBook(decisions);

                        return(null);
                    }
                }

                var edition = dbBook.Editions.Value.ExclusiveOrDefault(x => x.ForeignEditionId == decisions.First().Item.Edition.ForeignEditionId);
                if (edition == null)
                {
                    RejectBook(decisions);
                    return(null);
                }

                // Populate the new DB book
                foreach (var decision in decisions)
                {
                    decision.Item.Book    = dbBook;
                    decision.Item.Edition = edition;
                }

                book = dbBook;
            }

            return(book);
        }
示例#2
0
        protected override bool RefreshChildren(SortedChildren localChildren, List <Edition> remoteChildren, Author remoteData, bool forceChildRefresh, bool forceUpdateFileTags, DateTime?lastUpdate)
        {
            // make sure only one of the releases ends up monitored
            MonitorSingleEdition(localChildren);

            localChildren.All.ForEach(x => _logger.Trace($"release: {x} monitored: {x.Monitored}"));

            _editionService.InsertMany(localChildren.Added);

            return(_refreshEditionService.RefreshEditionInfo(localChildren.Added, localChildren.Updated, localChildren.Merged, localChildren.Deleted, localChildren.UpToDate, remoteChildren, forceUpdateFileTags));
        }
示例#3
0
        private Book EnsureBookAdded(List <ImportDecision <LocalBook> > decisions)
        {
            var book = decisions.First().Item.Book;

            if (book.Id == 0)
            {
                var dbBook = _bookService.FindById(book.ForeignBookId);

                if (dbBook == null)
                {
                    _logger.Debug($"Adding remote book {book}");
                    try
                    {
                        book.Added = DateTime.UtcNow;
                        _bookService.InsertMany(new List <Book> {
                            book
                        });

                        book.Editions.Value.ForEach(x => x.BookId = book.Id);
                        _editionService.InsertMany(book.Editions.Value);

                        dbBook = _bookService.FindById(book.ForeignBookId);
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "Failed to add book {0}", book);
                        RejectAlbum(decisions);

                        return(null);
                    }
                }

                var edition = dbBook.Editions.Value.ExclusiveOrDefault(x => x.ForeignEditionId == decisions.First().Item.Edition.ForeignEditionId);
                if (edition == null)
                {
                    RejectAlbum(decisions);
                    return(null);
                }

                // Populate the new DB book
                foreach (var decision in decisions)
                {
                    decision.Item.Book    = dbBook;
                    decision.Item.Edition = edition;
                }
            }

            return(book);
        }
示例#4
0
        public Book AddBook(Book newBook, bool doRefresh = true)
        {
            _bookRepository.Upsert(newBook);

            var editions = newBook.Editions.Value;

            editions.ForEach(x => x.BookId = newBook.Id);

            _editionService.InsertMany(editions.Where(x => x.Id == 0).ToList());
            _editionService.SetMonitored(editions.FirstOrDefault(x => x.Monitored) ?? editions.First());

            _eventAggregator.PublishEvent(new BookAddedEvent(GetBook(newBook.Id), doRefresh));

            return(newBook);
        }
示例#5
0
        public Book AddBook(Book newBook, bool doRefresh = true)
        {
            if (newBook.AuthorMetadataId == 0)
            {
                throw new InvalidOperationException("Cannot insert book with AuthorMetadataId = 0");
            }

            _bookRepository.Upsert(newBook);

            var editions = newBook.Editions.Value;

            editions.ForEach(x => x.BookId = newBook.Id);

            _editionService.InsertMany(editions.Where(x => x.Id == 0).ToList());
            _editionService.SetMonitored(editions.FirstOrDefault(x => x.Monitored) ?? editions.First());

            _eventAggregator.PublishEvent(new BookAddedEvent(GetBook(newBook.Id), doRefresh));

            return(newBook);
        }