Exemplo n.º 1
0
        public ContentResult AddPost(Post post)
        {
            string json;

            ModelState.Clear();

            if (TryValidateModel(post)){
                int id = _blogRepository.AddPost(post);

                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id,
                                                           success = true,
                                                           message = "Post added successfully."
                                                       });
            }
            else{
                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id = 0,
                                                           success = false,
                                                           message = "Failed to add the post."
                                                       });
            }

            return Content(json, "application/json");
        }
Exemplo n.º 2
0
 public int AddPost(Post post)
 {
     if (_context.Entry(post).State == EntityState.Detached)
     {
         _context.Posts.Attach(post);
     }
     _context.Posts.Add(post);
     _context.SaveChanges();
     return post.Id;
 }
Exemplo n.º 3
0
 public static MvcHtmlString PostLink(this HtmlHelper helper, Post post)
 {
     return helper.ActionLink(
         post.Title,
         "Post",
         "Blog",
         new {year = post.PostedOn.Year, month = post.PostedOn.Month, title = post.UrlSlug},
         new
             {
                 title = String.Format("Read full article: " + post.Title),
                 @class = "postTitle"
             });
 }
Exemplo n.º 4
0
        public void EditPost(Post post)
        {
            DbEntityEntry<Post> entry = _context.Entry(post);
            if (entry.State == EntityState.Detached)
            {
                DbSet set = _context.Set(post.GetType());

                var attachedEntity = (Post) set.Find(post.Id);
                if (attachedEntity != null)
                {
                    DbEntityEntry<Post> attachedEntry = _context.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(post);
                }
            }
            else
            {
                entry.State = EntityState.Modified;
            }
            _context.SaveChanges();
        }
Exemplo n.º 5
0
        public ContentResult EditPost(Post post)
        {
            string json;

            ModelState.Clear();

            if (TryValidateModel(post)){
                _blogRepository.EditPost(post);
                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id = post.Id,
                                                           success = true,
                                                           message = "Changes saved successfully."
                                                       });
            }
            else{
                json = JsonConvert.SerializeObject(new
                                                       {
                                                           id = 0,
                                                           success = false,
                                                           message = "Failed to save the changes."
                                                       });
            }

            return Content(json, "application/json");
        }