Exemplo n.º 1
0
        private void ConnectToBooksDatabase()
        {
            _booksDatabase = _client.GetDatabase("books_read");

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

            // this is a dummy query to get everything to date...

            var filter = Builders <BookRead> .Filter.Lte(
                new StringFieldDefinition <BookRead, BsonDateTime>("date"), new BsonDateTime(DateTime.Now));

            long totalCount = booksRead.Count(filter);

            if (totalCount == 0 && BooksRead.Count != 0)
            {
                booksRead.InsertMany(BooksRead);
                totalCount = booksRead.Count(filter);
            }
            else if (totalCount != 0 && BooksRead.Count == 0)
            {
                BooksRead.Clear();

                using (var cursor = booksRead.FindSync(filter))
                {
                    var booksList = cursor.ToList();
                    foreach (var book in booksList)
                    {
                        BooksRead.Add(book);
                    }
                }
                UpdateCollections();
                DataFromFile = false;
                DataFromDb   = true;
            }
        }
        public void Setup(IList <BookRead> books, IGeographyProvider geographyProvider)
        {
            _geographyProvider = geographyProvider;

            BooksRead.Clear();
            foreach (BookRead book in books.OrderBy(x => x.Date))
            {
                BooksRead.Add(book);
            }

            UpdateBookDeltas();
            UpdateBookPerYearDeltas();
            UpdateAuthors();
            UpdateWorldCountryLookup();
            int  booksReadWorldwide;
            uint pagesReadWorldwide;

            UpdateCountries(out booksReadWorldwide, out pagesReadWorldwide);
            UpdateLanguages(booksReadWorldwide, pagesReadWorldwide);
            BookLocationDeltas = new ObservableCollection <BookLocationDelta>();
            UpdateBookLocationDeltas();
            UpdateBooksPerMonth();
            UpdateBookTags();
            UpdateTalliedBooks();
            SelectedMonthTally = TalliedMonths.FirstOrDefault();
            _selectedMonth     = DateTime.Now;
            if (SelectedMonthTally != null)
            {
                _selectedMonth = SelectedMonthTally.MonthDate;
            }
        }
Exemplo 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;
        }