public List<Book> LoadToList()
        {
            List<Book> books = new List<Book>();

            try
            {
                XDocument document = XDocument.Load(filePath);
                var boooks = document.Elements("books").Elements("book");

                foreach (XElement e in boooks)
                {
                    Book b = new Book();
                    b.Title = e.Element("title").Value;
                    b.Author = e.Element("author").Value;
                    b.Publiser = e.Element("publisher").Value;
                    b.Year = int.Parse(e.Element("year").Value);
                    b.NumberOfPages = int.Parse(e.Element("numberOfPages").Value);

                    books.Add(b);
                }
                return books;
            }
            catch (FileNotFoundException)
            {
                throw new InvalidDataException("Error while saving");
            }
        }
        static void Main(string[] args)
        {
            FileRepository frep = new FileRepository();
            //XMLRepository xrep = new XMLRepository();
            //LINQ2XMLRepository linqrep = new LINQ2XMLRepository();

            BookList list = new BookList(frep);
            Book b1 = new Book() 
            { 
                Author = "Tolstoy",
                Title = "War and peace",
                Year = 1869,
                NumberOfPages = 1274,
                Publiser = "Moskva"
            };
            Book b2 = new Book() 
            { 
                Author = "Tolstoy",
                Title = "Anna Karenina",
                Year = 1877,
                NumberOfPages = 723,
                Publiser = "Russkii vestnik"
            };
            list.AddBook(b1);
            list.AddBook(b2);

            Book b = list.FindBookByTag(x => x.Publiser == "Moskva");
            Console.WriteLine(b.ToString());

            Console.ReadLine();
        }
        public List<Book> LoadToList()
        {
            List<Book> books = new List<Book>();

            try
            {
                using (BinaryReader reader = new BinaryReader(File.OpenRead(filePath)))
                {
                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        Book b = new Book();
                        b.Author = reader.ReadString();
                        b.Title = reader.ReadString();
                        b.Publiser = reader.ReadString();
                        b.NumberOfPages = reader.ReadInt32();
                        b.Year = reader.ReadInt32();
                        books.Add(b);
                    }
                }

                return books;
            }
            catch (FileNotFoundException)
            {
                throw new InvalidDataException("File not found");
            }
            catch (IOException)
            {
                throw new InvalidOperationException("Cannot load file");
            }
        }
Esempio n. 4
0
        private static void FillDb() {
            using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession())) {
                using(ITransaction tx = s.BeginTransaction()){
                    Book b1 = new Book(1,
                        "Eric Evans",
                        "Domain-Driven Design: Tackling Complexity in the Heart of Software",
                        "This book provides a broad framework for making design decisions and a technical vocabulary for discussing domain design. It is a synthesis of widely accepted best practices along with the author's own insights and experiences."
                        );

                    s.Save(b1);

                    Book b2 = new Book(2,
                        "Pierre Kuate",
                        "NHibernate in Action",
                        "In the classic style of Manning's 'In Action' series, NHibernate in Action introduces .NET developers to the NHibernate Object/Relational Mapping tool. As NHibernate is a port of Hibernate from Java to .NET.");
                    s.Save(b2);

                    Book b3 = new Book(3,
                        "John Doe",
                        "Foo book NHibernate",
                        "Foo series book");
                    s.Save(b3);

                    s.Flush();
                    tx.Commit();
                }
            }
        }
 public void Remove(Book book)
 {
     if (book == null) throw new ArgumentNullException();
     if (Contains(book))
     {
         List<Book> books = GetAll();
         books.Remove(book);
         SaveAll(books);
     }
     else throw new ArgumentException();
 }
        public List<Book> LoadToList()
        {
            List<Book> books = new List<Book>();

            try
            {
                using (var reader = XmlReader.Create(filePath))
                {
                    while (reader.Read())
                    {

                        Book book = new Book();
                        //reader.ReadToFollowing("book");
                        if (!reader.ReadToFollowing("title"))
                            break;
                        else
                        {
                            book.Title = reader.ReadElementContentAsString();
                            Console.WriteLine(book.Title);
                            reader.ReadToFollowing("author");
                            book.Author = reader.ReadElementContentAsString();
                            Console.WriteLine(book.Author);
                            reader.ReadToFollowing("publisher");
                            book.Publiser = reader.ReadElementContentAsString();
                            Console.WriteLine(book.Publiser);
                            reader.ReadToFollowing("year");
                            book.Year = reader.ReadElementContentAsInt();
                            Console.WriteLine(book.Year);
                            reader.ReadToFollowing("numberOfPages");
                            book.NumberOfPages = reader.ReadElementContentAsInt();
                            Console.WriteLine(book.NumberOfPages);
                            books.Add(book);
                        }
                    }
                }
                return books;
            }
            catch (XmlException)
            {
                throw new InvalidDataException("Invalid xml document");
            }
            catch (FileNotFoundException)
            {
                throw new InvalidDataException("Error while saving");
            }
        }
 public  void Save(Book book)
 {
     if (book == null) throw new ArgumentNullException();
     if (File.Exists(path))
     {
         if (Contains(book)) throw new ArgumentException();
         List<Book> books = GetAll();
         books.Add(book);
         SaveAll(books);
     }
     else
     using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate)))
     {
         writer.Write(book.Author);
         writer.Write(book.Title);
         writer.Write(book.Genre);
         writer.Write(book.YearOfCreating);
     }
 }
        /// <summary>
        /// Removes some book from repository (if this book was there)
        /// </summary>
        /// <param name="book">Some book to remove</param>
        public void RemoveBook(Book book)
        {
            try
            {
                if (book == null)
                    throw new ArgumentException("Book is null");

                books = repository.LoadToList();
                if (!books.Contains(book))
                    throw new ArgumentException("There is no this book in booklist");
                else
                {
                    books.Remove(book);
                    logger.Info("Book was removed successfully");
                    repository.LoadToFile(books);
                }
            }
            catch (Exception e)
            {
                logger.Info(e.Message);
                logger.Error(e.StackTrace);
            }
        }
        /// <summary>
        /// Adds new book in repository(if it was not there)
        /// </summary>
        /// <param name="book">Some new book</param>
        public void AddBook(Book book)
        {
            try
            {
                if (book == null)
                    throw new ArgumentNullException("Book is null");

                books = repository.LoadToList();
                if (books.Contains(book))
                    throw new ArgumentException("Book is already in booklist");
                else
                {
                    books.Add(book);
                    logger.Info("Book was added successfully");
                    repository.LoadToFile(books);
                }
            }
            catch (Exception e)
            {
                logger.Info(e.Message);
                logger.Error(e.StackTrace);
            }
        }
Esempio n. 10
0
        private static Book GetBookDetails()
        {
            Console.Write("Introduzca el identificador del libro: ");
            var identifier = Console.ReadLine();
            Console.Write("Introduzca el nombre del libro:");
            var nombreLibro = Console.ReadLine();

            var item = new Book() { ID = identifier, Name = nombreLibro };
            return item;
        }
Esempio n. 11
0
 /// <summary>
 /// Checks if booklist has such book
 /// </summary>
 /// <param name="book">Book to check</param>
 /// <returns>True if book is already in booklist, else false</returns>
 private bool Contains(Book book)
 {
     if (book == null)
         throw new ArgumentNullException("Book is null");
     foreach (Book b in books)
     {
         if (b.Equals(book))
             return true;
     }
     return false;
 }
Esempio n. 12
0
        /// <summary>
        /// Saves or updates a book
        /// </summary>
        /// <param name="book">A book</param>
        public void SaveOrUpdate(Book book)
        {
            if (book == null) {
                throw new ArgumentNullException("book");
            }

            this.Session.SaveOrUpdate(book);
        }
 bool Contains (Book book)
 {
     List<Book> books = GetAll();
     return books.Contains(book); 
 }