public static CommentModel ToModel(Comment commentEntity)
        {
            CommentModel commentModel = new CommentModel()
            {
                Author = commentEntity.Author.DisplayName,
                PostDate = commentEntity.PostDate,
                Text = commentEntity.Text
            };

            return commentModel;
        }
        public static Comment ToEntity(CommentModel commentModel, User author)
        {
            Comment commentEntity = new Comment()
            {
                Author = author,
                PostDate = DateTime.Now,
                Text = commentModel.Text
            };

            return commentEntity;
        }
        public HttpResponseMessage AddComment(int postId, CommentModel comment,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BloggingSystemContext();
                var user = GetAndValidateUser(sessionKey, context);
                var postEntity = context.Posts.FirstOrDefault(p => p.Id == postId);
                var commentEntity = new Comment() { PostDate = DateTime.Now, Text = comment.Text, User = user };
                postEntity.Comments.Add(commentEntity);
                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.OK);
                return response;
            });

            return responseMsg;
        }
示例#4
0
        public HttpResponseMessage CreateComment(int postId, [FromBody] CommentDto value)
        {
            try
            {
                var sessionKey = ApiControllerHelper.GetHeaderValue(Request.Headers, "X-SessionKey");
                if (sessionKey == null)
                {
                    throw new ArgumentNullException("No session key provided in the request header!");
                }

                Validate(value.Text, "text");

                var context = new BloggingSystemContext();

                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Users must be logged in to leave comments!");
                    }

                    var post = context.Posts.FirstOrDefault(p => p.Id == postId);
                    if (post == null)
                    {
                        throw new ArgumentException("Invalid post id: " + postId);
                    }

                    var newComment = new Comment()
                    {
                        Text = value.Text,
                        PostDate = DateTime.Now,
                        Author = user,
                        Post = post
                    };

                    context.Comments.Add(newComment);
                    context.SaveChanges();

                    var response = Request.CreateResponse(HttpStatusCode.OK);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
                throw new HttpResponseException(errorResponse);
            }
        }
示例#5
0
        public HttpResponseMessage PutComment(int postId, [FromBody]Comment comment, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);
                 using (context)
                 {
                     var currPost = context.Posts.FirstOrDefault(p => p.Id == postId);

                     if (currPost == null)
                     {
                         throw new InvalidOperationException("Post does not exist!");
                     }

                     Comment newComment = new Comment()
                     {
                         Id = comment.Id,
                         Date = comment.Date,
                         Post = comment.Post,
                         Text = comment.Text,
                         User = comment.User
                     };

                     currPost.Comments.Add(newComment);
                     context.Posts.Attach(currPost);
                     context.Entry(currPost).CurrentValues.SetValues(currPost);
                     context.SaveChanges();
                 }

                 return this.Request.CreateResponse(HttpStatusCode.Created);
             });

            return responseMsg;
        }
        public HttpResponseMessage PutCommentOnPost(int postId, CreateCommentModel model)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
            {
                var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey");
                var dbContext = new BlogContext();
                // dbContext.Configuration.ProxyCreationEnabled = false;

                using (dbContext)
                {
                    var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Users must be logged when create a new comment!");
                    }

                    var post = dbContext.Posts.FirstOrDefault(pst => pst.Id == postId);
                                     
                                        
                    var newComment = new Comment()
                    {
                        
                        Text = model.Text,
                        CommentDate = DateTime.Now,
                        User = user,
                        Post=post
                    };

                    dbContext.Comments.Add(newComment);
                    dbContext.SaveChanges();

                    var response =
                          this.Request.CreateResponse(HttpStatusCode.OK);
                    return response;
                }
            });
            return responseMessage;

        }
        public HttpResponseMessage PutNewComment([FromUri] int postId, [FromUri] string sessionKey, [FromBody] CommentModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();
                    using (context)
                    {
                        var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

                        if (user == null)
                        {
                            throw new InvalidOperationException("Users doesn't exists");
                        }

                        var post = context.Posts.FirstOrDefault(p => p.Id == postId);

                        if (post == null)
                        {
                            throw new InvalidOperationException("This post doesn't exists");
                        }

                        Comment newComment = new Comment();
                        newComment.Text = model.Text;
                        newComment.PostDate = DateTime.Now;
                        newComment.CommentedBy = user;
                        newComment.Post = post;

                        post.Comments.Add(newComment);
                        context.Comments.Add(newComment);

                        context.SaveChanges();

                        var response = this.Request.CreateResponse(HttpStatusCode.OK);

                        return response;
                    }
                });
            return responseMsg;
        }
        public HttpResponseMessage PutComment(CommentCreatedModel leavedCommentModel, int postId,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var context = new BloggingSystemContext();
            using (context)
            {
                var entityComment = context.Posts.FirstOrDefault(p => p.Id == postId);
                var commentModel = new CommentCreatedModel()
                {
                    Id = entityComment.Id,
                    Text = leavedCommentModel.Text
                };

                var newComment = new Comment()
                {
                    Text = commentModel.Text,
                    PostDate = DateTime.Now,
                    Id = commentModel.Id,
                };

                context.Comments.Add(newComment);
                context.SaveChanges();

                return this.Request.CreateResponse(HttpStatusCode.OK);
            }
        }
        public HttpResponseMessage PutNewComment(
            int postId, 
            [FromBody] CommentModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                ValidateCommentText(model.Text);

                var context = new BloggingSystemContext();
                using (context)
                {
                    var user = GetValidUser(sessionKey);

                    if (user == null)
                    {
                        throw new InvalidOperationException("You are not logged in!");
                    }

                    var post = context.Posts.Where(ps => ps.Id == postId).FirstOrDefault();

                    if (post == null)
                    {
                        throw new InvalidOperationException("No such post exists");
                    }

                    Comment newComment = new Comment()
                    {
                        Text = model.Text,
                        CommentDate = DateTime.Now,
                        User = user,
                        Post = post
                    };

                    context.Comments.Add(newComment);
                    context.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.Created);
                }
            });

            return responseMsg;
        }