示例#1
0
        public void AllPages_Should_Add_To_Cache_When_Cache_Is_Empty(bool loadPageContent)
        {
            // Arrange
            string cacheKey = (loadPageContent) ? (CacheKeys.AllPagesWithContent()) : (CacheKeys.AllPages());

            SettingsRepositoryMock settingsRepository = new SettingsRepositoryMock();
            PageRepositoryMock     pageRepository     = new PageRepositoryMock();

            pageRepository.AddNewPage(new Page()
            {
                Title = "1"
            }, "text", "admin", DateTime.UtcNow);
            pageRepository.AddNewPage(new Page()
            {
                Title = "2"
            }, "text", "admin", DateTime.UtcNow);

            CacheMock   listCache   = new CacheMock();
            PageService pageService = CreatePageService(null, listCache, settingsRepository, pageRepository);

            // Act
            pageService.AllPages(loadPageContent);

            // Assert
            Assert.That(listCache.CacheItems.Count, Is.EqualTo(1));
            Assert.That(listCache.CacheItems.FirstOrDefault().Key, Is.EqualTo(cacheKey));
        }
        /// <summary>
        /// Retrieves a list of all pages in the system.
        /// </summary>
        /// <returns>An <see cref="IEnumerable{PageViewModel}"/> of the pages.</returns>
        /// <exception cref="DatabaseException">An databaseerror occurred while retrieving the list.</exception>
        public IEnumerable <PageViewModel> AllPages(bool loadPageContent = false)
        {
            try
            {
                string cacheKey = "";
                IEnumerable <PageViewModel> pageModels  = null;
                IEnumerable <PageViewModel> pageModels2 = null;

                if (loadPageContent)
                {
                    cacheKey = CacheKeys.AllPagesWithContent();
                    //   pageModels = _listCache.Get<PageViewModel>(cacheKey);

                    if (pageModels == null)
                    {
                        IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title);
                        pageModels = from page in pages
                                     select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter);

                        _listCache.Add <PageViewModel>(cacheKey, pageModels);
                    }
                }
                else
                {
                    cacheKey = CacheKeys.AllPages();
                    //  pageModels = _listCache.Get<PageViewModel>(cacheKey);

                    if (pageModels == null)
                    {
                        IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title);
                        pageModels = from page in pages
                                     select new PageViewModel()
                        {
                            Id = page.Id, Title = page.Title
                        };


                        pageModels2 = from page in pageModels
                                      select new PageViewModel()
                        {
                            Id = page.Id, Title = page.Title, Relationships = page.GetRelationships(), RelationshipsWithLoggedInUser = page.GetRelationshipWithUser()
                        };

                        _listCache.Add <PageViewModel>(cacheKey, pageModels2);
                    }
                }

                return(pageModels2);
            }
            catch (DatabaseException ex)
            {
                throw new DatabaseException(ex, "An error occurred while retrieving all pages from the database");
            }
        }
        public void AllPages_Should_Load_From_Cache(bool loadPageContent)
        {
            string cacheKey = (loadPageContent) ? (CacheKeys.AllPagesWithContent()) : (CacheKeys.AllPages());

            // Arrange
            RepositoryMock repository = new RepositoryMock();
            CacheMock      listCache  = new CacheMock();

            PageService   pageService   = CreatePageService(null, listCache, repository);
            PageViewModel expectedModel = CreatePageViewModel();

            AddListCacheItem(listCache, cacheKey, new List <PageViewModel>()
            {
                expectedModel
            });

            // Act
            IEnumerable <PageViewModel> actualList = pageService.AllPages(loadPageContent);

            // Assert
            Assert.That(actualList, Contains.Item(expectedModel));
        }