コード例 #1
0
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                repository.Create(book);

                return RedirectToAction("Index");
            }

            return View(book);
        }
コード例 #2
0
ファイル: BookManager.cs プロジェクト: iMitaka/HackBulgaria
        public static void CreateNewBook(BookManagerDbContext data)
        {
            Console.Write("Title: ");
            string title = Console.ReadLine();
            Console.WriteLine("Author:");
            Console.Write("-First Name: ");
            string firstName = Console.ReadLine();
            Console.Write("-Last Name: ");
            string lastName = Console.ReadLine();
            Console.Write("-Year Born: ");
            DateTime born = DateTime.Parse(Console.ReadLine());
            Console.Write("-Year Died: ");
            DateTime died = DateTime.Parse(Console.ReadLine());
            Console.Write("Description: ");
            string description = Console.ReadLine();
            Console.Write("Date Published: ");
            DateTime published = DateTime.Parse(Console.ReadLine());
            Console.Write("Book Publisher: ");
            string publisher = Console.ReadLine();
            List<Genre> genres = new List<Genre>();
            Console.WriteLine("Select Book Genres:");
            Console.WriteLine("1. Comedy");
            Console.WriteLine("2. Action");
            Console.WriteLine("3. Fantasy");
            Console.Write("Enter your choises: ");
            string numbers = Console.ReadLine();
            string[] numbersSplited = numbers.Split(' ');
            foreach (var number in numbersSplited)
            {
                SelectGenres(genres, int.Parse(number), data);
            }
            Console.Write("Pages: ");
            int pages = int.Parse(Console.ReadLine());
            Console.Write("ISBN: ");
            string isbn = Console.ReadLine();

            var hasAuthor = data.Authors.Any(a => a.FirstName == firstName && a.LastName == lastName && a.YearBorn == born && a.YearDied == died);
            if (!hasAuthor)
            {
                var newAuthor = new Author() { FirstName = firstName, LastName = lastName, YearBorn = born, YearDied = died };
                var book = new Book() { Author = newAuthor, DatePublished = published, Description = description, Genres = genres, ISBN = isbn, Pages = pages, Title = title, Publisher = publisher, isLoan = false };
                newAuthor.Books.Add(book);

                data.Authors.Add(newAuthor);

                data.SaveChanges();
            }
            else
            {
                var author = data.Authors.Where(a => a.FirstName == firstName && a.LastName == lastName && a.YearBorn == born && a.YearDied == died).FirstOrDefault();
                var book = new Book() { Author = author, DatePublished = published, Description = description, Genres = genres, ISBN = isbn, Pages = pages, Title = title, Publisher = publisher, isLoan = false };
                author.Books.Add(book);

                data.SaveChanges();
            }
        }