示例#1
0
        /// <summary>
        /// Dodaj nową książkę do bazy danych.
        /// </summary>
        /// <param name="title">Tytuł ksiązki</param>
        /// <param name="purchaseDate">Data zakupu ksiązki</param>
        /// <param name="genere">Gatunek</param>
        /// <param name="shelf">Półka, na której znajduje się książka</param>
        /// <param name="authors">Lista autorów książki</param>
        public void AddBook(string title, DateTime purchaseDate, Genere genere, Shelf shelf, List <Author> authors)
        {
            title = title.Trim();

            if (title == null || title == "" || purchaseDate == null || genere == null || shelf == null || authors == null || authors.Count == 0)
            {
                throw new ArgumentNullException();
            }

            if (purchaseDate > DateTime.Now)
            {
                throw new ArgumentOutOfRangeException();
            }

            Book bookToAdd = new Book()
            {
                Title        = title,
                PurchaseDate = purchaseDate,
                GenereId     = genere.GenereId,
                ShelfId      = shelf.ShelfId
            };

            using (BookshelfContext context = new BookshelfContext())
            {
                context.Add(bookToAdd);
                context.SaveChanges();
            }

            foreach (Author author in authors)
            {
                AddBookAuthor(bookToAdd, author);
            }
        }
示例#2
0
        /// <summary>
        /// Dodaje istniejącego w bazie autora do istniejącej bazie ksiązki.
        /// </summary>
        /// <param name="book">Ksiązka, do której ma zostać dodany autor</param>
        /// <param name="author">Autor, który ma zostać przypisany do danej książki</param>
        public void AddBookAuthor(Book book, Author author)
        {
            if (book == null || author == null)
            {
                throw new ArgumentNullException();
            }

            using (BookshelfContext context = new BookshelfContext())
            {
                context.Add(new BookAuthor()
                {
                    BookId   = book.BookId,
                    AuthorId = author.AuthorId
                });
                context.SaveChanges();
            }
        }
示例#3
0
        /// <summary>
        /// Dodaje nowy gatunek do bazy danych.
        /// </summary>
        /// <param name="name">Nazwa nowego gatunku</param>
        public void AddGenere(string name)
        {
            name = name.Trim();

            if (name == null || name == "")
            {
                throw new ArgumentNullException();
            }

            using (BookshelfContext context = new BookshelfContext())
            {
                context.Add(new Genere()
                {
                    GenereName = name
                });
                context.SaveChanges();
            }
        }