public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // Insert the post into the posts collection
            var user = Request.GetOwinContext().Authentication.User;
            var post = new Post
            {
                Author = user.Identity.Name,
                CreatedAtUtc = DateTime.Now.ToUniversalTime(),
                Title = model.Title,
                Content = model.Content
            };
            foreach (var tag in model.Tags.Split(','))
            {
                post.Tags.Add(tag);
            }

            await blogContext.Posts.InsertOneAsync(post);

            return RedirectToAction("Post", new { id = post.Id });

            
        }
Example #2
0
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // Insert the post into the posts collection

            var post = new Post
            {
                Author = User.Identity.Name,
                Content = model.Content,
                Title = model.Title,
                CreatedAtUtc = DateTime.Now
            };
            if (string.IsNullOrWhiteSpace(model.Tags) == false)
            {
                post.Tags.AddRange(model.Tags.Split(',').Select(x=>x.Trim()));
            }

            await blogContext.Posts.InsertOneAsync(post);

            return RedirectToAction("Post", new {id = post.Id});
        }
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // Insert the post into the posts collection

            var list = new List<String>();
            
            model.Tags.Split(',').ToList().ForEach(data => list.Add(data.Trim()));

            var post = new Post {
                Author = this.User.Identity.Name
                ,Comments = new List<Comment>()
                ,Content = model.Content
                ,CreatedAtUtc = DateTime.Now
                ,Title = model.Title
                ,Tags = list
            };

            await blogContext.Posts.InsertOneAsync(post);

            return RedirectToAction("Post", new { id = post.Id });
        }
        public async Task<ActionResult> Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // fetch a user by the email in model.Email

            User user = await blogContext.Users.Find(Builders<User>.Filter.Eq(x => x.Email, model.Email)).SingleOrDefaultAsync();

            if (user == null)
            {
                ModelState.AddModelError("Email", "Email address has not been registered.");
                return View(model);
            }

            var identity = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, user.Name),
                    new Claim(ClaimTypes.Email, user.Email)
                },
                "ApplicationCookie");

            var context = Request.GetOwinContext();
            var authManager = context.Authentication;

            authManager.SignIn(identity);

            return Redirect(GetRedirectUrl(model.ReturnUrl));
        }
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            
            // XXX WORK HERE
            // Insert the post into the posts collection

            /* Response */
            Post post = new Post
            {
                Tags = model.Tags.Split(',').ToList(),
                Title = model.Title,
                Content = model.Content,
                Author = User.Identity.Name,
                CreatedAtUtc = DateTime.UtcNow,
                Comments = new List<Comment>()
            };

            await blogContext.Posts.InsertOneAsync(post);
            /* ---- */

            return RedirectToAction("Post", new { id = post.Id });
        }
        public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // create a new user and insert it into the database
            var builder = Builders<User>.Filter;
            var filter = builder.Eq("Email", model.Email);
            await blogContext.Users.InsertOneAsync(new User { Name = model.Name, Email = model.Email });

            return RedirectToAction("Index", "Home");
        }
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            // XXX WORK HERE
            // find the most recent 10 posts and order them
            // from newest to oldest
            var recentPosts = blogContext.Posts.Find(m => true).SortBy(m => m.CreatedAtUtc).ToListAsync().Result.Take(10).ToList();

            var model = new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            var recentPosts = await blogContext.Posts.Find(x => true)
                .SortByDescending(x => x.CreatedAtUtc)
                .Limit(10)
                .ToListAsync();

            var model = new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            // XXX WORK HERE
            // find the most recent 10 posts and order them
            // from newest to oldest
            var sort = Builders<Post>.Sort.Descending(p => p.CreatedAtUtc);
            var recentPosts = await blogContext.Posts.Find(new BsonDocument()).Sort(sort).Limit(10).ToListAsync();

           var model=new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
Example #10
0
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            // Find the most recent 10 posts and order them from newest to oldest.
            var recentPosts = await blogContext.Posts.Find<Post>(Builders<Post>.Filter.Empty)
                .SortByDescending(d => d.CreatedAtUtc)
                .Limit(10)
                .ToListAsync();

            var model = new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
        public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            var user = new User
            {
                Name = model.Name,
                Email = model.Email
            };

            await blogContext.Users.InsertOneAsync(user);
            return RedirectToAction("Index", "Home");
        }
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            // XXX WORK HERE
            // find the most recent 10 posts and order them
            // from newest to oldest

            var recentPosts = await blogContext.Posts.Find(new BsonDocument()).ToListAsync();

            recentPosts = recentPosts.OrderByDescending(post => post.CreatedAtUtc).Take(10).ToList();
            
            var model = new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
        public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // create a new user and insert it into the database
            var user = new User
            {
                Email = model.Email,
                Name = model.Name
            };
            await blogContext.Users.InsertOneAsync(user);

            return RedirectToAction("Index", "Home");
        }
Example #14
0
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();
            // Find the post with the given identifier
            var post = await blogContext.Posts.Find<Post>(p => p.Id == ObjectId.Parse(id)).SingleOrDefaultAsync();

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #15
0
        public async Task<ActionResult> Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // fetch a user by the email in model.Email

            string email = model.Email;
            var builder = Builders<User>.Filter;
            FilterDefinition<User> searchFilter = builder.Eq("Email", email);
            IMongoCollection<User> usersCollection =  getCollection();

            User user = null;
            var list = await usersCollection.Find(searchFilter).Limit(1).ToListAsync();
            foreach (User doc in list)
            {
                Console.WriteLine("Found User:"******"Email", "Email address has not been registered.");
                return View(model);
            }

            var identity = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, user.Name),
                    new Claim(ClaimTypes.Email, user.Email)
                },
                "ApplicationCookie");

            var context = Request.GetOwinContext();
            var authManager = context.Authentication;

            authManager.SignIn(identity);

            return Redirect(GetRedirectUrl(model.ReturnUrl));
        }
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Find the post with the given identifier
            var post = blogContext.Posts.Find(m => m.Id == id).FirstOrDefaultAsync().Result;
            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #17
0
        public async Task<ActionResult> Posts(string tag = null)
        {
            var blogContext = new BlogContext();
            // Find all the posts with the given tag if it exists.
            // Otherwise, return all the posts.
            // Each of these results should be in descending order.
            var posts = await blogContext.Posts.Find<Post>(p => p.Tags.Contains(tag))
                .SortByDescending(p => p.CreatedAtUtc)
                .ToListAsync();

            if (posts.Count == 0)
            {
                posts = await blogContext.Posts.Find<Post>(Builders<Post>.Filter.Empty)
                    .SortByDescending(p => p.CreatedAtUtc)
                    .ToListAsync();
            }

            return View(posts);
        }
Example #18
0
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Find the post with the given identifier
            var post = await blogContext.Posts.Find(Builders<Post>.Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync();

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #19
0
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            // XXX WORK HERE
            // find the most recent 10 posts and order them
            // from newest to oldest
            IMongoCollection<Post> postsCollection = getPostsCollection();

            List<Post> recentPosts = await postsCollection.Find(new BsonDocument())
                .Sort(Builders<Post>.Sort.Descending("CreatedAtUtc"))
                .Limit(10)
                .ToListAsync();

            var model = new IndexModel
            {
                RecentPosts = recentPosts
            };

            return View(model);
        }
        public async Task<ActionResult> Post(String id)
        {
            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Find the post with the given identifier

            var post = await blogContext.Posts.Find(new BsonDocument("_id", new ObjectId(id))).FirstOrDefaultAsync();

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #21
0
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            var post = new Post
            {
                Author = User.Identity.Name,
                Title = model.Title,
                Content = model.Content,
                Tags = model.Tags.Split(',').Select(p => p.Trim()).ToArray(),
                CreatedAtUtc = DateTime.UtcNow,
                Comments = new List<Comment>()
            };

            await blogContext.Posts.InsertOneAsync(post);

            return RedirectToAction("Post", new { id = post.Id });
        }
Example #22
0
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();

            var post = await blogContext.Posts.Find(x => x.Id == id).SingleOrDefaultAsync();

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post,
                NewComment = new NewCommentModel
                {
                    PostId = id
                }
            };

            return View(model);
        }
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // Insert the post into the posts collection
            var post = new Post{
                Author = this.User.Identity.Name,
                Title = model.Title,
                Content = model.Content,
                Tags = model.Tags.Split(',').Select(p => p.Trim()).ToList(),
                CreatedAtUtc = DateTime.UtcNow
            };

            await blogContext.Posts.InsertOneAsync(post);

            return RedirectToAction("Post", new { id = post.Id });
        }
Example #24
0
        public async Task<ActionResult> Index()
        {
            var blogContext = new BlogContext();
            var recentPosts = await blogContext.Posts.Find(x => true)
                .SortByDescending(x => x.CreatedAtUtc)
                .Limit(10)
                .ToListAsync();

            var tags = await blogContext.Posts.Aggregate()
                .Project(x => new { _id = x.Id, Tags = x.Tags })
                .Unwind(x => x.Tags)
                .Group<TagProjection>("{ _id: '$Tags', Count: { $sum: 1 } }")
                .ToListAsync();

            var model = new IndexModel
            {
                RecentPosts = recentPosts,
                Tags = tags
            };

            return View(model);
        }
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Find the post with the given identifier

            /* Response */
            Post post = await blogContext.Posts.Find(p => p.Id == ObjectId.Parse(id)).SingleAsync();
            /* ---- */

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #26
0
        public async Task<ActionResult> Post(string id)
        {
            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Find the post with the given identifier
            IMongoCollection<Post> postsCollection = getPostsCollection();
            var builder = Builders<Post>.Filter;
            FilterDefinition<Post> searchFilter = builder.Eq("_id", ObjectId.Parse(id));
            Post post = await postsCollection.Find(searchFilter).FirstOrDefaultAsync();

            if (post == null)
            {
                return RedirectToAction("Index");
            }

            var model = new PostModel
            {
                Post = post
            };

            return View(model);
        }
Example #27
0
        public async Task<ActionResult> NewPost(NewPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            // XXX WORK HERE
            // Insert the post into the posts collection
            Post post = new Post();
            post.Title = model.Title;
            post.Content = model.Content;
            post.Author = this.User.Identity.Name;
            //post.CreatedAtUtc = DateTime.Now;

            post.Tags= model.Tags.Split(',').ToList();

            IMongoCollection<Post> postsCollection = getPostsCollection();
            await postsCollection.InsertOneAsync(post);

            return RedirectToAction("Post", new { id = post.Id });
        }
Example #28
0
        public async Task<ActionResult> Posts(string tag = null)
        {
            var blogContext = new BlogContext();

            Expression<Func<Post, bool>> filter = x => true;

            if (tag != null)
            {
                filter = x => x.Tags.Contains(tag);
            }

            var posts = await blogContext.Posts.Find(filter)
                .SortByDescending(x => x.CreatedAtUtc)
				.Limit(10)
                .ToListAsync();

            return View(posts);
        }
Example #29
0
        public async Task<ActionResult> CommentLike(CommentLikeModel model)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Post", new { id = model.PostId });
            }

            var blogContext = new BlogContext();

            // XXX WORK HERE
            // Increment the Likes field for the comment at {model.Index}
            // inside the post {model.PostId}.
            //
            // NOTE: The 2.0.0 driver has a bug in the expression parser and 
            // might throw an exception depending on how you solve this problem. 
            // This is documented here along with a workaround:
            // https://jira.mongodb.org/browse/CSHARP-1246
            await blogContext.Posts.UpdateOneAsync(
                p => p.Id == model.PostId,
                Builders<Post>.Update.Inc(string.Format("Comments.{0}.Likes", model.Index), 1));

            return RedirectToAction("Post", new { id = model.PostId });
        }
Example #30
0
        public async Task<ActionResult> NewComment(NewCommentModel model)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Post", new { id = model.PostId });
            }

            var comment = new Comment
            {
                Author = User.Identity.Name,
                Content = model.Content,
                CreatedAtUtc = DateTime.UtcNow
            };

            var blogContext = new BlogContext();

            await blogContext.Posts.UpdateOneAsync(
                x => x.Id == model.PostId,
                Builders<Post>.Update.Push(x => x.Comments, comment));


            return RedirectToAction("Post", new { id = model.PostId });
        }