// Read all bytes from stream as book item private Book ReadFromStream(BinaryReader binaryReader) { if (binaryReader != null) { int id = binaryReader.ReadInt32(); string isbn = binaryReader.ReadString(); string author = binaryReader.ReadString(); string name = binaryReader.ReadString(); string publisher = binaryReader.ReadString(); int year = binaryReader.ReadInt32(); int numberOfPages = binaryReader.ReadInt32(); decimal price = binaryReader.ReadDecimal(); return(new Book { Id = id, ISBN = isbn, Author = author, Name = name, Publisher = publisher, Year = year, NumberOfPages = numberOfPages, Price = price }); } else { MyLogger.GetLogger().Info("Argument Null Exception!"); throw new ArgumentNullException(); } }
/// <summary> /// Update(edit) a book /// </summary> /// <param name="book"> Book to update </param> public void Update(Book book) { if (book != null) { var results = new List <ValidationResult>(); var context = new ValidationContext(book); if (!Validator.TryValidateObject(book, context, results, true)) { foreach (var error in results) { MyLogger.GetLogger().Info(error.ErrorMessage); } } else { Book b = GetById(book.Id); if (b != null) { b.ISBN = book.ISBN; b.Author = book.Author; b.Name = book.Name; b.Publisher = book.Publisher; b.Year = book.Year; b.NumberOfPages = book.NumberOfPages; b.Price = book.Price; _bookStorage.Save(); } } } }
/// <summary> /// Add new book into BookStorage /// </summary> /// <param name="book"> Book to add </param> public void Add(Book book) { if (book != null) { var results = new List <ValidationResult>(); var context = new ValidationContext(book); if (!Validator.TryValidateObject(book, context, results, true)) { foreach (var error in results) { MyLogger.GetLogger().Info(error.ErrorMessage); } } else { if (!_books.Contains(book)) { int id = _books.Max(b => b.Id) + 1; book.Id = id; _books.Add(book); _bookStorage.Save(); } else { MyLogger.GetLogger().Info("Duplicated book!"); throw new ArgumentException("Duplicated book!"); } } } }
/// <summary> /// Load from binary file /// </summary> /// <returns> Collection of books </returns> public IEnumerable <Book> Load() { if (File.Exists(_fullPath)) { _storage.Clear(); using (FileStream stream = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader binaryReader = new BinaryReader(stream)) { binaryReader.BaseStream.Seek(0, SeekOrigin.Begin); //// Read each value while not EOF while (binaryReader.PeekChar() != -1) // for FileStream binaryReader.PeekChar() != -1 for MemoryStream binaryReader.PeekChar() != 0 { _storage.Add(ReadFromStream(binaryReader)); } } } MyLogger.GetLogger().Info("Binary file was successfully loaded!"); return(_storage); } else { MyLogger.GetLogger().Info("Binary file Not Found!"); throw new FileNotFoundException(); } }
/// <summary> /// Get a book by Id /// </summary> /// <param name="id"> Id </param> /// <returns> Book from BookStorage </returns> public Book GetById(int id) { Book book = _books.Where(b => b.Id.Equals(id)).FirstOrDefault(); if (book == null) { MyLogger.GetLogger().Info("There is no such book in the BookStorage!"); throw new NullReferenceException("There is no such book in the BookStorage!"); } return(book); }
/// <summary> /// Remove a book with a given id from BookStorage /// </summary> /// <param name="id"> Id </param> public void Remove(int id) { Book book = GetById(id); if (book != null) { _books.Remove(book); _bookStorage.Save(); } else { MyLogger.GetLogger().Info("There is no such book in the BookStorage!"); throw new ArgumentException("There is no such book in the BookStorage!"); } }
/// <summary> /// Save to binary file /// </summary> public void Save() { using (FileStream stream = new FileStream(_fullPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (BinaryWriter binaryWriter = new BinaryWriter(stream)) { foreach (var item in _storage) { WriteToStream(binaryWriter, item); } } } MyLogger.GetLogger().Info("Binary file was successfully saved!"); }
// Write book as array of bytes to stream private void WriteToStream(BinaryWriter binaryWriter, Book book) { if (book != null && binaryWriter != null) { binaryWriter.Write(book.Id); binaryWriter.Write(book.ISBN); binaryWriter.Write(book.Author); binaryWriter.Write(book.Name); binaryWriter.Write(book.Publisher); binaryWriter.Write(book.Year); binaryWriter.Write(book.NumberOfPages); binaryWriter.Write(book.Price); } else { MyLogger.GetLogger().Info("Argument Null Exception!"); throw new ArgumentNullException(); } }
/// <summary> /// Parse a collection of url /// </summary> public IEnumerable <UrlAddress> Parse() { foreach (var item in _uris) { if (!_validator.IsValid(item)) { MyLogger.GetLogger().Info($"Invalid Url: {item}"); continue; } Uri uri = new Uri(item); _urlAddresses.Add(new UrlAddress { Scheme = uri.Scheme, Host = uri.Host, Segments = uri.Segments.Where(s => s != "/").Select(x => x.TrimEnd('/')).ToArray(), Parameters = HttpUtility.ParseQueryString(uri.Query).ToDictionary() }); } return(_urlAddresses); }