コード例 #1
0
        /// <summary>
        /// Returns a collection of Books that match the specified search criteria, and
        /// sorted according to the current sort sequence.
        /// </summary>
        /// <param name="criteria">The criteria use to match the bolks.</param>
        /// <returns>Sorted collection of matching Books.</returns>
        public static IEnumerable <BookViewModel> GetMatchingBooks(SearchViewModel criteria)
        {
            //  Return empty list if search criteria not specified.
            if (criteria == null)
            {
                return(new List <BookViewModel>());
            }

            //  Clear out the existing books.
            _libraryDataSource.AllBooks.Clear();

            //  Map the search criteria.
            //  Instantiate the mapper class.
            var mapper = new MapSearchViewModelToSearchCriteria <SearchViewModel>(_libraryDataSource._ioc.Resolve <SearchCriteriaFactory>());
            //  Map the SearchViewModel to the SearchCriteria.
            var searchCriteria = mapper.Map(criteria);

            //  Call the repository, SearchBooks()
            var allBooks = _libraryDataSource._libraryRepository.SearchBooks(searchCriteria);

            foreach (var book in allBooks)
            {
                _libraryDataSource.AllBooks.Add(MapLibraryBookToBookViewModel.Map(book));
            }

            //  Call the GetBooks method as this will invoke the current sort for the results.
            return(GetBooks());
        }
コード例 #2
0
        /// <summary>
        /// Gets a collection of books, containing all books in the library, sorted according
        /// to the current sort sequence.
        /// </summary>
        /// <returns>A sorted collection of books</returns>
        public static IEnumerable <BookViewModel> ResetAllBooks()
        {
            //  Clear out any existing books.
            _libraryDataSource.AllBooks.Clear();
            //  Get all books from the data model.
            var allBooks = _libraryDataSource._libraryRepository.GetAllBooks();

            foreach (var book in allBooks)
            {
                _libraryDataSource.AllBooks.Add(MapLibraryBookToBookViewModel.Map(book));
            }

            //  Call the GetBooks method as this will invoke the current sort for the results.
            return(GetBooks());
        }
コード例 #3
0
        /// <summary>
        /// Method <c>LoadData</c> is called to load the view model with the data
        /// from the underlying Data Model, via the repositories, which access
        /// the underlying Data Model.
        /// </summary>
        private void LoadData()
        {
            //  Load _allBooks      (LibraryRepository)
            var allBooks = _libraryRepository.GetAllBooks();

            //  Process each indiviually to ensure the UI observable stuff is updated.
            foreach (var book in allBooks)
            {
                this.AllBooks.Add(MapLibraryBookToBookViewModel.Map(book));
            }

            //  Load _allSearches   (SearchRepository)
            var allSearches = _searchRepository.GetSearches();

            foreach (var search in allSearches)
            {
                this.AllSearches.Add(MapSearchCriteriaToSearchViewModel.Map(search));
            }

            var searchTypes = _searchRepository.GetSearchTypes();

            foreach (var searchType in searchTypes)
            {
                //  Create a SearchTypeViewModel
                SearchTypesViewModel typeVM = new SearchTypesViewModel()
                {
                    Type   = searchType,
                    Values = new ObservableCollection <string>()
                };

                //  Add a "Select" message as the first value for the search Type.
                //  TODO: Correct the Grammer for this function, select An Author, Select a Keyword.
                if (searchType == "SearchString")
                {
                    typeVM.Values.Add(string.Format("Type a value into the search string"));
                }
                else
                {
                    typeVM.Values.Add(string.Format("Select a {0}....", searchType));
                }

                //  Get the possible values from the library.
                var values = _libraryRepository.GetSearchableBookValues(searchType);
                //  Add each possible value into the SearchTypesViewModel
                foreach (var val in values)
                {
                    typeVM.Values.Add(val);
                }

                //  Add the SearchType to the view model
                this.CurrentSearch.SearchTypes.Add(typeVM);
                this.SearchTypes.Add(typeVM);
            }

            //  Load _currentSearch (item 0 of the searches loaded, if there are any)
            if (this.AllSearches.Count() == 0)
            {
                //  set to new search
                this.CurrentSearch.UniqueId               = string.Empty;
                this.CurrentSearch.Type                   = _searchTypes[0].Type;
                this.CurrentSearch.SearchString           = string.Empty;
                this.CurrentSearch.SearchDate             = DateTime.Now.ToString();
                this.CurrentSearch.SelectedTypeIndex      = 0;
                this.CurrentSearch.SelectedTypeValueIndex = 0;
            }
            else
            {
                //  Set to the first search.
                this.CurrentSearch.UniqueId     = this.AllSearches[0].UniqueId;
                this.CurrentSearch.Type         = this.AllSearches[0].Type;
                this.CurrentSearch.SearchString = this.AllSearches[0].SearchString;
                this.CurrentSearch.SearchDate   = this.AllSearches[0].SearchDate;
                int idx = this.SearchTypes.IndexOf(new SearchTypesViewModel()
                {
                    Type = this.CurrentSearch.Type
                });
                this.CurrentSearch.SelectedTypeIndex      = idx;
                this.CurrentSearch.SelectedTypeValueIndex = 0;
            }

            //  Set the default sort sequence for display, which is by Title
            this._currentSortSequence = BookSortEnum.Title;
        }