public void Search_returns_only_distinct_results()
        {
            var store = new TestableStore();

            var duplicatedTag = "alpha";

            var testTags = new[] { duplicatedTag, duplicatedTag, "beta" }.ToList();

            using (var tagsController = new TagsController(store))
            {
                using (var session = store.OpenSession())
                {
                    var entry = new Entry { Tags = new Collection<string>(testTags) };
                    session.Store(entry);
                    session.SaveChanges();

                    RavenControllerTestHelper.SetSessionOnController(tagsController, session);

                    var actionResult = tagsController.Search("alpha");
                    var tags = GetTagsFromResult(actionResult);

                    Assert.Equal(1, tags.Count());
                    Assert.Equal(duplicatedTag, tags.First());
                }
            }
        }
        public ActionResult Save(EntryEditModel editModel)
        {
            if(!this.ModelState.IsValid)
            {
                return this.Json(new { error = true });
            }

            var isNewEntry = IsNewEntry(editModel);

            Entry entry;

            if (isNewEntry)
            {
                entry = new Entry();
            }
            else
            {
                entry = this.session.Load<Entry>(editModel.Id);
                if (entry == null)
                {
                    return this.Json(new { error = true });
                }
            }

            this.ApplyEntryEditModelToEntry(entry, editModel);

            if (isNewEntry)
            {
                this.session.Store(entry);
            }

            this.session.SaveChanges();

            return this.Json(entry);
        }
        public void Delete_sets_entry_deleted()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var store = new TestableStore();

            var entry = new Entry
            {
                Id = "entries/123",
                Title = "foo"
            };

            using (var entriesController = new EntriesController(store))
            {
                using (var session = store.OpenSession())
                {
                    session.Store(entry);
                    session.SaveChanges();

                    RavenControllerTestHelper.SetSessionOnController(entriesController, session);

                    entriesController.Delete(entry.Id);

                    var entryLoaded = session.Load<Entry>(entry.Id);
                    Assert.True(entryLoaded.Deleted);
                }
            }

            Mapper.Reset();
        }
        public void Existing_database_entry_slug_remains_unchanged()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var testableStore = new TestableStore();

            var entry = new Entry
            {
                Slug = "foo-title"
            };

            using (var session = testableStore.OpenSession())
            {
                session.Store(entry);
                session.SaveChanges();

                var entryEditModel = new EntryEditModel
                {
                    Id = entry.Id,
                    Title = "Bar Title"
                };

                var entryEditModelToEntryMapper = new EntryEditModelToEntryMapper(session);
                entryEditModelToEntryMapper.Map(entry, entryEditModel);

                Assert.Equal(entry.Slug, entry.Slug);
            }

            Mapper.Reset();
        }
        public void Edit_pulls_entry_from_database_and_creates_viewmodel()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var store = new TestableStore();

            var entry = new Entry
            {
                Id = "entries/123",
                Title = "foo"
            };

            using (var entriesController = new EntriesController(store))
            {
                using (var session = store.OpenSession())
                {
                    session.Store(entry);
                    session.SaveChanges();

                    RavenControllerTestHelper.SetSessionOnController(entriesController, session);

                    string uniquePart = entry.Id.Replace(Entry.IdPrefix, "");

                    var actionResult = entriesController.Edit(uniquePart);
                    var entryEditModel = ControllerTestHelper.GetModelInActionResult<EntryEditModel>(actionResult);

                    Assert.Equal(entry.Title, entryEditModel.Title);
                }
            }

            Mapper.Reset();
        }
 private static void SetSlug(Entry entry)
 {
     if (string.IsNullOrEmpty(entry.Slug))
     {
         var slugMaker = new SlugMaker();
         entry.Slug = slugMaker.CreateSlug(entry.Title);
     }
 }
        public ActionResult New()
        {
            var entry = new Entry();

            var entryEditModel = this.CreateEntryEditModel(entry);

            return this.View("Edit", entryEditModel);
        }
 private void MapBasicProperties(Entry entry, EntryEditModel editModel)
 {
     entry.Body = editModel.Body;
     entry.Deleted = editModel.Deleted;
     entry.PageId = editModel.PageId;
     entry.Published = editModel.Published;
     entry.Tags = new Collection<string>(editModel.Tags);
     entry.Title = editModel.Title;
 }
        public EntryEditModel Map(Entry entry)
        {
            var entryEditModel = Mapper.Map<EntryEditModel>(entry);

            var markdown = new Markdown();

            entryEditModel.Preview = markdown.Transform(entry.Body);

            this.PopulatePageValues(entryEditModel);
            this.PopulatePossiblePages(entryEditModel);

            return entryEditModel;
        }
        private Entry ConvertPostToEntry(Post post)
        {
            var entry = new Entry();

            entry.PageId = this.GetPageIdFromPost(post);
            entry.Body = post.Body;
            entry.PublishedAtUtc = post.PublishedAtUtc;
            entry.LastModifiedAtUtc = DateTimeOffset.UtcNow;
            entry.Slug = post.Slug;
            entry.Title = post.Title;
            entry.Tags = new Collection<string>(post.Tags.Select(t => t.Slug).ToList());

            return entry;
        }
        public EntryViewModel Map(Entry entry)
        {
            var entryViewModel = Mapper.Map<EntryViewModel>(entry);

            var pageViewModel = this.GetPageViewModel(entryViewModel.PageId);

            PopulatePageDetails(entryViewModel, pageViewModel);
            PopulateBody(entryViewModel);

            this.PopulateCanonicalUrl(entryViewModel);

            PopulateDisqusThread(entryViewModel, pageViewModel);

            return entryViewModel;
        }
        public void Entry_deleted_flag_is_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var entry = new Entry
            {
                Deleted = true
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(true, entryViewModel.Deleted);
            }

            Mapper.Reset();
        }
        public void Non_published_entry_in_database_is_not_returned_by_its_slug()
        {
            var testableStore = new TestableStore();

            string slug = "foo-slug";

            var testEntry = new Entry
            {
                Slug = slug,
                Published = false
            };

            using (var session = testableStore.OpenSession())
            {
                session.Store(testEntry);
                session.SaveChanges();

                var entryReturned = GetEntryUsingFactory(session, slug);

                Assert.Null(entryReturned);
            }
        }
        public void Entry_LastModifiedAtUtc_is_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var lastModifiedAtUtc = DateTimeOffset.UtcNow;

            var entry = new Entry
            {
                LastModifiedAtUtc = lastModifiedAtUtc
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(lastModifiedAtUtc, entryViewModel.LastModifiedAtUtc);
            }

            Mapper.Reset();
        }
        public void Entry_id_is_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            string id = "entries/123";

            var entry = new Entry
            {
                Id = id
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(id, entryViewModel.Id);
            }

            Mapper.Reset();
        }
        public void Last_modified_date_updates()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var testableStore = new TestableStore();

            var entryEditModel = new EntryEditModel
            {
                LastModifiedAtUtc = DateTimeOffset.MinValue
            };

            using (var session = testableStore.OpenSession())
            {
                var entry = new Entry();

                var entryEditModelToEntryMapper = new EntryEditModelToEntryMapper(session);
                entryEditModelToEntryMapper.Map(entry, entryEditModel);

                Assert.NotEqual(entryEditModel.LastModifiedAtUtc, entry.LastModifiedAtUtc);
            }

            Mapper.Reset();
        }
        public void New_database_entry_has_slug_set_based_on_title()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var testableStore = new TestableStore();

            var entryEditModel = new EntryEditModel();
            entryEditModel.Title = "Foo Title";

            string expectedSlug = "foo-title";

            using (var session = testableStore.OpenSession())
            {
                var entry = new Entry();

                var entryEditModelToEntryMapper = new EntryEditModelToEntryMapper(session);
                entryEditModelToEntryMapper.Map(entry, entryEditModel);

                Assert.Equal(expectedSlug, entry.Slug);
            }

            Mapper.Reset();
        }
        public void Entry_slug_are_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            string slug = "foo-bar-baz";

            var entry = new Entry
            {
                Slug = slug
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(slug, entryViewModel.Slug);
            }

            Mapper.Reset();
        }
        public void Published_date_maps()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var testableStore = new TestableStore();

            var pubDate = DateTimeOffset.MinValue;

            using (var session = testableStore.OpenSession())
            {
                var entryEditModel = new EntryEditModel
                {
                    PublishedAtUtc = pubDate
                };

                var entry = new Entry();

                var entryEditModelToEntryMapper = new EntryEditModelToEntryMapper(session);
                entryEditModelToEntryMapper.Map(entry, entryEditModel);

                Assert.Equal(pubDate, entry.PublishedAtUtc);
            }

            Mapper.Reset();
        }
        public void Entry_tags_are_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var tags = new Collection<string>(new[] { "iphone", "android" });

            var entry = new Entry
            {
                Tags = tags
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(tags, entryViewModel.Tags);
            }

            Mapper.Reset();
        }
        public void Entry_title_is_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            string title = "foo";

            var entry = new Entry
            {
                Title = title
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(title, entryViewModel.Title);
            }

            Mapper.Reset();
        }
 public void Map(Entry entry, EntryEditModel entryEditModel)
 {
     MapBasicProperties(entry, entryEditModel);
     SetSlug(entry);
     UpdateTimestamps(entry, entryEditModel);
 }
        public void Published_entry_in_database_is_successfully_returned_by_its_slug()
        {
            var testableStore = new TestableStore();

            string slug = "foo-slug";
            string title = "Foo Title";

            var testEntry = new Entry
            {
                Slug = slug,
                Title = title,
                Published = true
            };

            using (var session = testableStore.OpenSession())
            {
                session.Store(testEntry);
                session.SaveChanges();

                var entryReturned = GetEntryUsingFactory(session, slug);

                Assert.Equal(title, entryReturned.Title);
            }
        }
        public void Entry_references_are_correctly_mapped()
        {
            Mapper.Reset();
            AutoMapperConfig.RegisterMappings();

            var references = new Collection<string>(new[] { "images/123" });

            var entry = new Entry
            {
                References = references
            };

            var store = new TestableStore();

            using (var session = store.OpenSession())
            {
                var entryViewModel = GetMappedEntryViewModel(session, entry);

                Assert.Equal(references, entryViewModel.References);
            }

            Mapper.Reset();
        }
        private static EntryViewModel GetMappedEntryViewModel(IDocumentSession session, Entry entry)
        {
            var testableUrlHelperFactory = new TestableUrlHelperFactory();
            var urlHelper = testableUrlHelperFactory.Create();

            var entryToEntryViewModelMapper = new EntryToEntryViewModelMapper(session, urlHelper);
            return entryToEntryViewModelMapper.Map(entry);
        }
 private EntryEditModel CreateEntryEditModel(Entry entry)
 {
     var mapper = new EntryToEntryEditModelMapper(this.session);
     return mapper.Map(entry);
 }
 private static void UpdateTimestamps(Entry entry, EntryEditModel entryEditModel)
 {
     entry.LastModifiedAtUtc = DateTimeOffset.UtcNow;
     entry.PublishedAtUtc = entryEditModel.PublishedAtUtc;
 }
 private void ApplyEntryEditModelToEntry(Entry entry, EntryEditModel editModel)
 {
     var mapper = new EntryEditModelToEntryMapper(this.session);
     mapper.Map(entry, editModel);
 }