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); }
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); }
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"); }
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 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); }
public IEnumerable <Book> GetAllElements() { BookLogger.Info($"Method runs GetAllElements"); return(this._listBooks); }