public HttpResponseMessage LeaveComment(int postId, [FromBody] CommentModel commentModel,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);

                if (postId < 0)
                {
                    throw new ArgumentOutOfRangeException("Post ID must be a positive inreger number!");
                }

                var context = this.contextFactory.Create();

                using (context)
                {
                    var user = context.Set<User>().Where(x => x.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid session key authentication!");
                    }

                    var post = context.Set<Post>().Where(x => x.Id == postId).FirstOrDefault();

                    if (post == null)
                    {
                        throw new InvalidOperationException("Invalid post ID!");
                    }

                    Comment commentToAdd = new Comment()
                    {
                        Content = commentModel.Content,
                        PostDate = DateTime.Now,
                        Post = post,
                        Creator = user
                    };

                    context.Set<Comment>().Add(commentToAdd);
                    context.SaveChanges();

                    var response = this.Request.CreateResponse(HttpStatusCode.OK, "Comment added!");
                    return response;
                }
            });

            return responseMessage;
        }
Exemplo n.º 2
0
        public ActionResult WriteComment(Comment comment)
        {
            if (comment != null)
            {
                var post = db.Posts.GetById(comment.Post.Id);
                if (post != null)
                {
                    comment.Post = post;
                    var user = db.Users.FindUser(User.Identity.GetUserId());
                    comment.User = user;
                    db.Comments.Add(comment);
                    db.SaveChanges();

                    return PartialView("_SingleComment", comment);
                }
            }

            return PartialView("_writeComment", comment);
        }
        public HttpResponseMessage PostCreateComment(CommentCreateModel inputComment, string sessionKey)
        {
            HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  ForumContext context = new ForumContext();

                  User currentUser = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

                  Post currentPost = context.Posts.FirstOrDefault(pst => pst.Id == inputComment.PostId);

                  if (currentUser == null)
                  {
                      throw new ArgumentNullException("If you want to comment posts, you should be logged or registered.");
                  }

                  if (currentPost == null)
                  {
                      throw new ArgumentNullException("The post you are trying to comment, doesn't exist.");
                  }

                  Comment newComment = new Comment()
                  {
                      Author = currentUser,
                      Post = currentPost,
                      Content = inputComment.Content,
                      CreationDate = DateTime.Now
                  };

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

                  CommentModel createdComment = new CommentModel()
                  {
                      Author = newComment.Author.Username,
                      Content = newComment.Content,
                      CreationDate = newComment.CreationDate
                  };

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

            return responseMessage;
        }
        //public HttpResponseMessage PostNewPost(PostNewModel model)
        //{
        //    var responseMsg = this.PerformOperationAndHandleExceptions(
        //      () =>
        //      {
        //          var context = new BlogSystemContext();
        //          using (context)
        //          {
        //              var user = context.Users.FirstOrDefault(
        //                  usr => usr.SessionKey == sessionKey);
        //              if (user == null)
        //              {
        //                  throw new InvalidOperationException("Invalid sessionKey");
        //              }
        //              if (model.Title == null || model.Text == null)
        //              {
        //                  throw new ArgumentNullException("Post title or post text cannot be null");
        //              }
        //              string[] titleWords = model.Title.Split(
        //                  new char[] { ' ', ',', '.', '!', '?', '\'', '(', ')' },
        //                  StringSplitOptions.RemoveEmptyEntries);
        //              IList<Tag> tags = new List<Tag>();
        //              if (model.Tags != null)
        //              {
        //                  foreach (var item in model.Tags)
        //                  {
        //                      var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower());
        //                      if (tag == null)
        //                      {
        //                          tag = new Tag()
        //                          {
        //                              Name = item.ToLower()
        //                          };
        //                          context.Tags.Add(tag);
        //                          context.SaveChanges();
        //                      }
        //                      tags.Add(tag);
        //                  }
        //              }
        //              foreach (var item in titleWords)
        //              {
        //                  var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower());
        //                  if (tag == null)
        //                  {
        //                      tag = new Tag()
        //                      {
        //                          Name = item.ToLower()
        //                      };
        //                      context.Tags.Add(tag);
        //                      context.SaveChanges();
        //                  }
        //                  tags.Add(tag);
        //              }
        //              var post = new Post()
        //              {
        //                  Title = model.Title,
        //                  Text = model.Text,
        //                  PostDate = DateTime.Now,
        //                  User = user,
        //                  Tags = tags
        //              };
        //              context.Posts.Add(post);
        //              context.SaveChanges();
        //              var createdModel = new PostCreatedModel()
        //              {
        //                  Id = post.Id,
        //                  Title = post.Title
        //              };
        //              var response =
        //                  this.Request.CreateResponse(HttpStatusCode.Created,
        //                                  createdModel);
        //              return response;
        //          }
        //      });
        //    return responseMsg;
        //}
        //[ActionName("comment")]
        public HttpResponseMessage PutComment(int id, CommentModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new ForumContext();
                  using (context)
                  {
                      var post = context.Posts.FirstOrDefault(p => p.Id == id);

                      if (post == null)
                      {
                          throw new InvalidOperationException("Invalid id");
                      }

                      var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                      //var user = context.Users.FirstOrDefault(u => u.Id == 2);
                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid sessionKey");
                      }

                      if (model.Text == null)
                      {
                          throw new ArgumentNullException("Comment text cannot be null");
                      }

                      var comment = new Comment()
                      {
                          Text = model.Text,
                          Creator = user,
                          CreatedOn = DateTime.Now,
                          Post = post
                      };

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

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

            return responseMsg;
        }