Exemple #1
0
        public async void AddBook(BookInputModel book)
        {
            var bookEntity = new Book
            {
                Title          = book.Title,
                Isbn           = book.Isbn,
                PublishingYear = book.PublishingYear ?? default(int),
                Type           = book.Type,
                Price          = book.Price ?? default(double),
                PublisherId    = book.PublisherId ?? default(int)
            };

            var bookId = _bookRepo.AddBook(bookEntity);

            var details = new BookDetails
            {
                BookId      = bookId,
                Description = book.Description,
                Font        = book.Font,
                PageCount   = book.PageCount ?? default(int),
                Length      = book.Length ?? default(int)
            };

            _bookRepo.AddBookDetails(details);

            foreach (var id in book.Author)
            {
                var AuthorConnection = new BookAuthorConnection
                {
                    BookId   = bookId,
                    AuthorId = id
                };

                _bookRepo.AddBookAuthorConnection(AuthorConnection);
            }

            foreach (var id in book.Genre)
            {
                var GenreConnection = new BookGenreConnection
                {
                    BookId  = bookId,
                    GenreId = id
                };
                _bookRepo.AddBookGenreConnection(GenreConnection);
            }

            using (var memoryStream = new MemoryStream())
            {
                await book.CoverImage.CopyToAsync(memoryStream);

                var img = new CoverImage
                {
                    BookId = bookId,
                    Img    = memoryStream.ToArray()
                };
                _bookRepo.AddImage(img);
            }
        }
Exemple #2
0
 public void RemoveBookGenreConnection(BookGenreConnection connection)
 {
     _db.Remove(connection);
     _db.SaveChanges();
 }
Exemple #3
0
 public void AddBookGenreConnection(BookGenreConnection connection)
 {
     _db.Add(connection);
     _db.SaveChanges();
 }
Exemple #4
0
        public async void ModifyBook(BookModifyInputModel book)
        {
            var bookEntity = new Book
            {
                Id             = book.BookId,
                Title          = book.Title,
                Isbn           = book.Isbn,
                PublishingYear = book.PublishingYear ?? default(int),
                Type           = book.Type,
                Price          = book.Price ?? default(double),
                PublisherId    = book.Publisher ?? default(int)
            };

            _bookRepo.ModBook(bookEntity);

            var details = new BookDetails
            {
                Id          = _bookRepo.GetDetailsId(book.BookId),
                BookId      = book.BookId,
                Description = book.Description,
                Font        = book.Font,
                PageCount   = book.PageCount ?? default(int),
                Length      = book.Length ?? default(int)
            };

            _bookRepo.ModBookDetails(details);

            var genreConnections = _bookRepo.GetBookGenreConnections(book.BookId);

            foreach (var g in genreConnections)
            {
                _bookRepo.RemoveBookGenreConnection(g);
            }

            foreach (var g in book.Genre)
            {
                var genreConnection = new BookGenreConnection
                {
                    BookId  = book.BookId,
                    GenreId = g
                };
                _bookRepo.AddBookGenreConnection(genreConnection);
            }

            var authorConnections = _bookRepo.GetBookAuthorConnections(book.BookId);

            foreach (var a in authorConnections)
            {
                _bookRepo.RemoveBookAuthorConnection(a);
            }

            foreach (var a in book.Author)
            {
                var authorConnection = new BookAuthorConnection
                {
                    BookId   = book.BookId,
                    AuthorId = a
                };
                _bookRepo.AddBookAuthorConnection(authorConnection);
            }

            if (book.NewCoverImage != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await book.NewCoverImage.CopyToAsync(memoryStream);

                    var img = new CoverImage
                    {
                        Id     = _bookRepo.GetCoverImageId(book.BookId),
                        BookId = book.BookId,
                        Img    = memoryStream.ToArray()
                    };
                    _bookRepo.ModImage(img);
                }
            }
        }