Esempio n. 1
0
        //private static Author GetAuthor(Author author)
        //{
        //    using (var context = new BookDBDataContext())
        //    {
        //        return context.Authors.FirstOrDefault(a => a.FirstName.Equals(author.FirstName) && a.LastName.Equals(author.LastName));
        //    }
        //}

        private static void AddGenre(string genre)
        {
            using (var context = new BookDBDataContext())
            {
                if (context.Genres.FirstOrDefault(g => g.Name.Equals(genre)) == null)
                {
                    Genre gen = new Genre()
                    {
                        Name = genre
                    };

                    context.Genres.InsertOnSubmit(gen);
                    context.SubmitChanges();
                }
            }
        }
Esempio n. 2
0
        private static void AddAuthorsToBook(int bookID, List <Author> bookAuthors)
        {
            using (BookDBDataContext context = new BookDBDataContext())
            {
                foreach (var author in bookAuthors)
                {
                    var bookAuthor = new BookAuthor()
                    {
                        BookID   = bookID,
                        AuthorID = author.ID
                    };
                    context.BookAuthors.InsertOnSubmit(bookAuthor);
                }

                context.SubmitChanges();
            }
        }
Esempio n. 3
0
        private static Book AddBook(string title, string description, DateTime datePublished, int pages, string isbn, int bookCopies)
        {
            using (BookDBDataContext context = new BookDBDataContext())
            {
                Book newBook = new Book()
                {
                    Title         = title,
                    BookResume    = description,
                    DatePublished = datePublished,
                    Pages         = pages,
                    ISBN          = isbn
                };

                context.Books.InsertOnSubmit(newBook);

                while (bookCopies > 0)
                {
                    var bookCopy = new BookCopy()
                    {
                        Book = newBook
                    };
                    context.BookCopies.InsertOnSubmit(bookCopy);
                    bookCopies--;
                }

                //var existing = context.Authors.FirstOrDefault(a => a.FirstName.Contains("G"));

                //if (existing != null)
                //{
                //    var bookAuthor = new BookAuthor()
                //    {
                //        BookID = newBook.ID,
                //        AuthorID = existing.ID
                //    };
                //}

                //var bookGenre = new BookGenre()
                //{
                //    BookID = newBook.ID
                //};

                context.SubmitChanges();
                return(newBook);
            }
        }
Esempio n. 4
0
        private static void AddAuthor(string firstName, string lastName, DateTime yearBorn, DateTime yearDied)
        {
            using (var context = new BookDBDataContext())
            {
                if (context.Authors.FirstOrDefault(author => author.FirstName == firstName && author.LastName == lastName) == null)
                {
                    Author temp = new Author()
                    {
                        FirstName = firstName,
                        LastName  = lastName,
                        YearBorn  = yearBorn,
                        YearDied  = yearDied
                    };

                    context.Authors.InsertOnSubmit(temp);
                    context.SubmitChanges();
                }
            }
        }
Esempio n. 5
0
        private static void AddUser(string firstName, string lastName, string pseudonim, string email, int phone)
        {
            using (var context = new BookDBDataContext())
            {
                if (context.Users.FirstOrDefault(user => user.Email == email) == null)
                {
                    User temp = new User()
                    {
                        FirstName = firstName,
                        LastName  = lastName,
                        Pseudonim = pseudonim,
                        Email     = email,
                        Phone     = phone
                    };

                    context.Users.InsertOnSubmit(temp);
                    context.SubmitChanges();
                }
            }
        }
Esempio n. 6
0
        private static void AddGenresToBook(int bookID, List <string> genres)
        {
            using (BookDBDataContext context = new BookDBDataContext())
            {
                foreach (var genre in genres)
                {
                    AddGenre(genre);

                    var bookGenres = new BookGenre()
                    {
                        BookID  = bookID,
                        GenreID = GetGenre(genre).ID
                    };

                    context.BookGenres.InsertOnSubmit(bookGenres);
                }

                context.SubmitChanges();
            }
        }
 // POST: api/Books
 public int Post([FromBody] Book book)
 {
     db.Books.InsertOnSubmit(book);
     db.SubmitChanges();
     return(book.Id);
 }