Ejemplo n.º 1
0
        private static void UpdateLanguageAndCountryCounts(
            Dictionary <string, Tuple <UInt32, UInt32> > languageCounts,
            Dictionary <string, Tuple <UInt32, UInt32> > countryCounts,
            BookRead book)
        {
            if (!languageCounts.ContainsKey(book.OriginalLanguage))
            {
                languageCounts.Add(book.OriginalLanguage, new Tuple <UInt32, UInt32>(1, book.Pages));
            }
            else
            {
                var updatedCounts =
                    new Tuple <UInt32, UInt32>(
                        languageCounts[book.OriginalLanguage].Item1 + 1,
                        languageCounts[book.OriginalLanguage].Item2 + book.Pages);
                languageCounts[book.OriginalLanguage] = updatedCounts;
            }

            if (!countryCounts.ContainsKey(book.Nationality))
            {
                countryCounts.Add(book.Nationality, new Tuple <UInt32, UInt32>(1, book.Pages));
            }
            else
            {
                var updatedCounts =
                    new Tuple <UInt32, UInt32>(
                        countryCounts[book.Nationality].Item1 + 1,
                        countryCounts[book.Nationality].Item2 + book.Pages);
                countryCounts[book.Nationality] = updatedCounts;
            }
        }
Ejemplo n.º 2
0
        private void AddNewBookToDatabase(BookRead newBook)
        {
            _client        = new MongoClient(DatabaseConnectionString);
            _booksDatabase = _client.GetDatabase("books_read");

            IMongoCollection <BookRead> booksRead = _booksDatabase.GetCollection <BookRead>("books");

            booksRead.InsertOne(newBook);
        }
Ejemplo n.º 3
0
        public void ReadBooksFromFile(string filename)
        {
            using (var sr = new StreamReader(filename, Encoding.Default))
            {
                var csv = new CsvReader(sr);

                BooksRead.Clear();

                // Date,DD/MM/YYYY,Author,Title,Pages,Note,Nationality,Original Language,Book,Comic,Audio
                while (csv.Read())
                {
                    var stringFieldDate             = csv.GetField <string>(0);
                    var stringFieldDDMMYYYY         = csv.GetField <string>(1);
                    var stringFieldAuthor           = csv.GetField <string>(2);
                    var stringFieldTitle            = csv.GetField <string>(3);
                    var stringFieldPages            = csv.GetField <string>(4);
                    var stringFieldNote             = csv.GetField <string>(5);
                    var stringFieldNationality      = csv.GetField <string>(6);
                    var stringFieldOriginalLanguage = csv.GetField <string>(7);
                    var stringFieldBook             = csv.GetField <string>(8);
                    var stringFieldComic            = csv.GetField <string>(9);
                    var stringFieldAudio            = csv.GetField <string>(10);

                    DateTime dateForBook;
                    if (DateTime.TryParseExact(stringFieldDDMMYYYY, "d/M/yyyy",
                                               CultureInfo.InvariantCulture, DateTimeStyles.None, out dateForBook))
                    {
                        UInt16 pages = 0;
                        UInt16.TryParse(stringFieldPages, out pages);
                        BookRead book = new BookRead()
                        {
                            DateString       = stringFieldDate,
                            Date             = dateForBook,
                            Author           = stringFieldAuthor,
                            Title            = stringFieldTitle,
                            Pages            = pages,
                            Note             = stringFieldNote,
                            Nationality      = stringFieldNationality,
                            OriginalLanguage = stringFieldOriginalLanguage,
                            Audio            = stringFieldAudio,
                            Book             = stringFieldBook,
                            Comic            = stringFieldComic,
                        };

                        BooksRead.Add(book);
                    }
                }
            }
            UpdateCollections();
            Properties.Settings.Default.InputFile = filename;
            Properties.Settings.Default.Save();
            DataFromFile = true;
        }
Ejemplo n.º 4
0
        private void UpdateBookInDatabase(BookRead editBook)
        {
            _client        = new MongoClient(DatabaseConnectionString);
            _booksDatabase = _client.GetDatabase("books_read");

            IMongoCollection <BookRead> booksRead = _booksDatabase.GetCollection <BookRead>("books");

            var filterOnId = Builders <BookRead> .Filter.Eq(s => s.Id, editBook.Id);

            long totalCount = booksRead.Count(filterOnId);

            var result = booksRead.ReplaceOne(filterOnId, editBook);
        }
Ejemplo n.º 5
0
        public bool UpdateBook(BookRead editBook, out string errorMsg)
        {
            errorMsg = "";

            if (DataFromDb)
            {
                UpdateBookInDatabase(editBook);
            }

            UpdateCollections();

            return(true);
        }
Ejemplo n.º 6
0
        public bool AddNewBook(BookRead newBook, out string errorMsg)
        {
            errorMsg = "";

            // for the moment insist only that the date is after the last of the existing items
            if (BooksRead.Last().Date > newBook.Date)
            {
                errorMsg = "Date must be after last date : " + BooksRead.Last().DateString;
                return(false);
            }

            BooksRead.Add(newBook);

            if (DataFromDb)
            {
                AddNewBookToDatabase(newBook);
            }

            UpdateCollections();

            return(true);
        }
Ejemplo n.º 7
0
 public TalliedBook(BookRead book)
 {
     Book = book;
 }