Exemple #1
0
        public async Task <Book> AddBook(BookDto bookDto)
        {
            if (!await _bookRepository.BookExists(bookDto.GoogleBookId))
            {
                List <Author> authors = await GetAuthors(bookDto.Authors);

                List <Category> categoriesToAdd = await GetCategories(bookDto.Categories);

                // todo : use auto mapper
                var bookToCreate = new Book()
                {
                    Title        = bookDto.Title,
                    Publisher    = bookDto.Publisher,
                    ImageLink    = bookDto.ImageLink,
                    Description  = bookDto.Description,
                    GoogleBookId = bookDto.GoogleBookId,
                    PageCount    = bookDto.PageCount,
                };

                foreach (var category in categoriesToAdd)
                {
                    bookToCreate.Categories.Add(new BookCategory()
                    {
                        Book = bookToCreate, Category = category
                    });
                }

                foreach (var author in authors)
                {
                    bookToCreate.BookAuthors.Add(new BookAuthor()
                    {
                        Book = bookToCreate, Author = author
                    });
                }

                var createdBook = await _bookRepository.AddBook(bookToCreate, bookDto.UserId, bookDto.BookshelfId);

                return(createdBook);
            }
            else
            {
                return(await _bookRepository.AddExistingBook(bookDto.GoogleBookId, bookDto.UserId, bookDto.BookshelfId));
            }
        }