public ActionResult Upload(HttpPostedFileBase fileContent)
        {
            BooksRepository r = new BooksRepository();

            string extension = Path.GetExtension(fileContent.FileName);

            if (extension.ToLower() == ".json")
            {
                StreamReader sr   = new StreamReader(fileContent.InputStream);
                string       body = sr.ReadToEnd();

                //System.IO.File.AppendAllText("c:\\myfile.json", body);
                //string myFileData = System.IO.File.ReadAllText("c:\\myfile.json");


                //var myFileStream = System.IO.File.Open("c:\\myfile.json", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                //var myFileStream = System.IO.File.OpenRead("c:\\myfile.json");
                //StreamReader fileReader = new StreamReader(myFileStream);
                //fileReader.ReadLine();

                var books = JsonConvert.DeserializeObject <IEnumerable <BookModel> >(body).ToList();

                foreach (BookModel b in books)
                {
                    b.Id = 0;
                    r.Add(b);
                }
            }
            else if (extension.ToLower() == ".csv")
            {
                StreamReader sr = new StreamReader(fileContent.InputStream);
                while (!sr.EndOfStream)
                {
                    string    row    = sr.ReadLine();
                    string[]  fields = row.Split(',', ';');
                    BookModel b      = new BookModel
                    {
                        Name       = fields[0].Trim().Trim('"'),
                        AuthorName = fields[1].Trim().Trim('"'),
                        ISBN       = fields[2].Trim().Trim('"'),
                        Year       = int.Parse(fields[3].Trim().Trim('"'))
                    };

                    r.Add(b);
                }
            }

            return(RedirectToAction("List"));
        }
示例#2
0
        private static void Main(string[] args)
        {
            var rnd       = new Random();
            var booksRepo = new BooksRepository();

            booksRepo.Add(
                new RepoAuthor
            {
                FirstName = "Damon",
                LastName  = "German"
            },
                new RepoTitle
            {
                BookTitle     = "Why it's Hard to Write Clean Code",
                Copyright     = "1998",
                EditionNumber = 5,
                ISBN          = GetRandomString(rnd)
            },
                new RepoTitle
            {
                BookTitle     = "Why Map is my Favorite Data Structure",
                Copyright     = "2007",
                EditionNumber = 1,
                ISBN          = GetRandomString(rnd)
            }
                );

            DisplayAuthorsAndTitles(booksRepo);
            DisplayAuthorsAndISBNs(booksRepo);
        }
示例#3
0
        private void CreateEdit(object sender, EventArgs e)
        {
            if (!CheckInputFields())
            {
                return;
            }

            if (_bookToEdit == null)
            {
                var newBook = new Book
                {
                    Name           = nameTextBox.Text,
                    Genre          = (Genre)genreComboBox.SelectedItem,
                    NumberOfCopies = int.Parse(numberOfCopiesTextBox.Text),
                    NumberOfPages  = int.Parse(numberOfPagesTextBox.Text),
                    AuthorId       = ((Author)authorsListBox.SelectedItem).Id,
                    PublisherId    = ((Publisher)publishersListBox.SelectedItem).Id,
                };

                _booksRepository.Add(newBook);
            }
            else
            {
                _bookToEdit.Name           = nameTextBox.Text;
                _bookToEdit.AuthorId       = ((Author)authorsListBox.SelectedItem).Id;
                _bookToEdit.PublisherId    = ((Publisher)publishersListBox.SelectedItem).Id;
                _bookToEdit.Genre          = (Genre)genreComboBox.SelectedItem;
                _bookToEdit.NumberOfPages  = int.Parse(numberOfPagesTextBox.Text);
                _bookToEdit.NumberOfCopies = int.Parse(numberOfCopiesTextBox.Text);

                _booksRepository.Edit(_bookToEdit);
            }

            Close();
        }
        public ActionResult Add(BookModel book)
        {
            //bookList.Add(book);
            BooksRepository r = new BooksRepository();

            r.Add(book);

            return(View("AddSuccess"));
        }
示例#5
0
        public ActionResult Add(BookModel book)
        {
            BooksRepository r = new BooksRepository();

            r.Add(book);
            //listaCarti.Add(book);

            return(View("Succes"));
        }
示例#6
0
        public static void AddBook()
        {
            var bookRepo = new BooksRepository();

            bookRepo.Add(new Book {
                Title = "Som man bäddar får man ligga", ReleaseDate = new DateTime(2007, 10, 7)
            });
            bookRepo.Save();
            Console.WriteLine("Added new book");
        }
示例#7
0
        public ActionResult Upload(HttpPostedFileBase fileContent)
        {
            string extension = Path.GetExtension(fileContent.FileName);

            if (extension.ToLower() == ".json")
            {
                StreamReader sr   = new StreamReader(fileContent.InputStream);
                string       body = sr.ReadToEnd();


                var books = JsonConvert.DeserializeObject <IEnumerable <BookModel> >(body).ToList();

                BooksRepository r = new BooksRepository();

                foreach (BookModel b in books)
                {
                    b.Id = 0;
                    r.Add(b);
                }
            }
            else if (extension.ToLower() == ".csv")
            {
                StreamReader    sr = new StreamReader(fileContent.InputStream);
                BooksRepository r  = new BooksRepository();
                while (!sr.EndOfStream)
                {
                    string    row    = sr.ReadLine();
                    string[]  fields = row.Split(',', ';');
                    BookModel b      = new BookModel
                    {
                        Name       = fields[0],
                        AuthorName = fields[1],
                        ISBN       = fields[2],
                        Year       = int.Parse(fields[3])
                    };

                    r.Add(b);
                }
            }



            return(RedirectToAction("List"));
        }
 private void AddEditBookForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (DialogResult == DialogResult.OK)
     {
         if (string.IsNullOrEmpty(textBoxName.Text) || string.IsNullOrEmpty(textBoxAuthor.Text))
         {
             MessageBox.Show("Введите название книги и автора", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
             textBoxName.Focus();
             e.Cancel = true;
             return;
         }
         SetDataFromControls();
         if (state == AddEditFormState.Add)
         {
             booksRepository.Add(book);
         }
         else
         {
             booksRepository.Update(book);
         }
         e.Cancel = false;
     }
 }
示例#9
0
        // POST: api/Books
        public int Post([FromBody] Book book)
        {
            BooksRepository booksRepo = new BooksRepository();

            return(booksRepo.Add(book));
        }
示例#10
0
 public int Add(Book _Book)
 {
     return(_BooksRepository.Add(_Book));
 }
示例#11
0
 internal Book Add(Book newBook)
 {
     return(_repo.Add(newBook));
 }
示例#12
0
        public string Add(BookRequestDTO book)
        {
            var message = _bookRepository.Add(book);

            return(message);
        }
        public string get()
        {
            using (var ctx = new libraryContext())
            {
                var AuthorRepo    = new AuthorRepository(ctx);
                var PublisherRepo = new PublisherRepository(ctx);
                var BooksRepo     = new BooksRepository(ctx);
                try
                {
                    var authorFile = @"E:\OneDrive - The University of Texas at Dallas\Spring18\DB\Projects\project1\books.csv";
                    //var borrowerFile = "";
                    List <Model>     list       = new List <Model>();
                    var              fileStream = new FileStream(authorFile, FileMode.Open, FileAccess.Read);
                    HashSet <string> authors    = new HashSet <string>();
                    HashSet <string> publishers = new HashSet <string>();
                    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
                    {
                        string line;
                        line = streamReader.ReadLine();
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            var temp = line.Split('\t');
                            foreach (var author in temp[3].Split(','))
                            {
                                authors.Add(author);
                            }

                            publishers.Add(temp[5]);

                            int pages = Int16.Parse(temp[6]);
                            var model = new Model(temp[0], temp[2], temp[3].Split(','), temp[4], temp[5], pages);
                            list.Add(model);
                            //BooksRepo.Add(temp[0], temp[2], temp[5], Int16.Parse(temp[6]), temp[4], temp[3].Split(','));
                            //ctx.SaveChanges();
                        }
                    }
                    foreach (var author in authors)
                    {
                        AuthorRepo.Add(author);
                    }
                    foreach (var publisher in publishers)
                    {
                        PublisherRepo.Add(publisher);
                    }
                    ctx.SaveChanges();
                    var count = 0;
                    foreach (var item in list)
                    {
                        count++;
                        BooksRepo.Add(item.isbn, item.title, item.cover, item.pages, item.publisher, item.authors);
                        if (count % 5000 == 0)
                        {
                            Console.Write(ctx.SaveChanges());
                        }
                    }
                    ctx.SaveChanges();
                }
                catch (Exception e)
                {
                    return(e.Message);
                }
                return("Success");
            }
        }