Example #1
0
        public async Task <IActionResult> PutBookAuthor(int id, BookAuthor bookAuthor)
        {
            if (id != bookAuthor.BookId)
            {
                return(BadRequest());
            }

            _context.Entry(bookAuthor).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookAuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public int PostBook(BookDTO book)
        {
            var newBook = new Book
            {
                Title       = book.Title,
                Year        = book.Year,
                WhoTookIt   = book.WhoTookIt,
                IsAvailable = book.IsAvailable
            };

            foreach (var a in book.Authors)
            {
                var ba = new BookAuthor
                {
                    //Author = _context.Authors.Single(a => a.Id == a.Id),
                    AuthorId = a.Id,
                    BookId   = newBook.Id
                };
                newBook.BookAuthors.Add(ba);
                _context.BookAuthor.Add(ba);
            }

            _context.Books.Add(newBook);
            _context.SaveChanges();

            return(book.Id);
        }
 public int Update([FromBody] BookAuthor input)
 {
     repository.Update(input);
     repository.Save();
     // mesle crtl s dar data base mibashad savechange
     return(input.Id);
 }
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _context.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _context.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _context.Add(bookAuthor);
            }


            // Creating a category list first and then add it to the
            var bookCategories = new List <BookCategory>();

            foreach (var category in categories)
            {
                bookCategories.Add(new BookCategory()
                {
                    Category = category,
                    Book     = book
                });
            }

            _context.AddRange(bookCategories);
            _context.Add(book);
            return(Save());
        }
Example #5
0
        public void AddAuthors(BookDetailDTO book, long bookCreatedId)
        {
            book.BookAuthors.ForEach(a => {
                long authorId = 0;

                if (a.Id > 0)
                {
                    authorId = a.Id;
                }
                else
                {
                    var newAuthor = new Author {
                        Name = a.Name
                    };
                    _context.Author.Add(newAuthor);
                    _context.SaveChanges();
                    authorId = newAuthor.Id;
                }
                var bookAuthor = new BookAuthor {
                    BookId = bookCreatedId, AuthorId = authorId
                };
                _context.BookAuthors.Add(bookAuthor);
                _context.SaveChanges();
            });
        }
Example #6
0
        public IActionResult AddAuthor(Guid id, Guid authorId)
        {
            var bookAuthor = _uow.BookAuthorRepository.Find(id, authorId);

            if (bookAuthor != null)
            {
                return(RedirectToAction("Authors", new { id }));
            }

            var book = _uow.BookRepository.Find(id);

            if (book == null)
            {
                return(NotFound());
            }

            var author = _uow.AuthorRepository.Find(authorId);

            if (author == null)
            {
                return(NotFound());
            }

            bookAuthor = new BookAuthor {
                BookId = id, AuthorId = authorId
            };
            _uow.BookAuthorRepository.Add(bookAuthor);
            _uow.Commit();

            return(RedirectToAction("Authors", new { id }));
        }
        public BookAuthor AddBookAuthor(BookAuthor bookAuthor)
        {
            _repositoryWrapper.BookAuthor.AddBookAuthor(bookAuthor);
            //_logger.WriteInfo($"Added user with id: {user.Id}.");

            return(bookAuthor);
        }
        public BookAuthor UpdateBookAuthor(BookAuthor existing, BookAuthor bookAuthor)
        {
            _repositoryWrapper.BookAuthor.UpdateBookAuthor(existing, bookAuthor);
            // _logger.WriteInfo($"Updated user with id: {user.Id}.");

            return(bookAuthor);
        }
Example #9
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                var newBookAuthor = new BookAuthor
                {
                    Name           = request.Name,
                    LastName       = request.LastName,
                    DOB            = request.DOB,
                    BookAuthorGuid = Convert.ToString(Guid.NewGuid())
                };

                var result = await _context.AddAsync(newBookAuthor);

                if (result?.Entity == null)
                {
                    throw new Exception("Error while adding new BookAuthor.");
                }

                var transactions = await _context.SaveChangesAsync();

                if (transactions > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Error while saving BookAuthor.");
            }
Example #10
0
        public ActionResult Delete(string isbn)
        {
            if (Session["User"] == null)
            {
                return(RedirectToAction("Login", "Pages"));
            }

            try
            {
                // Let's get the book first
                Book book = Book.getByISBN(isbn);

                // Let's delete the link between the book and its author(s)
                BookAuthor.delete(book.ISBN);

                // And finally, let's delete the book
                book.delete();
            }
            catch
            {
                return(View("Error500"));
            }

            return(RedirectToAction("List"));
        }
        public async Task <ResponseObject <bool> > Create(BookDto bookDto)
        {
            var response = new ResponseObject <bool> {
                Data = false
            };

            using (var transaction = await _dbContext.Database.BeginTransactionAsync())
            {
                try
                {
                    var book = Mapper.Map <Models.Book>(bookDto);
                    await _dbContext.Books.AddAsync(book);

                    await _dbContext.SaveChangesAsync();

                    foreach (var authorDto in bookDto.Authors)
                    {
                        var author = await _dbContext.Authors.Where(x => x.Id == authorDto.Id)
                                     .FirstOrDefaultAsync();

                        if (author != null)
                        {
                            var joinEntity = new BookAuthor
                            {
                                AuthorId = author.Id,
                                BookId   = book.Id
                            };
                            await _dbContext.BookAuthors.AddAsync(joinEntity);

                            continue;
                        }

                        var newAuthor = Mapper.Map <Models.Author>(authorDto);
                        await _dbContext.Authors.AddAsync(newAuthor);

                        var newJoinEntity = new BookAuthor
                        {
                            AuthorId = newAuthor.Id,
                            BookId   = book.Id
                        };

                        await _dbContext.BookAuthors.AddAsync(newJoinEntity);
                    }

                    await _dbContext.SaveChangesAsync();

                    transaction.Commit();
                    response.Data         = true;
                    response.ResponseType = ResponseType.Success;
                    return(response);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    response.Error        = e.Message;
                    response.ResponseType = ResponseType.Error;
                    return(response);
                }
            }
        }
        public IHttpActionResult PutBookAuthor(int id, BookAuthor bookAuthor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != bookAuthor.BookAuthor_Id)
            {
                return(BadRequest());
            }

            db.Entry(bookAuthor).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookAuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        private async Task SaveAuthor()
        {
            var(bookId, authorId) = bookAuthorsVM;

            if (!string.IsNullOrEmpty(authorId) && bookId != 0)
            {
                var bookAuthor = new BookAuthor
                {
                    BookId   = bookId,
                    AuthorId = int.Parse(authorId)
                };
                await @Service.BookAuthors.AddAsync(bookAuthor);

                @Service.Save();

                bookAuthorsVM.Authors = bookAuthorsVM.Authors
                                        .Where(_ => _.Value != authorId);

                bookAuthorsVM.BookAuthors = (await @Service
                                             .BookAuthors
                                             .GetAllAsync(_ => _.BookId == bookId,
                                                          includeProperties: "Book,Author")
                                             ).ToList();

                bookAuthorsVM.BookAuthorVM = new BookAuthorViewModel
                {
                    BookId   = bookId,
                    AuthorId = ""
                };

                await RefreshBooksList();
            }
        }
Example #14
0
        public ActionResult <BookDto> CreateBookForAuthor(
            Guid authorId, BookForCreationForAuthorDto book)
        {
            var checkAuthorExists = _repositoryWrapper.Authors.Get(authorId);

            if (checkAuthorExists == null)
            {
                return(NotFound());
            }

            var bookEntity = _mapper.Map <Book>(book);

            var authorEntity = _repositoryWrapper.Authors.Get(authorId);

            _repositoryWrapper.Books.Add(bookEntity);

            BookAuthor bookAuthor = new BookAuthor()
            {
                BookId   = bookEntity.BookId,
                Book     = bookEntity,
                AuthorId = authorEntity.AuthorId,
                Author   = authorEntity
            };

            _repositoryWrapper.BookAuthors.Add(bookAuthor);

            var bookToReturn = _mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, bookId = bookToReturn.BookId },
                                  bookToReturn));
        }
Example #15
0
        public bool CreateBook(List <int> authorId, List <int> categorieId, Book book)
        {
            var authors    = _bookDBContext.Authors.Where(a => authorId.Contains(a.Id)).ToList();
            var categories = _bookDBContext.Categories.Where(c => categorieId.Contains(c.Id)).ToList();

            foreach (var a in authors)
            {
                var bookauthor = new BookAuthor
                {
                    Author = a,
                    Book   = book
                };
            }

            foreach (var c in categories)
            {
                var bookcategories = new BookCategory
                {
                    Category = c,
                    Book     = book
                };
            }

            _bookDBContext.Add(book);

            return(Save());
        }
Example #16
0
        public MainWindow()
        {
            Data data = new Data();

            InitializeComponent();
            Database.SetInitializer(new CustomInitializer <LibraryDb>());
            MaintainDb db    = new MaintainDb();
            Author     jkrow = new Author("Joanne", "Rowling", "Harry Potter's mother");
            Book       hp1   = new Book("Harry Potter and the Philosopher's Stone", 1, 1997, "English", 223);

            data.addAuthor(jkrow);
            data.addBook(hp1);
            books   = data.getBooks();
            authors = data.getAuthors();

            foreach (Book b in books)
            {
                foreach (Author a in authors)
                {
                    if (b.authorId == a.id)
                    {
                        bookWithAuthor = new BookAuthor(b, a);
                        System.Console.WriteLine(bookWithAuthor.Id + " " + bookWithAuthor.Title);
                        DataContext = bookWithAuthor;
                    }
                }
            }
        }
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookDbContext.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
                // We could also create List<BookAuthor> and add all bookAuthor objects to it,
                // and then use AddRange on the DbContext to add it all at once. Either way is fine.
            }

            foreach (var category in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            _bookDbContext.Add(book);

            return(Save());
        }
        public void SeedData()
        {
            if (_libraryContext.Books.Count() == 0 && _libraryContext.Authors.Count() == 0)
            {
                var sapkowski = new Author {
                    AuthorId = Guid.NewGuid(), BirthYear = new DateTime(175, 10, 13), Name = "Andrzej Sapkowski"
                };
                var krewElfow = new Book {
                    BookId = Guid.NewGuid(), Price = 50, Title = "Krew Elfów", PublishedYear = new DateTime(2001, 10, 10)
                };
                var bookAuthor = new BookAuthor {
                    BookAuthorId = Guid.NewGuid(), Author = sapkowski, Book = krewElfow
                };
                sapkowski.BookAuthors = new List <BookAuthor> {
                    bookAuthor
                };
                krewElfow.BookAuthors = new List <BookAuthor> {
                    bookAuthor
                };

                _libraryContext.Authors.Add(sapkowski);
                _libraryContext.Books.Add(krewElfow);
                _libraryContext.SaveChanges();
            }
        }
Example #19
0
        public ResponseModel UpdateBookAuthor(BookAuthor bookAuthor, int id)
        {
            try
            {
                var authorBookSearch = _context.BookAuthor.Where(booAut => booAut.Id == id).FirstOrDefault();

                if (authorBookSearch != null)
                {
                    authorBookSearch.BookId       = bookAuthor.BookId;
                    authorBookSearch.AuthorId     = bookAuthor.AuthorId;
                    authorBookSearch.CreationDate = bookAuthor.CreationDate;
                    authorBookSearch.ChangeDate   = DateTime.Now;

                    _context.BookAuthor.Update(authorBookSearch);
                    _context.SaveChanges();

                    response.Status  = 200;
                    response.Message = "Atualização realizada com sucesso!";
                }
                else
                {
                    response.Status  = 400;
                    response.Message = "O código informado não existe na base de dados!";
                }

                return(response);
            }
            catch (Exception)
            {
                response.Status  = 500;
                response.Message = "Houve um erro ao acessar o banco de dados!";

                return(response);
            }
        }
        public bool UpdateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookDbContext.Categories.Where(c => categoriesId.Contains(c.Id)).ToList();

            var bookAuthorsToDelete    = _bookDbContext.BookAuthors.Where(b => b.BookId == book.Id);    // here it's selected the BookId and the AuthorId from BookAuthors;
            var bookCategoriesToDelete = _bookDbContext.BookCategories.Where(b => b.BookId == book.Id); // here it's selected the BookId and the CategoryId from BookCategories;

            _bookDbContext.RemoveRange(bookAuthorsToDelete);
            _bookDbContext.RemoveRange(bookCategoriesToDelete);

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
            }

            foreach (var category in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            _bookDbContext.Update(book);

            return(Save());
        }
Example #21
0
        public ActionResult Edit(Book book, List <int> AuthorIds)
        {
            db.Entry(book).State = EntityState.Modified;
            var bookMatchesInJoinTable = db.BookAuthors.Where(entry => entry.BookId == book.BookId).ToList();

            // Remove all BookAuthor entries for specified book.
            foreach (var author in bookMatchesInJoinTable)
            {
                int authorId  = author.AuthorId;
                var joinEntry = db.BookAuthors
                                .Where(entry => entry.AuthorId == authorId)
                                .Where(entry => entry.BookId == book.BookId);
                foreach (var entry in joinEntry)
                {
                    db.BookAuthors.Remove(entry);
                }
            }
            // Add BookAuthor entries based on bookMatches.
            foreach (var id in AuthorIds)
            {
                Author     author        = db.Authors.FirstOrDefault(_ => _.AuthorId == id);
                BookAuthor newBookAuthor = new BookAuthor(book.BookId, author.AuthorId);
                db.BookAuthors.Add(newBookAuthor);
            }
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public IActionResult DeleteBookAuthorsById(Guid book_id, Guid author_id)
        {
            // Check if the book already exist
            Book bookById = this.repository.GetWhere((item) => item.Id.Equals(book_id)).FirstOrDefault();

            if (bookById == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"The book, {book_id}, was not found.");
                return(NotFound(error));
            }

            // Check if the author exist
            BookAuthor bookAuthor = this.repositoryBookAuthor.GetWhere((item) => item.Author.Id.Equals(author_id) &&
                                                                       item.Book.Id.Equals(book_id)).FirstOrDefault();

            if (bookAuthor == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"The author id {author_id} was not found.");
                return(NotFound(error));
            }

            this.repositoryBookAuthor.Delete(bookAuthor);
            this.repositoryBookAuthor.Save();

            return(Ok());
        }
        public async Task <IActionResult> PutBookAuthor(int authorid, string isbn, BookAuthor bookAuthor)
        {
            if (authorid != bookAuthor.AuthorID || isbn != bookAuthor.ISBN || !ModelState.IsValid)
            {
                return(BadRequest(ModelState.SelectMany(x => x.Value.Errors)));
            }

            _context.Entry(bookAuthor).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookAuthorExists(authorid, isbn))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #24
0
        public bool CreateBook(List <int> authorsId, List <int> categoriesId, Book book)
        {
            var authors    = _bookContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();
            var categories = _bookContext.Categories.Where(a => categoriesId.Contains(a.Id)).ToList();

            foreach (var author in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = author,
                    Book   = book
                };
                _bookContext.Add(bookAuthor);
            }
            foreach (var category in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = category,
                    Book     = book
                };
                _bookContext.Add(bookCategory);
            }

            _bookContext.Add(book);

            return(Save());
        }
Example #25
0
        public ActionResult ViewSingleBook(int id)
        {
            Book   targetBook  = null;
            string authorNames = null;

            using (GaryBookstoreEntities dbc = new GaryBookstoreEntities())
            {
                targetBook = dbc.Books.Include("Inventory").SingleOrDefault(b => b.BookId == id);
                List <BookAuthor> authors = dbc.BookAuthors.Where(b => b.BookId == id).ToList();
                for (int i = 0; i < authors.Count; i++)
                {
                    BookAuthor author = authors[i];
                    authorNames += dbc.Authors.SingleOrDefault(a => a.Id == author.AuthorId).Name;
                    if (i < authors.Count() - 1)
                    {
                        authorNames += ", ";
                    }
                }
            }
            if (targetBook == null)
            {
                return(View("Error"));
            }
            else
            {
                ViewData["authors"] = authorNames;
                return(View("BookDetails", targetBook));
            }
        }
        public async Task <IActionResult> Edit(int id, [Bind("BookAuthorId,BookId,AuthorId")] BookAuthor bookAuthor)
        {
            if (ModelState.IsValid)
            {
                var ba = await _context.BookAuthors.FirstAsync(item => item.BookAuthorId == bookAuthor.BookAuthorId);

                var book = await _context.Books.FindAsync(bookAuthor.BookId);

                var author = await _context.Authors.FindAsync(bookAuthor.AuthorId);

                book.Authors.Remove(ba);
                author.Books.Remove(ba);
                _context.BookAuthors.Remove(ba);
                author.Books.Add(bookAuthor);
                book.Authors.Add(bookAuthor);
                _context.BookAuthors.Add(bookAuthor);
                _context.Update(author);
                _context.Update(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", bookAuthor.AuthorId);
            ViewData["BookId"]   = new SelectList(_context.Books, "BookId", "BookId", bookAuthor.BookId);
            ViewData["Authors"]  = await _context.Authors.ToListAsync();

            return(View(bookAuthor));
        }
Example #27
0
        public bool CreateBook(List <int> authorsId, List <int> categoiesId, Book book)
        {
            //Get all the Author object from the list of authorsId passed
            var authors = _bookDbContext.Authors.Where(a => authorsId.Contains(a.Id)).ToList();

            //Get all the Category object from the list of categoriesId passed
            var categories = _bookDbContext.Categories.Where(a => categoiesId.Contains(a.Id)).ToList();

            //Looping through Author object list and populating the BookAuthor table
            foreach (var a in authors)
            {
                var bookAuthor = new BookAuthor()
                {
                    Author = a,
                    Book   = book
                };
                _bookDbContext.Add(bookAuthor);
            }

            //Looping through Category object list and populating the BookCategory table
            foreach (var c in categories)
            {
                var bookCategory = new BookCategory()
                {
                    Category = c,
                    Book     = book
                };
                _bookDbContext.Add(bookCategory);
            }

            //Populating to Books table
            _bookDbContext.Books.Add(book);

            return(Save());
        }
Example #28
0
        public bool UpdateBook(List <int> authorId, List <int> categorieId, Book book)
        {
            var authors    = _bookDBContext.Authors.Where(a => authorId.Contains(a.Id)).ToList();
            var categories = _bookDBContext.Categories.Where(c => categorieId.Contains(c.Id)).ToList();

            var bookauthortodelete   = _bookDBContext.BookAuthors.Where(b => b.BookId == book.Id);
            var bookcategorytodelete = _bookDBContext.BookCategories.Where(c => c.BookId == book.Id);

            _bookDBContext.RemoveRange(bookauthortodelete);
            _bookDBContext.RemoveRange(bookcategorytodelete);

            foreach (var a in authors)
            {
                var bookauthor = new BookAuthor
                {
                    Author = a,
                    Book   = book
                };
            }

            foreach (var c in categories)
            {
                var bookcategories = new BookCategory
                {
                    Category = c,
                    Book     = book
                };
            }

            _bookDBContext.Update(book);

            return(Save());
        }
        private async Task AddAuthorForBookToRelationsDatabase(int bookId, string author)
        {
            int    authorId;
            Author authorToDownload = new Author
            {
                FirstName = author.Split(" ")[0],
                LastName  = author.Split(" ")[1]
            };

            bool IsAuthorExists = await _authorsRepository.IsAuthorExists(authorToDownload);

            if (!IsAuthorExists)
            {
                await _authorsRepository.AddAuthorData(authorToDownload);

                authorId = authorToDownload.AuthorID;
            }
            else
            {
                authorId = await _authorsRepository.GetAuthorId(authorToDownload);
            }

            var bookAuthor = new BookAuthor(bookId, authorId);
            await _booksAuthorsRepository.AddBookAuthor(bookAuthor);
        }
Example #30
0
        public Book EditBook(int bookId, BookDTO bookDTO)
        {
            var book = _context.Books.Where(b => b.Id == bookId).FirstOrDefault();

            book.IsAvailable = bookDTO.IsAvailable;
            book.Title       = bookDTO.Title;
            book.WhoTookIt   = bookDTO.WhoTookIt;
            book.Year        = bookDTO.Year;

            while (_context.BookAuthor.Any(ba => ba.BookId == book.Id))
            {
                var bookAuthor = _context.BookAuthor.Where(ba => ba.BookId == book.Id).FirstOrDefault();
                _context.BookAuthor.Remove(bookAuthor);
                _context.SaveChanges();
            }
            book.BookAuthors = new List <BookAuthor>();
            foreach (var author in bookDTO.Authors)
            {
                var ba = new BookAuthor
                {
                    BookId   = book.Id,
                    AuthorId = author.Id
                };
                book.BookAuthors.Add(ba);
                _context.BookAuthor.Add(ba);
            }

            _context.SaveChanges();
            return(book);
        }
Example #31
0
        public static void SetAuthors(BooksDataContext db, string bookArg)
        {
            int bookId;
            if (!int.TryParse(bookArg, out bookId))
            {
                Console.WriteLine("Invalid book ID! Operation aborted!");
                return;
            }

            var bookForUpdate = (from book in db.Books
                                where book.BookID == bookId
                                select book).SingleOrDefault();

            if (bookForUpdate == null)
            {
                Console.WriteLine("No such book! Operation aborted!");
                return;
            }

            Console.WriteLine("Found book: ");
            Console.WriteLine($"BookID: {bookForUpdate.BookID}");
            Console.WriteLine($"Title: {bookForUpdate.Title}");
            Console.WriteLine($"Description: {bookForUpdate.Description}");
            Console.WriteLine($"Publisher: {bookForUpdate.Publisher}");
            Console.WriteLine($"Pages: {bookForUpdate.Pages}");
            Console.WriteLine($"ISBN: {bookForUpdate.ISBN}");
            Console.WriteLine();

            HashSet<int> authorIds = new HashSet<int>();
            foreach (var author in db.Authors)
            {
                authorIds.Add(author.AuthorID);
                Console.WriteLine($"{author.AuthorID} - {author.FirstName} {author.LastName}");
            }
            Console.WriteLine("Please write IDs of authors you want to add, separated by comma or space");
            int[] idsToAdd;
            try
            {
                idsToAdd = Console.ReadLine().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
            }
            catch (FormatException ex)
            {
                Console.WriteLine("Invalid ids format! Operation aborted!");
                Console.WriteLine(ex.Message);
                return;
            }

            bool allExist = true;
            foreach (var id in idsToAdd)
            {
                if (!authorIds.Contains(id))
                {
                    allExist = false;
                    break;
                }
            }

            if (!allExist)
            {
                Console.WriteLine("Ids provided does not exist! Operation aborted!");
                return;
            }

            foreach (var authID in idsToAdd)
            {
                var newAuth = new BookAuthor();
                newAuth.AuthorID = authID;
                bookForUpdate.BookAuthors.Add(newAuth);
            }

            try
            {
                db.SubmitChanges();
            }
            catch (ChangeConflictException ex)
            {
                Console.WriteLine("Cannot update book authors! Operation exited with message:");
                Console.WriteLine(ex.Message);
                return;
            }
            Console.WriteLine("Book authors updated successfully!");
        }
Example #32
0
        public ActionResult Create(FormCollection collection, HttpPostedFileBase file)
        {
            try
            {
                myHandler = new BusinessLogicHandler();
                book = new Book();
                book.BookTitle = collection.GetValue("books.BookTitle").AttemptedValue.ToString();
                book.Synopsis = collection.GetValue("books.Synopsis").AttemptedValue.ToString();
                book.ISBN = collection.GetValue("books.ISBN").AttemptedValue.ToString();
                book.BookCategoryID = Convert.ToInt32(collection.GetValue("CategoryName").AttemptedValue);
                book.PublisherID = Convert.ToInt32(collection.GetValue("PublisherName").AttemptedValue);
                book.SupplierID = Convert.ToInt32(collection.GetValue("Name").AttemptedValue);
                //book.AuthorID = Convert.ToInt32(collection.GetValue("FullName").AttemptedValue);
                string[] Authors = (string[])collection.GetValue("FullName").RawValue;
                book.CostPrice = Convert.ToDouble(collection.GetValue("books.CostPrice").AttemptedValue);
                book.SellingPrice = Convert.ToDouble(collection.GetValue("books.SellingPrice").AttemptedValue);
                book.DateAdded = DateTime.Now;

                //TryUpdateModel(book); //CONSIDER REMOVING THIS..!
                if (ModelState.IsValid)
                {
                    if (file != null)
                    {
                        file.SaveAs(HttpContext.Server.MapPath("~/Uploads/Books/") + file.FileName);
                        book.CoverImage = file.FileName;
                    }

                    Book ba = new Book();
                    BookAuthor bookAuthors = new BookAuthor();

                    ba = myHandler.AddExperimentBook(book);
                    ba.BookTitle = book.BookTitle;
                    ba.Synopsis = book.Synopsis;
                    ba.ISBN = book.ISBN;
                    ba.BookCategoryID = book.BookCategoryID;
                    ba.PublisherID = book.PublisherID;
                    ba.SupplierID = book.SupplierID;
                    ba.CoverImage = book.CoverImage;

                    //myHandler.AddBook(ba);
                    bookAuthors = myHandler.TrialInsertBook(ba);

                    foreach (var item in Authors) //INSERTING BOOK AUTHORS
                    {
                        bookAuthors.AuthorID = Convert.ToInt32(item);
                        myHandler.InsertBookAuthor(bookAuthors);
                    }
                    TempData["AlertMessage"] = "Book Successfully Entered";
                }

                //return RedirectToAction("Index", "Book", book);
                return RedirectToAction("Create", "Book", null); //REDIRECT TO GET ACTION METHOD #INCASE THEY WANT TO INSERT ANOTHER BOOK :)
            }
            catch
            {
                AddNewBookViewModel model = new AddNewBookViewModel();
                #region Create
                SupplierHandler supHandler = new SupplierHandler();
                /*TEMP LIST*/
                //List<Supplier> nameList = new List<Supplier>();
                IEnumerable<Supplier> nameList = (IEnumerable<Supplier>)supHandler.GetBookSupplierList();
                var disp = from nameAndId in nameList
                           select new { Value = nameAndId.SupplierID, Text = nameAndId.Name };

                ViewBag.SupplierList = new SelectList(disp.ToList());

                BookCategoryHandler typeHandler = new BookCategoryHandler();
                IEnumerable<BookCategory> typeList = (IEnumerable<BookCategory>)typeHandler.GetBookCategoryList();
                var dispBC = from name in typeList
                             select new { Value = name.BookCategoryID, Text = name.CategoryName };

                ViewBag.BookCategoryList = new SelectList(dispBC.ToList());

                AuthorHandler authHandler = new AuthorHandler();
                IEnumerable<Author> authList = (IEnumerable<Author>)authHandler.GetAuthorList();
                var dispAuth = from nameAndSurname in authList
                               select new { Value = nameAndSurname.AuthorID, Text = nameAndSurname.Name, nameAndSurname.Surname };
                ViewBag.authList = new SelectList(dispAuth.ToList());

                PublisherHandler publHandler = new PublisherHandler();
                IEnumerable<Publisher> pubList = (IEnumerable<Publisher>)publHandler.GetPublisherList();
                var dispPublisher = from pubName in pubList
                                    select new { Value = pubName.PublisherID, Text = pubName.Name };
                ViewBag.pubList = new SelectList(dispPublisher.ToList());

                #endregion

                #region Display
                List<SelectListItem> bookCategory = new List<SelectListItem>();
                //bookCategory.Add(new SelectListItem { Text = "Select Category", Value = "", Selected = true });
                foreach (var item in typeList)
                {
                    bookCategory.Add(new SelectListItem { Text = item.CategoryName, Value = item.BookCategoryID.ToString() });
                }
                model.bookCategories = new List<SelectListItem>();
                model.bookCategories = bookCategory;
                ViewData["bookCategories"] = bookCategory;

                List<SelectListItem> supplier = new List<SelectListItem>();
                //supplier.Add(new SelectListItem { Text = "Select Supplier", Value = "", Selected = true });
                foreach (var item in nameList)
                {
                    supplier.Add(new SelectListItem { Text = item.Name, Value = item.SupplierID.ToString() });
                }
                model.suppliers = new List<SelectListItem>();
                model.suppliers = supplier;
                ViewData["suppliers"] = supplier;

                List<SelectListItem> author = new List<SelectListItem>();
                //author.Add(new SelectListItem { Text = "Select Author", Value = "", Selected = true });
                foreach (var item in authList)
                {
                    author.Add(new SelectListItem { Text = item.Name, Value = item.AuthorID.ToString() });
                }
                model.authors = new List<SelectListItem>();
                model.authors = author;
                ViewData["authors"] = author;

                List<SelectListItem> publisher = new List<SelectListItem>();
                //publisher.Add(new SelectListItem { Text = "Select Publisher", Value = "", Selected = true });
                foreach (var item in pubList)
                {
                    publisher.Add(new SelectListItem { Text = item.Name, Value = item.PublisherID.ToString() });
                }
                model.publishers = new List<SelectListItem>();
                model.publishers = publisher;
                ViewData["publishers"] = publisher;
                #endregion
                return View(model);
            }
        }
Example #33
0
        public ActionResult Edit(FormCollection collection, AddNewBookViewModel model, HttpPostedFileBase file)
        {
            try
            {
                myHandler = new BusinessLogicHandler();
                book = new Book();
                if (ModelState.IsValid)
                {
                    model.books.BookCategoryID = Convert.ToInt32(collection.GetValue("CategoryName").AttemptedValue);
                    model.books.PublisherID = Convert.ToInt32(collection.GetValue("PublisherName").AttemptedValue);
                    model.books.SupplierID = Convert.ToInt32(collection.GetValue("Name").AttemptedValue);
                    string[] Authors = (string[])collection.GetValue("FullName").RawValue;

                    try
                    {
                        if (file != null)
                        {
                            model.books.CoverImage = file.FileName;
                            file.SaveAs(HttpContext.Server.MapPath("~/Uploads/Books/") + file.FileName);
                        }
                    }
                    catch
                    {

                    }
                    #region Clean Book_Authors
                    myHandler.BookAuthorUpdateDelete(model.books.BookID);
                    #endregion

                    #region Add New Book_Authors

                    BookAuthor bookAuthors = new BookAuthor();
                    bookAuthors.BookID = model.books.BookID;
                    foreach (var item in Authors) //INSERTING BOOK AUTHORS
                    {
                        bookAuthors.AuthorID = Convert.ToInt32(item);
                        myHandler.InsertBookAuthor(bookAuthors);
                    }

                    #endregion

                    myHandler.UpdateBookProduct(model.books);
                    myHandler.UpdateBook(model.books);
                }
                return RedirectToAction("AdminIndex", "Book", null);
            }
            catch
            {
                return View();
            }
        }
Example #34
0
        internal static void SeedData(SciHubDbContext context)
        {
            if (context.Users.Any())
            {
                return;
            }

            const string defaultPassword = "******";
            var userManager = new UserManager<User>(new UserStore<User>(context));

            // Users
            var newton = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Isaac",
                LastName = "Newton",
                Avatar =
                    "https://lh6.googleusercontent.com/-7kPxj5JfhT0/VE6WRbBqcqI/AAAAAAAAKFM/typmQq-_S5Y/Isaac%252520Newton%252520Religious%252520Views.jpg",
                Gender = Gender.Male,
                About = "The best physicist and mathmatician out there."
            };

            var einstein = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Albert",
                LastName = "Einstein",
                Avatar =
                    "http://static8.bornrichimages.com/cdn2/500/500/91/c/wp-content/uploads/2014/04/Albert-Einstein-1921_thumb.jpg",
                Gender = Gender.Male,
                About = "The new best physicist and mathmatician out there."
            };

            var turing = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Alan",
                LastName = "Turing",
                Avatar =
            "https://c4.staticflickr.com/4/3882/19046770766_073023b43f.jpg",
                Gender = Gender.Male,
                About = "Puzzles are easy for me."
            };

            var ada = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Ada",
                LastName = "Lovelace",
                Avatar =
            "https://40.media.tumblr.com/a1e0ee726344a22b21507d5673705328/tumblr_nz67nvweCf1smrroto1_500.jpg",
                Gender = Gender.Female,
                About = "Love punched tape!"
            };

            var paranoidAndroid = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Marvin",
                LastName = "None",
                Avatar = UserDefaultPictureConstants.Neutral,
                Gender = Gender.Other,
                About = "This will all end in tears."
            };

            var testMaleUser = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "John",
                LastName = "Doe",
                BirthDate = new DateTime(1990, 3, 3),
                Avatar = UserDefaultPictureConstants.Male,
                Gender = Gender.Male,
                About = "Nothing to say."
            };

            var testFemaleUser = new User
            {
                UserName = "******",
                Email = "*****@*****.**",
                FirstName = "Jane",
                LastName = "Doe",
                BirthDate = new DateTime(1992, 2, 1),
                Avatar = UserDefaultPictureConstants.Female,
                Gender = Gender.Female,
                About = "Nothing to say."
            };

            userManager.Create(newton, defaultPassword);
            userManager.AddToRole(newton.Id, UserRoleConstants.Default);
            userManager.Create(einstein, defaultPassword);
            userManager.AddToRole(einstein.Id, UserRoleConstants.Default);
            userManager.Create(turing, defaultPassword);
            userManager.AddToRole(turing.Id, UserRoleConstants.Default);
            userManager.Create(ada, defaultPassword);
            userManager.AddToRole(ada.Id, UserRoleConstants.Default);
            userManager.Create(paranoidAndroid, defaultPassword);
            userManager.AddToRole(paranoidAndroid.Id, UserRoleConstants.Default);
            userManager.Create(testMaleUser, defaultPassword);
            userManager.AddToRole(testMaleUser.Id, UserRoleConstants.Default);
            userManager.Create(testFemaleUser, defaultPassword);
            userManager.AddToRole(testFemaleUser.Id, UserRoleConstants.Default);

            context.SaveChanges();

            // Tags
            var dark = new Tag
            {
                Name = "dark"
            };

            var funny = new Tag
            {
                Name = "funny"
            };

            var sad = new Tag
            {
                Name = "sad"
            };

            var ai = new Tag
            {
                Name = "ai"
            };

            var aliens = new Tag
            {
                Name = "aliens"
            };

            var planet = new Tag
            {
                Name = "planet"
            };

            var space = new Tag
            {
                Name = "space"
            };

            var distopian = new Tag
            {
                Name = "distopian"
            };

            var zombies = new Tag
            {
                Name = "zombies"
            };

            var apocalypitc = new Tag
            {
                Name = "apocalyptic"
            };

            var survival = new Tag
            {
                Name = "survival"
            };

            var cyberpunk = new Tag
            {
                Name = "cyberpunk"
            };

            var cyborgs = new Tag
            {
                Name = "cyborgs"
            };

            var exploration = new Tag
            {
                Name = "exploration"
            };

            var timeTravel = new Tag
            {
                Name = "time travel"
            };

            context.Tags.Add(dark);
            context.Tags.Add(funny);
            context.Tags.Add(sad);
            context.Tags.Add(ai);
            context.Tags.Add(aliens);
            context.Tags.Add(planet);
            context.Tags.Add(space);
            context.Tags.Add(distopian);
            context.Tags.Add(zombies);
            context.Tags.Add(apocalypitc);
            context.Tags.Add(survival);
            context.Tags.Add(cyberpunk);
            context.Tags.Add(cyborgs);
            context.Tags.Add(exploration);
            context.Tags.Add(timeTravel);

            context.SaveChanges();

            // Short Stories
            var firstStory = new ShortStory
            {
                Title = "The Dawning",
                Content = @"Sed porttitor lectus nibh. Cras ultricies ligula sed magna dictum porta. Proin eget tortor risus. Nulla quis lorem ut libero malesuada feugiat. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Cras ultricies ligula sed magna dictum porta. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus. Nulla quis lorem ut libero malesuada feugiat. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a.

                Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.Donec sollicitudin molestie malesuada.Curabitur arcu erat,
                accumsan id imperdiet et,
                porttitor at sem.Cras ultricies ligula sed magna dictum porta.Pellentesque in ipsum id orci porta dapibus.Praesent sapien massa,
                convallis a pellentesque nec,
                egestas non nisi.Praesent sapien massa,
                convallis a pellentesque nec,
                egestas non nisi.Nulla porttitor accumsan tincidunt.Pellentesque in ipsum id orci porta dapibus.Vivamus magna justo,
                lacinia eget consectetur sed,
                convallis at tellus.",

                AuthorId = ada.Id,
            };

            var fistStoryRating = new ShortStoryRating
            {
                Value = 4,
                ShortStoryId = firstStory.Id,
                UserId = newton.Id
            };

            var firstStoryFirstComment = new ShortStoryComment
            {
                Content = "Very funny!",
                ShortStoryId = firstStory.Id,
                AuthorId = einstein.Id
            };

            var firstStorySecondComment = new ShortStoryComment
            {
                Content = "The main character was great.",
                ShortStoryId = firstStory.Id,
                AuthorId = turing.Id
            };

            firstStory.Ratings.Add(fistStoryRating);
            firstStory.Comments.Add(firstStoryFirstComment);
            firstStory.Comments.Add(firstStorySecondComment);
            firstStory.Tags.Add(funny);
            firstStory.Tags.Add(ai);

            var secondStory = new ShortStory
            {
                Title = "He knows",
                Content = @"Vivamus suscipit tortor eget felis porttitor volutpat. Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Nulla porttitor accumsan tincidunt. Donec rutrum congue leo eget malesuada. Vivamus suscipit tortor eget felis porttitor volutpat. Nulla quis lorem ut libero malesuada feugiat. Cras ultricies ligula sed magna dictum porta. Proin eget tortor risus. Donec rutrum congue leo eget malesuada.

            Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Sed porttitor lectus nibh. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Donec rutrum congue leo eget malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus. Nulla quis lorem ut libero malesuada feugiat. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Cras ultricies ligula sed magna dictum porta. Curabitur aliquet quam id dui posuere blandit. Cras ultricies ligula sed magna dictum porta.

            Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Nulla porttitor accumsan tincidunt. Pellentesque in ipsum id orci porta dapibus. Cras ultricies ligula sed magna dictum porta. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Sed porttitor lectus nibh. Donec rutrum congue leo eget malesuada. Donec sollicitudin molestie malesuada. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Donec rutrum congue leo eget malesuada. Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus.",

                AuthorId = testFemaleUser.Id,
            };

            var secondStoryRating = new ShortStoryRating
            {
                Value = 5,
                ShortStoryId = secondStory.Id,
                UserId = einstein.Id
            };
            var secondStoryFirstComment = new ShortStoryComment
            {
                Content = "Intriguing!",
                ShortStoryId = secondStory.Id,
                AuthorId = ada.Id
            };

            secondStory.Ratings.Add(secondStoryRating);
            secondStory.Comments.Add(secondStoryFirstComment);
            secondStory.Tags.Add(dark);

            var thirdStory = new ShortStory
            {
                Title = "Into maddness",
                Content = @"Cras ultricies ligula sed magna dictum porta. Donec rutrum congue leo eget malesuada. Quisque velit nisi, pretium ut lacinia in, elementum id enim. Curabitur aliquet quam id dui posuere blandit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Proin eget tortor risus. Vivamus suscipit tortor eget felis porttitor volutpat. Donec rutrum congue leo eget malesuada. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Cras ultricies ligula sed magna dictum porta. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                AuthorId = einstein.Id,
            };

            var thirdStoryFirstRating = new ShortStoryRating
            {
                Value = 3,
                ShortStoryId = thirdStory.Id,
                UserId = einstein.Id
            };

            var thirdStorySecondRating = new ShortStoryRating
            {
                Value = 5,
                ShortStoryId = thirdStory.Id,
                UserId = newton.Id
            };

            var thirdStoryComment = new ShortStoryComment
            {
                Content = "Great!",
                ShortStoryId = thirdStory.Id,
                AuthorId = testMaleUser.Id
            };

            thirdStory.Ratings.Add(thirdStoryFirstRating);
            thirdStory.Ratings.Add(thirdStorySecondRating);
            thirdStory.Comments.Add(thirdStoryComment);
            thirdStory.Tags.Add(aliens);
            thirdStory.Tags.Add(sad);

            context.ShortStories.Add(firstStory);
            context.ShortStories.Add(secondStory);
            context.ShortStories.Add(thirdStory);
            context.SaveChanges();

            var webClient = new WebClient();

            // Books
            var herbert = new BookAuthor
            {
                FirstName = "Frank",
                LastName = "Herbert",
                BirthDate = new DateTime(1920, 10, 8)
            };

            var orwell = new BookAuthor
            {
                FirstName = "George",
                LastName = "Orwell",
                BirthDate = new DateTime(1903, 1, 23)
            };

            var gibson = new BookAuthor
            {
                FirstName = "William",
                LastName = "Gibson",
                BirthDate = new DateTime(1948, 3, 17)
            };

            context.BookAuthors.Add(herbert);
            context.BookAuthors.Add(orwell);
            context.BookAuthors.Add(gibson);

            context.SaveChanges();

            var dune = new Book
            {
                Title = "Dune",
                PublicationYear = 1965,
                Summary = @"Set in the far future amidst a sprawling feudal interstellar empire where planetary dynasties are controlled by noble houses that owe an allegiance to the imperial House Corrino, Dune tells the story of young Paul Atreides (the heir apparent to Duke Leto Atreides and heir of House Atreides) as he and his family accept control of the desert planet Arrakis, the only source of the 'spice' melange, the most important and valuable substance in the cosmos. The story explores the complex, multi-layered interactions of politics, religion, ecology, technology, and human emotion as the forces of the empire confront each other for control of Arrakis.",
                AuthorId = herbert.Id,
                Cover = new BookCover
                {
                    Image = webClient.DownloadData("https://upload.wikimedia.org/wikipedia/en/5/5a/FrankHerbert_Dune_1st.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            dune.Ratings.Add(new BookRating
            {
                Value = 4,
                UserId = ada.Id,
            });

            dune.Tags.Add(planet);
            dune.Tags.Add(space);

            var ninety = new Book
            {
                Title = "1984",
                PublicationYear = 1949,
                Summary = @"The year 1984 has come and gone, but George Orwell's prophetic, nightmarish vision in 1949 of the world we were becoming is timelier than ever. 1984 is still the great modern classic of negative utopia -a startlingly original and haunting novel that creates an imaginary world that is completely convincing, from the first sentence to the last four words. No one can deny the novel's hold on the imaginations of whole generations, or the power of its admonitions -a power that seems to grow, not lessen, with the passage of time.",
                AuthorId = orwell.Id,
                Cover = new BookCover
                {
                    Image = webClient.DownloadData("https://upload.wikimedia.org/wikipedia/en/c/c3/1984first.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            ninety.Ratings.Add(new BookRating
            {
                Value = 5,
                UserId = einstein.Id,
            });

            ninety.Ratings.Add(new BookRating
            {
                Value = 3,
                UserId = paranoidAndroid.Id,
            });

            ninety.Comments.Add(new BookComment
            {
                Content = "One of the best",
                AuthorId = testFemaleUser.Id
            });

            ninety.Tags.Add(distopian);

            var neuromancer = new Book
            {
                Title = "Neuromancer",
                PublicationYear = 1984,
                Summary = @"The Matrix is a world within the world, a global consensus- hallucination, the representation of every byte of data in cyberspace",
                AuthorId = gibson.Id,
                Cover = new BookCover
                {
                    Image = webClient.DownloadData("https://upload.wikimedia.org/wikipedia/en/4/4b/Neuromancer_(Book).jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            neuromancer.Ratings.Add(new BookRating
            {
                Value = 5,
                UserId = newton.Id,
            });

            neuromancer.Ratings.Add(new BookRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            neuromancer.Comments.Add(new BookComment
            {
                Content = "Futuristic genious",
                AuthorId = testFemaleUser.Id
            });

            neuromancer.Comments.Add(new BookComment
            {
                Content = "Loved it!",
                AuthorId = paranoidAndroid.Id
            });

            neuromancer.Tags.Add(cyberpunk);

            var mnemonic = new Book
            {
                Title = "Johnny Mnemonic",
                PublicationYear = 1981,
                Summary = @"Johnny is a courier. He carries data, uploaded into his brain through a jack implanted in his skull. And so Johnny's troubles begin. The data is stolen, and to get it back the owners have hired the Yakuza who intend to get hold of Johnny. But all they really need is his cryogenically frozen head.",
                AuthorId = gibson.Id,
                Cover = new BookCover
                {
                    Image = webClient.DownloadData("https://www.outerplaces.com/images/user_upload/covers7.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            mnemonic.Ratings.Add(new BookRating
            {
                Value = 3,
                UserId = newton.Id,
            });

            mnemonic.Ratings.Add(new BookRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            mnemonic.Comments.Add(new BookComment
            {
                Content = "Amazing",
                AuthorId = testMaleUser.Id
            });

            mnemonic.Comments.Add(new BookComment
            {
                Content = "Words cannot describe how I love this book",
                AuthorId = paranoidAndroid.Id
            });

            mnemonic.Tags.Add(cyberpunk);

            context.Books.Add(dune);
            context.Books.Add(ninety);
            context.Books.Add(neuromancer);
            context.Books.Add(mnemonic);
            context.SaveChanges();

            // Actors
            var sigorney = new Actor
            {
                FirstName = "Sigorney",
                LastName = "Weaver",
                BirthDate = new DateTime(1949, 8, 10)
            };

            var linda = new Actor
            {
                FirstName = "Linda",
                LastName = "Hamilton",
                BirthDate = new DateTime(1956, 9, 26)
            };

            var daisy = new Actor
            {
                FirstName = "Daisy",
                LastName = "Ridley",
                BirthDate = new DateTime(1992, 4, 10)
            };

            var nancy = new Actor
            {
                FirstName = "Nancy",
                LastName = "Allen",
                BirthDate = new DateTime(1950, 6, 24)
            };

            var gina = new Actor
            {
                FirstName = "Gina",
                LastName = "Torres",
                BirthDate = new DateTime(1969, 4, 25)
            };

            var jonathan = new Actor
            {
                FirstName = "Jonathan",
                LastName = "Frakes",
                BirthDate = new DateTime(1952, 8, 19)
            };

            var tom = new Actor
            {
                FirstName = "Tom",
                LastName = "Skerritt",
                BirthDate = new DateTime(1933, 8, 25)
            };

            var arnold = new Actor
            {
                FirstName = "Arnold",
                LastName = "Schwarzenegger",
                BirthDate = new DateTime(1947, 7, 30)
            };

            var boyega = new Actor
            {
                FirstName = "John",
                LastName = "Boyega",
                BirthDate = new DateTime(1992, 3, 17)
            };

            var weller = new Actor
            {
                FirstName = "Peter",
                LastName = "Weller",
                BirthDate = new DateTime(1947, 6, 24)
            };

            var nathan = new Actor
            {
                FirstName = "Nathan",
                LastName = "Fillion",
                BirthDate = new DateTime(1971, 3, 27)
            };

            var patrick = new Actor
            {
                FirstName = "Patrick",
                LastName = "Stewart",
                BirthDate = new DateTime(1940, 7, 13)
            };

            var tennant = new Actor
            {
                FirstName = "David",
                LastName = "Tennant",
                BirthDate = new DateTime(1971, 4, 18)
            };

            var coleman = new Actor
            {
                FirstName = "Jenna",
                LastName = "Coleman",
                BirthDate = new DateTime(1986, 4, 27)
            };

            context.Actors.Add(sigorney);
            context.Actors.Add(daisy);
            context.Actors.Add(nancy);
            context.Actors.Add(gina);
            context.Actors.Add(jonathan);
            context.Actors.Add(tom);
            context.Actors.Add(arnold);
            context.Actors.Add(boyega);
            context.Actors.Add(weller);
            context.Actors.Add(nathan);
            context.Actors.Add(patrick);
            context.Actors.Add(linda);
            context.Actors.Add(tennant);
            context.Actors.Add(coleman);

            context.SaveChanges();

            // Directors

            var ridley = new Director
            {
                FirstName = "Ridley",
                LastName = "Scott",
                BirthDate = new DateTime(1937, 11, 30)
            };

            var cameron = new Director
            {
                FirstName = "James",
                LastName = "Cameron",
                BirthDate = new DateTime(1954, 8, 16)
            };

            var abrams = new Director
            {
                FirstName = "J.J.",
                LastName = "Abrams",
                BirthDate = new DateTime(1966, 6, 27)
            };

            var verhoven = new Director
            {
                FirstName = "Paul",
                LastName = "Verhoeven",
                BirthDate = new DateTime(1938, 7, 18)
            };

            var whedon = new Director
            {
                FirstName = "Joss",
                LastName = "Whedon",
                BirthDate = new DateTime(1964, 6, 23)
            };

            var frakes = new Director
            {
                FirstName = "Jonathan",
                LastName = "Frakes",
                BirthDate = new DateTime(1952, 8, 19)
            };

            var moffat = new Director
            {
                FirstName = "Steven",
                LastName = "Moffat",
                BirthDate = new DateTime(1961, 11, 18)
            };

            var wheatley = new Director
            {
                FirstName = "Ben",
                LastName = "Wheatley"
            };

            var landau = new Director
            {
                FirstName = "Les",
                LastName = "Landau"
            };

            var scheerer = new Director
            {
                FirstName = "Robert",
                LastName = "Scheerer"
            };

            context.Directors.Add(ridley);
            context.Directors.Add(cameron);
            context.Directors.Add(abrams);
            context.Directors.Add(verhoven);
            context.Directors.Add(whedon);
            context.Directors.Add(frakes);
            context.Directors.Add(wheatley);
            context.Directors.Add(moffat);
            context.Directors.Add(landau);
            context.Directors.Add(scheerer);

            context.SaveChanges();

            // Studios

            var fox = new MovieStudio
            {
                Name = "20th Century-Fox"
            };

            var universal = new MovieStudio
            {
                Name = "Universal Pictures"
            };

            var carolco = new MovieStudio
            {
                Name = "Carolco Pictures"

            };

            var lucas = new MovieStudio
            {
                Name = "Lucasfilm"

            };

            var metro = new MovieStudio
            {
                Name = "Metro-Goldwyn-Mayer"
            };

            var paramount = new MovieStudio
            {
                Name = "Paramount Pictures"
            };

            context.MovieStudios.Add(fox);
            context.MovieStudios.Add(universal);
            context.MovieStudios.Add(carolco);
            context.MovieStudios.Add(lucas);
            context.MovieStudios.Add(metro);
            context.MovieStudios.Add(paramount);

            context.SaveChanges();

            // Movies

            var alien = new Movie
            {
                Title = "Alien",
                Year = 1979,
                Summary = @"The commercial vessel Nostromo receives a distress call from an unexplored planet. After searching for survivors, the crew heads home only to realize that a deadly bioform has joined them.",
                DirectorId = ridley.Id,
                StudioId = fox.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("http://i.telegraph.co.uk/multimedia/archive/03064/Alien-intro_3064438b.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            alien.Ratings.Add(new MovieRating
            {
                Value = 5,
                UserId = newton.Id,
            });

            alien.Ratings.Add(new MovieRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            alien.Comments.Add(new MovieComment
            {
                Content = "Scary and intriguing all the way!",
                AuthorId = testMaleUser.Id
            });

            alien.Comments.Add(new MovieComment
            {
                Content = "Beautiful",
                AuthorId = paranoidAndroid.Id
            });

            alien.Actors.Add(sigorney);
            alien.Actors.Add(tom);

            alien.Tags.Add(aliens);
            alien.Tags.Add(space);
            alien.Tags.Add(dark);
            alien.Tags.Add(survival);

            var terminatorTwo = new Movie
            {
                Title = "Terminator 2: Judgment Day",
                Year = 1991,
                Summary = @"A cyborg, identical to the one who failed to kill Sarah Connor, must now protect her young son, John Connor, from a more advanced cyborg, made out of liquid metal.",
                DirectorId = cameron.Id,
                StudioId = carolco.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("https://posterspy.com/wp-content/uploads/2014/08/TERMINATOR_POSTERSPY850px.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            terminatorTwo.Ratings.Add(new MovieRating
            {
                Value = 3,
                UserId = testFemaleUser.Id,
            });

            terminatorTwo.Ratings.Add(new MovieRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            terminatorTwo.Ratings.Add(new MovieRating
            {
                Value = 5,
                UserId = newton.Id,
            });

            terminatorTwo.Comments.Add(new MovieComment
            {
                Content = "Cyborgs and guns!",
                AuthorId = testMaleUser.Id
            });

            terminatorTwo.Comments.Add(new MovieComment
            {
                Content = "Thrilling!",
                AuthorId = ada.Id
            });

            terminatorTwo.Actors.Add(arnold);
            terminatorTwo.Actors.Add(nancy);

            terminatorTwo.Tags.Add(ai);
            terminatorTwo.Tags.Add(distopian);
            terminatorTwo.Tags.Add(cyborgs);

            var starWars = new Movie
            {
                Title = "Star Wars: Episode VII - The Force Awakens",
                Year = 2015,
                Summary = @"Three decades after the defeat of the Galactic Empire, a new threat arises. The First Order attempts to rule the galaxy and only a ragtag group of heroes can stop them, along with the help of the Resistance.",
                DirectorId = abrams.Id,
                StudioId = lucas.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("http://a.dilcdn.com/bl/wp-content/uploads/sites/6/2015/10/star-wars-force-awakens-official-poster.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            starWars.Ratings.Add(new MovieRating
            {
                Value = 3,
                UserId = turing.Id,
            });

            starWars.Actors.Add(daisy);
            starWars.Actors.Add(boyega);

            starWars.Tags.Add(space);

            var robocop = new Movie
            {
                Title = "Robocop",
                Year = 1987,
                Summary = @"In a dystopic and crime-ridden Detroit, a terminally wounded cop returns to the force as a powerful cyborg haunted by submerged memories.",
                DirectorId = verhoven.Id,
                StudioId = metro.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("http://www.goautographs.com/media/catalog/product/cache/1/image/800x/040ec09b1e35df139433887a97daa66f/2/2/22250MPMO.JPG"),
                    ImageFileExtention = "jpg"
                }
            };

            robocop.Ratings.Add(new MovieRating
            {
                Value = 5,
                UserId = testFemaleUser.Id,
            });

            robocop.Ratings.Add(new MovieRating
            {
                Value = 5,
                UserId = testMaleUser.Id,
            });

            robocop.Comments.Add(new MovieComment
            {
                Content = "Hands down best movie of all times!",
                AuthorId = testMaleUser.Id
            });

            robocop.Actors.Add(weller);
            robocop.Actors.Add(linda);

            robocop.Tags.Add(ai);
            robocop.Tags.Add(distopian);
            robocop.Tags.Add(cyborgs);

            var serenity = new Movie
            {
                Title = "Serenity",
                Year = 2005,
                Summary = @"The crew of the ship Serenity tries to evade an assassin sent to recapture one of their number who is telepathic.",
                DirectorId = whedon.Id,
                StudioId = universal.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("http://cdn.fontmeme.com/images/Serenity-Poster.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            serenity.Ratings.Add(new MovieRating
            {
                Value = 3,
                UserId = testFemaleUser.Id,
            });

            serenity.Ratings.Add(new MovieRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            serenity.Comments.Add(new MovieComment
            {
                Content = "Firefly!",
                AuthorId = paranoidAndroid.Id
            });

            serenity.Actors.Add(nathan);
            serenity.Actors.Add(gina);

            serenity.Tags.Add(space);
            serenity.Tags.Add(funny);

            var starTrek = new Movie
            {
                Title = "Star Trek: First Contact",
                Year = 1996,
                Summary = @"The Borg go back in time intent on preventing Earth's first contact with an alien species. Captain Picard and his crew pursue them to ensure that Zefram Cochrane makes his maiden flight reaching warp speed.",
                DirectorId = frakes.Id,
                StudioId = paramount.Id,
                Poster = new MoviePoster
                {
                    Image = webClient.DownloadData("http://i.imgur.com/pBiGr19.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            starTrek.Ratings.Add(new MovieRating
            {
                Value = 5,
                UserId = testFemaleUser.Id,
            });

            starTrek.Ratings.Add(new MovieRating
            {
                Value = 4,
                UserId = testMaleUser.Id,
            });

            starTrek.Comments.Add(new MovieComment
            {
                Content = "Best star trek movie",
                AuthorId = turing.Id
            });

            starTrek.Actors.Add(patrick);
            starTrek.Actors.Add(jonathan);

            starTrek.Tags.Add(space);
            starTrek.Tags.Add(aliens);
            starTrek.Tags.Add(dark);

            context.Movies.Add(alien);
            context.Movies.Add(terminatorTwo);
            context.Movies.Add(starWars);
            context.Movies.Add(robocop);
            context.Movies.Add(serenity);
            context.Movies.Add(starTrek);

            context.SaveChanges();

            // Channels

            var foxTv = new TvShowChannel
            {
                Name = "20th Century-Fox Television(Fox Tv)"
            };

            var nbc = new TvShowChannel
            {
                Name = "National Broadcasting Company(NBC)"
            };

            var bbc = new TvShowChannel
            {
                Name = "British Broadcasting Corporation(BBC)"
            };

            context.TvShowChannels.Add(foxTv);
            context.TvShowChannels.Add(nbc);
            context.TvShowChannels.Add(bbc);

            context.SaveChanges();

            // TvShows
            var firefly = new TvShow
            {
                Title = "Firefly",
                StartYear = 2002,
                EndYear = 2003,
                Summary = @"Five hundred years in the future, a renegade crew aboard a small spacecraft tries to survive as they travel the unknown parts of the galaxy and evade warring factions as well as authority agents out to get them.",
                ChannelId = foxTv.Id,
                Poster = new TvShowPoster
                {
                    Image = webClient.DownloadData("https://gooreviewsonline.files.wordpress.com/2012/11/firefly-poster-2.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            firefly.Ratings.Add(new TvShowRating
            {
                Value = 5,
                UserId = testFemaleUser.Id,
            });

            firefly.Ratings.Add(new TvShowRating
            {
                Value = 5,
                UserId = testMaleUser.Id,
            });

            firefly.Comments.Add(new TvShowComment
            {
                Content = "Very nice but only one season :(",
                AuthorId = turing.Id
            });

            firefly.Actors.Add(nathan);
            firefly.Actors.Add(gina);
            firefly.Directors.Add(whedon);

            firefly.Tags.Add(space);
            firefly.Tags.Add(funny);

            var starTrekTv = new TvShow
            {
                Title = "Star Trek: The Next Generation",
                StartYear = 1987,
                EndYear = 1994,
                Summary = @"Set decades after Captain Kirk's five-year mission, a new generation of Starfleet officers set off in a new Enterprise on their own mission to go where no one has gone before.",
                ChannelId = nbc.Id,
                Poster = new TvShowPoster
                {
                    Image = webClient.DownloadData("https://fanart.tv/fanart/tv/71470/tvposter/star-trek-the-next-generation-521b936d8d1d0.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            starTrekTv.Ratings.Add(new TvShowRating
            {
                Value = 5,
                UserId = testFemaleUser.Id,
            });

            starTrekTv.Ratings.Add(new TvShowRating
            {
                Value = 5,
                UserId = testMaleUser.Id,
            });

            starTrekTv.Comments.Add(new TvShowComment
            {
                Content = "The essential sci fi series",
                AuthorId = turing.Id
            });

            starTrekTv.Actors.Add(patrick);
            starTrekTv.Actors.Add(jonathan);
            starTrekTv.Directors.Add(landau);
            starTrekTv.Directors.Add(scheerer);

            starTrekTv.Tags.Add(space);
            starTrekTv.Tags.Add(exploration);

            var who = new TvShow
            {
                Title = "Doctor Who",
                StartYear = 2005,
                Summary = @"The further adventures of the time traveling alien adventurer and his companions.",
                ChannelId = bbc.Id,
                Poster = new TvShowPoster
                {
                    Image = webClient.DownloadData("http://merchandise.thedoctorwhosite.co.uk/wp-content/uploads/11-doctors-poster-1.jpg"),
                    ImageFileExtention = "jpg"
                }
            };

            who.Ratings.Add(new TvShowRating
            {
                Value = 4,
                UserId = testFemaleUser.Id,
            });

            who.Comments.Add(new TvShowComment
            {
                Content = "Very Entertaining!",
                AuthorId = turing.Id
            });

            who.Actors.Add(tennant);
            who.Actors.Add(coleman);
            who.Directors.Add(wheatley);
            who.Directors.Add(moffat);

            who.Tags.Add(space);
            who.Tags.Add(timeTravel);
            who.Tags.Add(funny);
            who.Tags.Add(aliens);

            context.TvShows.Add(firefly);
            context.TvShows.Add(starTrekTv);
            context.TvShows.Add(who);
            context.SaveChanges();
        }