Example #1
0
        public IEnumerable <Categorys> GetPosts()
        {
            try
            {
                using (BlogEntities db = new BlogEntities())
                {
                    var oCategory = (from c in db.category
                                     select new Categorys()
                    {
                        Id = c.id,
                        Name = c.name
                    });

                    return(oCategory.ToList());
                }
            }
            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);
            }
        }
Example #4
0
        public void DeletePost(int id)
        {
            try
            {
                using (BlogEntities db = new BlogEntities())
                {
                    var oPost = db.post.Find(id);

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

                    oPost.is_active = false;

                    db.Entry(oPost).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }