public IEnumerable <Book> Load()
        {
            List <Book> bookList = new List <Book>();

            using (var fs = new FileStream(this._fileStorage, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
            {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        var readedBook = LoadData(reader);

                        if (readedBook == null)
                        {
                            BookLogger.Error($"{nameof(readedBook)} is null");
                            throw new ArgumentNullException(nameof(readedBook));
                        }

                        bookList.Add(readedBook);
                    }
                }
            }

            BookLogger.Info($"List books was loaded");
            return(bookList);
        }
        public Book Update(Book model)
        {
            if (model == null)
            {
                BookLogger.Error($"{nameof(model)} is null");
                throw new ArgumentNullException(nameof(model));
            }

            var deletedBook = Get(model.ISBN);

            if (deletedBook == null)
            {
                BookLogger.Error($"Book with ISBN {model.ISBN} doesn't exists in the storage");
                throw new DeleteBookException(model.Name, model.ISBN);
            }

            this._listBooks = this._listBooks.Except(new List <Book> {
                deletedBook
            });
            this._listBooks = this._listBooks.Concat(new[] { model });

            SaveBookList();
            BookLogger.Info($"Book with ISBN {model.ISBN} was updated");
            return(model);
        }
        public Book Add(Book model)
        {
            if (model == null)
            {
                BookLogger.Error($"{nameof(model)}is null");
                throw new ArgumentNullException(nameof(model));
            }

            if (this._listBooks.ToList().Contains(model))
            {
                BookLogger.Error($"Book with ISBN {model.ISBN} is alredy exists");
                throw new AddBookException(model.Name, model.ISBN);
            }

            using (var fs = new FileStream(this._fileStorage, FileMode.Append, FileAccess.Write, FileShare.Read))
            {
                using (BinaryWriter writer = new BinaryWriter(fs))
                {
                    SaveBook(writer, model);
                    BookLogger.Info($"Book with ISBN {model.ISBN} was added to storage");
                }
            }

            this._listBooks = this._listBooks.Concat(new[] { model });

            return(model);
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            string _fileSource = @"D:\Test\BookStorage.txt";

            if (File.Exists(_fileSource))
            {
                File.Delete(_fileSource);
            }

            IBookListService <Book> _bookService = new BookListService(new BookListStorage(_fileSource));

            _bookService.AddBook(new ScientificBook("978-0735667457", "Richter", "CLR via C#", "O'REILlY", 2013, 896, 176));
            _bookService.AddBook(new ScientificBook("978-5-84592087-4", "Albahary", "C# in nutshell", "O'REILlY", 2017, 1040, 250));
            _bookService.AddBook(new ScientificBook("0-321-12742-0", "Fauler", "Architecture of corporate software applications", "Williams", 2006, 541, 90));
            _bookService.AddBook(new ScientificBook("978-1509304066", "Chambers", "ASP .Net Core application development", "Microsot Press", 2017, 464, 70));

            BookLogger.Debug("Tests");

            var books = _bookService.GetAllBooks();

            PrintArray(books);

            _bookService.AddBook(new ScientificBook("1111111", "Test", "Test", "Test", 2013, 896, 176));
            books = _bookService.GetAllBooks();
            PrintArray(books);

            books = _bookService.SortBookByTag(new YearComparer());
            PrintArray(books);

            _bookService.RemoveBook(new ScientificBook("1111111", "Test", "Test", "Test", 2013, 896, 176));
            books = _bookService.SortBookByTag(new YearComparer());
            PrintArray(books);

            Console.ReadLine();
        }
        /// <summary>
        /// Sorts book by tags name
        /// </summary>
        /// <param name="comparer">Tags name</param>
        /// <returns>Enumeration of books</returns>
        public IEnumerable <Book> SortBookByTag(IComparer <Book> comparer)
        {
            if (comparer == null)
            {
                BookLogger.Error($"{nameof(comparer)} is null value");
                throw new ArgumentNullException(nameof(comparer));
            }

            return(this._bookRepository.SortByTag(comparer));
        }
        /// <summary>
        /// Finds book by tags name and value
        /// </summary>
        /// <param name="filter">Filter</param>
        /// <returns>Enumeration of found books</returns>
        public IEnumerable <Book> FindBookByTag(Predicate <Book> filter)
        {
            if (filter == null)
            {
                BookLogger.Error($"{nameof(filter)} is null value");
                throw new ArgumentNullException(nameof(filter));
            }

            return(this._bookRepository.Find(filter));
        }
        /// <summary>
        /// Removes book
        /// </summary>
        /// <param name="book">Book</param>
        /// <returns><value>Book if it is removed</value>
        /// <value>null -otherwise</value></returns>
        public Book RemoveBook(Book book)
        {
            if (book == null)
            {
                BookLogger.Error($"{nameof(book)} is null value");
                throw new ArgumentNullException(nameof(book));
            }

            return(this._bookRepository.Delete(book));
        }
        /// <summary>
        /// Initialize a new instance of <see cref="BookListService"/>
        /// </summary>
        /// <param name="bookRepository">Books storage</param>
        public BookListService(IBookRepository bookRepository)
        {
            if (bookRepository == null)
            {
                BookLogger.Fatal($"{nameof(bookRepository)} is null value");
                throw new ArgumentNullException(nameof(bookRepository));
            }

            this._bookRepository = bookRepository;
            BookLogger.Debug($"{nameof(BookListService)} was created");
        }
        private Book LoadData(BinaryReader reader)
        {
            var isbn       = reader.ReadString();
            var author     = reader.ReadString();
            var name       = reader.ReadString();
            var publishing = reader.ReadString();
            var year       = reader.ReadUInt32();
            var pageCount  = reader.ReadUInt32();
            var price      = reader.ReadDecimal();

            BookLogger.Info($"Book with {isbn} was loaded");
            return(new ScientificBook(isbn, author, name, publishing, year, pageCount, price));
        }
        private void SaveBook(BinaryWriter writer, Book book)
        {
            writer.Write(book.ISBN);
            writer.Write(book.Author);
            writer.Write(book.Name);
            writer.Write(book.Publishig);
            writer.Write(book.Year);
            writer.Write(book.PageCount);
            writer.Write(book.Price);
            writer.Flush();

            BookLogger.Info($"Book with {book.ISBN} was saved");
        }
Exemplo n.º 11
0
        public BookListServiceTests()
        {
            if (File.Exists(this._fileSource))
            {
                File.Delete(this._fileSource);
            }

            this._bookService = new BookListService(new BookListStorage(this._fileSource));
            _bookService.AddBook(new ScientificBook("978-0735667457", "Richter", "CLR via C#", "O'REILlY", 2013, 896, 176));
            _bookService.AddBook(new ScientificBook("978-5-84592087-4", "Albahary", "C# in nutshell", "O'REILlY", 2017, 1040, 250));
            _bookService.AddBook(new ScientificBook("0-321-12742-0", "Fauler", "Architecture of corporate software applications", "Williams", 2006, 541, 90));
            _bookService.AddBook(new ScientificBook("978-1509304066", "Chambers", "ASP .Net Core application development", "Microsot Press", 2017, 464, 70));

            BookLogger.Debug("Tests");
        }
        private void SaveBookList()
        {
            using (var fs = new FileStream(this._fileStorage, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                using (BinaryWriter writer = new BinaryWriter(fs))
                {
                    foreach (var book in this._listBooks)
                    {
                        SaveBook(writer, book);
                    }

                    BookLogger.Info($"Books list was saved");
                }
            }
        }
        public IEnumerable <Book> SortByTag(IComparer <Book> comparer)
        {
            if (comparer == null)
            {
                BookLogger.Error($"{nameof(comparer)} is null");
                throw new ArgumentNullException(nameof(comparer));
            }

            var sortedList = this._listBooks.ToList();

            sortedList.Sort(comparer);

            BookLogger.Info($"Books list was sorted");
            return(sortedList);
        }
        /// <summary>
        /// Gets instance of books storage
        /// </summary>
        /// <param name="fileStorage">Source file name</param>
        public BookListStorage(string fileStorage)
        {
            if (string.IsNullOrWhiteSpace(fileStorage))
            {
                throw new ArgumentNullException($"{nameof(fileStorage)}");
            }

            this._fileStorage = fileStorage;
            this._listBooks   = this.Load();
            if (_listBooks == null)
            {
                BookLogger.Fatal($"{nameof(BookListStorage)} wasn't created");
                throw new ArgumentNullException(nameof(_listBooks));
            }

            BookLogger.Debug($"{nameof(BookListStorage)} was created");
        }
        public Book Get(Book model)
        {
            var findElement = this.Find(x => x.ISBN == model.ISBN);

            if (!findElement.Any())
            {
                BookLogger.Warn($"{nameof(findElement)} with ISBN {model.ISBN} doesn't exists in the storage");
                return(null);
            }

            if (findElement.Count() > 1)
            {
                BookLogger.Error($"{nameof(findElement)} with ISBN {model.ISBN} shoud be one");
                throw new GetBookException(model.ISBN);
            }

            return(findElement.ToArray()[0]);
        }
Exemplo n.º 16
0
 public void ClearTest()
 {
     BookLogger.Debug("Test done");
 }
Exemplo n.º 17
0
 public void InitialisationsTestMethod()
 {
     BookLogger.Debug("Test run");
 }
 public IEnumerable <Book> GetAllElements()
 {
     BookLogger.Info($"Method runs GetAllElements");
     return(this._listBooks);
 }