Example #1
0
        public CreatePost GetPostForEdit(int id)
        {
            CreatePost model = new CreatePost();

            try
            {
                using (BlogEntities db = new BlogEntities())
                {
                    var oPost = db.post.Find(id);

                    if (oPost == null)
                    {
                        throw new Exception();
                    }

                    var oCat = db.category.Find(oPost.id_category);
                    model.Title        = oPost.title;
                    model.Content      = oPost.post_content;
                    model.Image        = oPost.image;
                    model.Category     = oCat;
                    model.CreationDate = oPost.creation_date;
                }

                return(model);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Example #2
0
        public void EditPost(CreatePost model)
        {
            try
            {
                using (BlogEntities db = new BlogEntities())
                {
                    var oPost = db.post.Find(model.Id);
                    oPost.title        = model.Title;
                    oPost.post_content = model.Content;
                    oPost.image        = model.Image;
                    oPost.id_category  = model.Category.id;

                    db.Entry(oPost).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Example #3
0
        public void CreatePost(CreatePost model)
        {
            try
            {
                using (BlogEntities db = new BlogEntities())
                {
                    var oPost = new post();
                    oPost.title         = model.Title;
                    oPost.post_content  = model.Content;
                    oPost.image         = model.Image;
                    oPost.id_category   = model.Category.id;
                    oPost.creation_date = model.CreationDate;
                    oPost.is_active     = true;

                    db.post.Add(oPost);
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }