public void CreatePost(Post post)
        {
            ConnectToDatabase();

            Document doc = new Document();
            doc["PostDate"] = post.PostDate;
            doc["Title"] = post.Title;
            doc["Body"] = post.Body;
            doc["CharCount"] = post.CharCount;
            doc["CommentCount"] = 0;

            collection.Save(doc);

            DisconnectFromDatabase();

            return;
        }
        public void CreatePost(Post post)
        {
            try
            {
                ConnectToDatabase();

                post.Id = Guid.NewGuid().ToString();
                collection.Insert(post);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                DisconnectFromDatabase();
            }

            return;
        }
        public ActionResult PostCreate(Post post)
        {
            if (ModelState.IsValid)
            {
                post.CharCount = post.Body.Length;
                post.PostDate = DateTime.UtcNow;

                //post.Comments = new List<Comment>
                //{
                //    { new Comment() { TimePosted = DateTime.Now, Name = "Bob McBob", Email = "*****@*****.**", Body = "This article is too short!" } },
                //    { new Comment() { TimePosted = DateTime.Now, Name = "Jane McJane", Email = "*****@*****.**", Body = "I agree with Bob." } }
                //};

                postRepository.CreatePost(post);

                return RedirectToAction("MongoBlog");
            }
            else
            {
                return View(post);
            }
        }
 // POST api/insertpost
 public int Post(Post value)
 {
     return AppService.Insert(value);
 }
        public Post GetPostById(string Id)
        {
            ConnectToDatabase();

            Document doc = collection.FindOne(new Document() { { "_id", new Oid(Id) } });

            IList<Comment> comments = new List<Comment>();

            //List<Document> comments  = (doc["Comments"] as Document[]).ToList();

            //var comments = (doc["Comments"] as Document[]);

            //int x = ((Document[])doc["Comments"]).Count();

            Post post = new Post { Id = doc["_id"].ToString().Replace("\"", ""), PostDate = Convert.ToDateTime(doc["PostDate"]), Title = doc["Title"].ToString(), Body = doc["Body"].ToString(), CharCount = Convert.ToInt32(doc["CharCount"]), CommentCount = Convert.ToInt32(doc["CommentCount"]) };
            post.Comments = comments;

            DisconnectFromDatabase();

            return post;
        }
        public void UpdatePost(Post post)
        {
            ConnectToDatabase();

            var qry = new Document { { "_id", new Oid(post.Id) } };

            Document doc = collection.FindOne(qry);

            doc["Title"] = post.Title;
            doc["Body"] = post.Body;
            doc["CharCount"] = post.CharCount;
            doc["CommentCount"] = 0;

            collection.Update(doc);

            DisconnectFromDatabase();

            return;
        }
        // posts
        public IEnumerable<Post> GetPosts()
        {
            ConnectToDatabase();

            IList<Post> posts = new List<Post>();

            using (var all = collection.FindAll())
            {
                foreach (var doc in all.Documents)
                {
                    Post p = new Post { Id = doc["_id"].ToString().Replace("\"", ""), PostDate = Convert.ToDateTime(doc["PostDate"]), Title = doc["Title"].ToString(), Body = doc["Body"].ToString(), CharCount = Convert.ToInt32(doc["CharCount"]), CommentCount = Convert.ToInt32(doc["CommentCount"]) };

                    posts.Add(p);
                }
            }

            DisconnectFromDatabase();

            return posts;
        }
 // POST api/deletepost
 public int Post(Post value)
 {
     return AppService.Delete(value);
 }
        public void UpdatePost(Post post)
        {
            try
            {
                ConnectToDatabase();

                collection.Update(new { _id = post.Id }, x => x.SetValue(p => p.Title, post.Title));
                collection.Update(new { _id = post.Id }, x => x.SetValue(p => p.Body, post.Body));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                DisconnectFromDatabase();
            }

            return;
        }
        public ActionResult PostEdit(Post post)
        {
            if (ModelState.IsValid)
            {
                post.CharCount = post.Body.Length;

                postRepository.UpdatePost(post);

                return RedirectToAction("MongoBlog");
            }
            else
            {
                return View(post);
            }
        }
 // POST api/updatepost
 public int Post(Post value)
 {
     return AppService.Update(value);
 }