Пример #1
0
        public ActionResult Create(Create model)
        {
            if (string.IsNullOrWhiteSpace(model.Title))
                ModelState.AddModelError("Required", "You must enter a title for the post.");

            if (string.IsNullOrWhiteSpace(model.Body))
                ModelState.AddModelError("Required", "You must write some content for the post.");

            if (model.Availability == Availability.Scheduled && model.ScheduleFor.HasValue == false)
                ModelState.AddModelError("Required", "To schedule a post you must select a scheduled date and time.");

            if (ModelState.IsValid == false)
                return View("Create", model);

            var slug = Helper.ConvertToSlug(model.Title);
            var post = Session.Query<Post>().Where(x => x.Slug == slug).FirstOrDefault();

            if (post != null)
            {
                if (post.Slug == slug)
                    ModelState.AddModelError("Conflict", string.Format("A post with the title '{0}' already exists.", model.Title));
            }

            if (ModelState.IsValid == false)
                return View("Create", model);

            post = new Post(model.Title, slug, HttpContext.User.Identity.Name, model.Body)
                       {
                           Description = model.Description,
                           EnableComments = model.EnableComments == Status.Enabled,
                           Tags = Helper.ConvertToTagList(model.Tags)
                       };

            if (model.Availability == Availability.Publish)
                post.Publish();
            else if (model.Availability == Availability.Scheduled && model.ScheduleFor.HasValue)
                post.ScheduleFor(model.ScheduleFor.Value);
            else if (model.Availability == Availability.Draft)
                post.MarkAsDraft();

            Session.Store(post);
            Session.SaveChanges();

            if (post.EnableComments && post.IsPublished())
            {
                Session.Store(new PostComments(post.Id, post.Title, post.Slug, post.PublishedAt.Value));
                Session.SaveChanges();
            }

            return RedirectToAction("Edit", new { slug });
        }
Пример #2
0
        private void BuildSampleData(IDocumentStore store)
        {
            using (var session = store.OpenSession())
            {
                var settings = new Settings();
                if (session.Load<Settings>(settings.Id) == null)
                {
                    session.Store(settings);
                }

                if (session.Load<User>("admin") == null)
                {
                    session.Store(new User("admin", "password"));
                }

                if (session.Query<Post>().Count() == 0)
                {
                    var post1 = new Post("Sample Blog Post", "sample-blog-post", "admin",
                        "<p>Welcome to the Sura Blog system</p><p>This is an automatically generated sample post</p>");
                    post1.Publish();
                    post1.Tags.Add("sample");
                    session.Store(post1);

                    var post2 = new Post("Another sample post", "another-sample-post", "admin",
                        "<p>This is another sample blog post</p>");
                    post2.Publish();
                    post2.Tags.Add("sample");
                    post2.Tags.Add("amazing");
                    session.Store(post2);

                    var post3 = new Post("A scheduled sample post", "scheduled-sample-post", "admin",
                        "<p>This is a sample blog post scheduled to post on 1st January 2013.</p>");
                    post3.ScheduleFor(new DateTimeOffset(new DateTime(2013, 1, 1)));
                    post3.Tags.Add("scheduled");
                    session.Store(post3);

                    var post4 = new Post("A draft sample post", "draft-sample-post", "admin",
                        "<p>This sample post is marked as a draft copy.</p>");
                    post4.MarkAsDraft();
                    post4.Tags.Add("sample");
                    post4.Tags.Add("draft");
                    session.Store(post4);
                }

                session.SaveChanges();
            }
        }