Ejemplo n.º 1
0
        public ActionResult Create(Post post)
        {
            if (String.IsNullOrEmpty(post.Title) || String.IsNullOrEmpty(post.Text))
            {
                return View();
            }
            post.ID = Guid.NewGuid();
            post.Posted = DateTime.Now;
            post.Index = post.Title;
            var blogEntities = new BlogEntities();
            blogEntities.AddToPosts(post);
            blogEntities.SaveChanges();

            return RedirectToRouteResult(post.ID);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Post modifiedPost)
        {

            var blogEntities = new BlogEntities();
            var postID = modifiedPost.ID;
            var originalPost = GetPostByID(postID);
            if (!ModelState.IsValid)
            {
                return View(originalPost);
            }

            blogEntities.Attach(originalPost);
            blogEntities.ApplyCurrentValues(originalPost.EntityKey.EntitySetName, modifiedPost);
            blogEntities.SaveChanges();

            return RedirectToRouteResult(postID);
        }
Ejemplo n.º 3
0
        public ActionResult Index(int startFrom = 1)
        {
            using (var blogEntities = new BlogEntities())
            {
                List<Post> posts;

                var filteredPosts = blogEntities.Posts
                    .FilterDrafts();
                var count = filteredPosts.Count();
                var pageSize = 10;
                posts = filteredPosts
                    .OrderByDescending(post => post.Posted)
                    .Skip(startFrom - 1)
                    .Take(pageSize)
                    .ToList();
                var pager = new PagerModel {ShowFrom = startFrom, ItemCount = count, PageSize = pageSize};
                var blogModel = new BlogModel{Posts = posts, PagerModel = pager};
                return View(blogModel);
            }
        }
Ejemplo n.º 4
0
 private Post GetPostByID(Guid postID)
 {
     var blogEntities = new BlogEntities();
     var queryable = from post in blogEntities.Posts 
                     where post.ID == postID
                     select post;
     var result = queryable.Single();
     blogEntities.Detach(result);
     return result;
 }
Ejemplo n.º 5
0
 public ActionResult Delete(Guid postID)
 {
     var postByID = GetPostByID(postID);
     var blogEntities = new BlogEntities();
     blogEntities.Attach(postByID);
     blogEntities.DeleteObject(postByID);
     blogEntities.SaveChanges();
     return RedirectToAction("Index");
 }