public ActionResult Index()
        {
            var viewModel = new IndexViewModel();

            var post = new Post();
            post.Title = "titulo";
            post.Text = "texto";
            post.PublishedAt = DateTime.Now.AddDays(-3);

            viewModel.PostsPublicados.Add(post);

            return View(viewModel);
        }
 private void CreateRandomPost(Int32 i)
 {
     var rnd = new Random(i * (int)DateTime.Now.Ticks);
     var post = new Post
     {
         CreatedAt = DateTime.Now.AddSeconds(-new Random().Next()),
         IsPublished = rnd.NextDouble() < 0.5,
         LastUpdatedAt = DateTime.Now.AddMilliseconds(-rnd.Next()),
         PublishedAt = DateTime.Now.AddMilliseconds(-rnd.Next()),
         Text = "Texto do post aleatório " + Guid.NewGuid().ToString(),
         Title = "Post aleatório " + Guid.NewGuid().ToString(),
         WebLog = webLogs[rnd.Next(5)]
     };
     post.WebLog.Posts.Add(post);
 }
        public void Initialize()
        {
            var context = new EntityFrameworkRepositoryContext(new BlogContext());
            WebLogs = new GenericRepository<WebLog>(context);

            var title = Guid.NewGuid().ToString();

            var user = new User { Name = "user " + title };
            var webLog = new WebLog { Title = webLogID, CreatedAt = DateTime.Now };
            var post = new Post { Title = postID, Text = "oi", CreatedAt = DateTime.Now, LastUpdatedAt = DateTime.Now, PublishedAt = DateTime.Now };

            webLog.Collaborators.Add(user);
            user.AddBlog(webLog);
            webLog.Posts.Add(post);
            post.WebLog = webLog;

            WebLogs.SaveOrUpdate(webLog);
        }
		public void SelectWithLambdaExpressions()
		{
            var title = Guid.NewGuid().ToString();
            var user = new User { Name = "user " + title };
            var webLog = new WebLog { Title = "weblog " + title, CreatedAt = DateTime.Now };
            var post = new Post { Title = title, Text = "oi", CreatedAt = DateTime.Now, LastUpdatedAt = DateTime.Now, PublishedAt = DateTime.Now };

            webLog.Collaborators.Add(user);
            user.AddBlog(webLog);
            webLog.Posts.Add(post);
            post.WebLog = webLog;

            webLogRepository.SaveOrUpdate(webLog);
            webLogRepository.SaveChanges();

            var posts = (from w in webLogRepository.Query()
                            from p in w.Posts
                            where p.Title == title
                            select p).ToList(); 
            Assert.AreEqual(1, posts.Count);
		}
		public ActionResult Create(String action, CreateViewModel viewModel)
		{
			if (!ModelState.IsValid) return View(viewModel);

			PostState postState;
			Post post;

			if (viewModel.PostID == 0)
			{
				postState = PostState.New;
				post = new Post();
				post.WebLog.ID = 1;
			}
			else
			{
				post = postRepository.Where(p => p.ID == viewModel.PostID).SingleOrDefault();

				if (post.IsPublished)
				{
					postState = PostState.Published;
				}
				else
				{
					postState = PostState.Draft;
				}
			}

			post.Text = viewModel.PostText;
			post.Title = viewModel.PostTitle;

			post.LastUpdatedAt = DateTime.Now;

			if (post.ID == 0) // insert
			{
				post.CreatedAt = DateTime.Now;
			}

			// action = ("Publish", "Save")
			if (action == "Publish")
			{
				post.IsPublished = true;
				post.LastUpdatedAt = DateTime.Now;

				if (post.ID == 0) // insert
				{
					post.CreatedAt = DateTime.Now;
				}
				postRepository.SaveOrUpdate(post);
				postRepository.SaveChanges();

				// redirect to confirmation
				ViewData["ID"] = post.ID;
				return View("PublishConfirm");
			}
			else // Save
			{
				post.IsPublished = false;
				postRepository.SaveOrUpdate(post);
				postRepository.SaveChanges();

				if (postState == PostState.Published)
				{
					// RedirectToPostList();
					return RedirectToAction("Index");
				}
				else
				{
					// ShowForm();

					// se criou o post, exibe mensagem de sucesso
					viewModel.BlogCreated = true;

					// passo o ID pra view no caso de nova inserção
					viewModel.PostID = post.ID;

					// exibe o form novamente
					return View(viewModel);
				}
			}			
		}
		public void InsertPostWithNewTagsAndComments()
		{
            var title = Guid.NewGuid().ToString();

            var webLog = new WebLog { Collaborators = new[] { new User { Name = "user " + title } }, Title = "weblog " + title, CreatedAt = DateTime.Now };            
            var post = new Post
            {
                Title = title,
                Text = "oi",
                CreatedAt = DateTime.Now,
                LastUpdatedAt = DateTime.Now,
                PublishedAt = DateTime.Now,
                Comments = new [] {
                    new Comment { ApprovedAt = DateTime.Now, Text="comment1", Title="oie", AuthorEmail="oxe", AuthorName="name", AuthorUrl="url", CreatedAt=DateTime.Now },
                    new Comment { ApprovedAt = DateTime.Now, Text="comment2", Title="oie", AuthorEmail="oxe", AuthorName="name", AuthorUrl="url", CreatedAt=DateTime.Now }
                },
                Tags = new [] {
                    new Tag { CreatedAt = DateTime.Now, Text = "tag1" },
                    new Tag { CreatedAt = DateTime.Now, Text = "tag2" },
                }
            };

            webLog.Posts.Add(post);
            post.WebLog = webLog;
            foreach (var c in post.Comments) c.Post = post;

            webLogRepository.SaveOrUpdate(webLog);
            webLogRepository.SaveChanges();

            var posts = (from w in webLogRepository.Query()
                         from p in w.Posts
                         where p.Title == title
                         select p).ToList(); 

            Assert.AreEqual(1, posts.Count);
		}
        public void InsertPostWithNullableProperty()
        {
            var title = Guid.NewGuid().ToString();

            var webLog = new WebLog { Collaborators = new[] { new User { Name = "user " + title } }, Title = "weblog " + title, CreatedAt = DateTime.Now };
            var post = new Post
            {
                Title = title,
                Text = "Post sem data de publicação",
                CreatedAt = DateTime.Now,
                LastUpdatedAt = DateTime.Now
            };

            webLog.Posts.Add(post);
            post.WebLog = webLog;

            webLogRepository.SaveOrUpdate(webLog);
            webLogRepository.SaveChanges();

            var posts = (from w in webLogRepository.Query()
                         from p in w.Posts
                         where p.Title == title
                         select p).ToList();

            Assert.AreEqual(1, posts.Count);
        }
        public void InsertPostWithExistingTagsAndComments()
        {
            var title = Guid.NewGuid().ToString();

            var tags = tagRepository.Query().Where(t => t.Text.Contains("tag1")).ToList();

            var webLog = new WebLog { Collaborators = new[] { new User { Name = "user " + title } }, Title = "weblog " + title, CreatedAt = DateTime.Now };
            var post = new Post
            {
                Title = title,
                Text = "Post com tags existentes",
                CreatedAt = DateTime.Now,
                LastUpdatedAt = DateTime.Now,
                PublishedAt = DateTime.Now,
                Tags = tags
            };

            webLog.Posts.Add(post);
            post.WebLog = webLog;

            webLogRepository.SaveOrUpdate(webLog);
            webLogRepository.SaveChanges();

            var posts = (from w in webLogRepository.Query()
                         from p in w.Posts
                         where p.Title == title
                         select p).ToList();

            Assert.AreEqual(1, posts.Count);
        }
        public DetailsViewModel()
        {
            this.Post = new Post();

        }