Example #1
0
        public PostBDO GetPost(int id)
        {
            PostBDO postBDO = null;

            using (var BEntities = new BlogEntities())
            {
                var post = (from p in BEntities.Posts
                            where p.PostId == id
                            select p).FirstOrDefault();

                if (post != null)
                {
                    postBDO = new PostBDO()
                    {
                        PostId       = post.PostId,
                        PostTitle    = post.PostTitle,
                        PostContent  = post.PostContent,
                        PostDateTime = post.PostDateTime,
                        RowVersion   = post.RowVersion
                    }
                }
                ;
            }
            return(postBDO);
        }
Example #2
0
        public bool UpdatePost(ref Post post, ref string message)
        {
            var result = true;

            if (string.IsNullOrEmpty(post.PostTitle))
            {
                message = "Title cannot be empty";
                result  = false;
            }

            else if (string.IsNullOrEmpty(post.PostContent))
            {
                message = "Content cannot be empty";
                result  = false;
            }
            else
            {
                try
                {
                    var postBDO = new PostBDO();
                    TranslatePostDTOtoPostBDO(post, postBDO);
                    result          = postLogic.UpdatePost(ref postBDO, ref message);
                    post.RowVersion = postBDO.RowVersion;
                }
                catch (Exception e)
                {
                    var msg = e.Message;
                    throw new FaultException <PostFault>(new PostFault(msg), msg);
                }
            }
            return(result);
        }
Example #3
0
        public Post GetPost(int id)
        {
            PostBDO postBDO = null;

            try
            {
                postBDO = postLogic.GetPost(id);
            }
            catch (Exception e)
            {
                var msg    = e.Message;
                var reason = "GetPost Exception";
                throw new FaultException <PostFault>(new PostFault(msg), reason);
            }

            if (postBDO == null)
            {
                var msg    = string.Format("No post found for id {0}", id);
                var reason = "GetPost Empty Post";
                throw new FaultException <PostFault>(new PostFault(msg), reason);
            }

            var post = new Post();

            TranslatePostBDOToPostDTO(postBDO, post);
            return(post);
        }
Example #4
0
        //DTO -> BDO

        private void TranslatePostDTOtoPostBDO(Post post, PostBDO postBDO)
        {
            postBDO.PostId       = post.PostId;
            postBDO.PostTitle    = post.PostTitle;
            postBDO.PostContent  = post.PostContent;
            postBDO.PostDateTime = post.PostDateTime;
            postBDO.RowVersion   = post.RowVersion;
        }
Example #5
0
        public bool UpdatePost(ref PostBDO postBDO, ref string message)
        {
            var postInDB = GetPost(postBDO.PostId);

            //invalid post to update
            if (postInDB == null)
            {
                message = "cannot get post for this ID";
                return(false);
            }
            else
            {
                return(postDAO.UpdatePost(ref postBDO, ref message));
            }
        }
Example #6
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);
        }
Example #7
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);
        }
Example #8
0
 public bool CreatePost(ref PostBDO postBDO, ref string message)
 {
     return(postDAO.CreatePost(ref postBDO, ref message));
 }