Exemplo n.º 1
0
        public bool UpdatePost(ref PostBDO postBDO, ref string message)
        {
            message = "post updated successfully";
            var ret = true;

            using (var BEntities = new BlogEntities())
            {
                var  postId   = postBDO.PostId;
                Post postInDB = (from p in BEntities.Posts
                                 where p.PostId == postId
                                 select p).FirstOrDefault();

                //check post
                if (postInDB == null)
                {
                    throw new Exception("No post with ID " + postBDO.PostId + " found.");
                }

                //update post
                postInDB.PostTitle   = postBDO.PostTitle;
                postInDB.PostContent = postBDO.PostContent;
                postInDB.RowVersion  = postBDO.RowVersion;

                BEntities.Posts.Attach(postInDB);
                BEntities.Entry(postInDB).State = System.Data.Entity.EntityState.Modified;
                var num = BEntities.SaveChanges();

                postBDO.RowVersion = postInDB.RowVersion;

                if (num != 1)
                {
                    ret     = false;
                    message = "no post is updated";
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
        public bool CreatePost(ref PostBDO postBDO, ref string message)
        {
            message = "post created";
            var ret = true;

            using (var BEntities = new BlogEntities())
            {
                var maxId     = 0;
                var maxIdPost = BEntities.Posts.OrderByDescending(i => i.PostId).FirstOrDefault();
                if (maxIdPost != null)
                {
                    maxId = maxIdPost.PostId;
                }

                var postId = maxId + 1;

                //var postId = postBDO.PostId;

                Post postToDB = new Post
                {
                    PostId       = postBDO.PostId,
                    PostTitle    = postBDO.PostTitle,
                    PostContent  = postBDO.PostContent,
                    PostDateTime = DateTime.Now
                };
                BEntities.Posts.Add(postToDB);

                var num = BEntities.SaveChanges();

                if (num != 1)
                {
                    ret     = false;
                    message = "no post created";
                }
            }
            return(ret);
        }