public IHttpActionResult EditPost(int id, EditPostBindingModel model) { var post = this.Data.Posts.GetById(id); if (post == null) { return(this.BadRequest(Messege.NoSuchPostError)); } if (this.UserIdProvider.GetUserId() != post.Author.Id) { return(this.BadRequest(Messege.NotYourPostError)); } if (model.Title != null) { post.Title = model.Title; } if (model.Comment != null) { post.Comment = model.Comment; } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } this.Data.Posts.Update(post); this.Data.SaveChanges(); return(this.Ok()); }
public ActionResult Edit(int id, EditPostBindingModel model) { Post editPost = this.GetPost(id); if (editPost == default(Post) || editPost.IsDeleted || id != model.Id) { return(this.RedirectToAction("BadRequest", "Error")); } if (!this.ModelState.IsValid) { return(this.View(model)); } User user = this.UnitOfWork .UserRepository .Select(u => u.UserName == this.HttpContext.User.Identity.Name) .FirstOrDefault(); editPost.Text.Text = model.Text; editPost.Changer = user; editPost.ChangeDate = DateTime.Now; this.UnitOfWork .PostRepository .Update(editPost); this.UnitOfWork.SaveChanges(); // to do return(this.RedirectToAction("View", "Post", new { Id = editPost.PostId })); }
public IHttpActionResult EditPost(EditPostBindingModel bindingModel, int postId) { if (bindingModel == null) { return(this.BadRequest("Invalid data!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } var currentUserId = this.UserIdProvider.GetUserId(); if (post.AuthorId != currentUserId) { return(this.BadRequest("You have no permissions to edit this post.")); } post.Content = bindingModel.Content; Data.SaveChanges(); return(this.Ok()); }
public IHttpActionResult EditPost(int id, EditPostBindingModel model) { var post = this.Data.Posts.GetById(id); if (post == null) { return this.BadRequest(Messege.NoSuchPostError); } if (this.UserIdProvider.GetUserId() != post.Author.Id) { return this.BadRequest(Messege.NotYourPostError); } if (model.Title != null) { post.Title = model.Title; } if (model.Comment != null) { post.Comment = model.Comment; } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } this.Data.Posts.Update(post); this.Data.SaveChanges(); return this.Ok(); }
public ActionResult Edit(EditPostBindingModel model) { if (ModelState.IsValid) { this.service.UpdatePost(model); return(RedirectToAction("Index")); } var vm = this.service.GetViewModel(model); return(View(vm)); }
public void UpdatePost(EditPostBindingModel model) { if (model.Id < 0) { throw new ArgumentException(Consts.InvlidIdError); } var postToEdit = this.Context.Posts.FirstOrDefault(m => m.Id == model.Id); postToEdit.Title = model.Title; postToEdit.Content = model.Content; postToEdit.DateEdited = DateTime.Now; this.Context.SaveChanges(); }
public async Task <HttpResponseMessage> EditPostById([FromUri] int id, [FromBody] EditPostBindingModel model) { if (model == null) { return(await this.BadRequest("Post cannot be empty.").ExecuteAsync(new CancellationToken())); } if (!this.ModelState.IsValid) { return(await this.BadRequest(this.ModelState).ExecuteAsync(new CancellationToken())); } var post = this.Data.GroupPosts.FirstOrDefault(p => p.Id == id); if (post == null) { return(await this.NotFound().ExecuteAsync(new CancellationToken())); } var currentUserId = User.Identity.GetUserId(); var currentUser = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId); if (currentUser == null) { return(await this.BadRequest("Invalid user token! Please login again!").ExecuteAsync(new CancellationToken())); } if (post.Author != currentUser) { return(await this.Unauthorized().ExecuteAsync(new CancellationToken())); } post.Content = model.postContent; this.Data.SaveChanges(); var postPreview = this.Data.GroupPosts.Where(p => p.Id == post.Id) .Select(GroupPostViewModel.Create) .FirstOrDefault(); return(await this.Ok(postPreview).ExecuteAsync(new CancellationToken())); }
/// <summary> /// Empty action for edit /// </summary> /// <param name="id">Id of post</param> /// <returns>View of EditPostBindingModel</returns> public ActionResult Edit(int id) { Post post = this.GetPost(id); if (post == default(Post) || post.IsDeleted) { return(this.RedirectToAction("BadRequest", "Error")); } EditPostBindingModel postToEdit = new EditPostBindingModel() { Id = post.PostId, Text = post.Text.Text, Thread = new IdentifiableThreadBindingModel() { Id = post.Thread.ThreadId, Title = post.Thread.Title } }; return(this.View(postToEdit)); }
public IHttpActionResult EditPost( int id, [FromBody] EditPostBindingModel model) { //check if the post exists var post = this.Context.Posts.Find(id); if (post == null) { return(this.NotFound()); } var loggedInUser = this.User.Identity.GetUserId(); if (loggedInUser != post.AuthorId) { return(this.Unauthorized()); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (model == null) { return(this.BadRequest("No model data is sent!")); } post.Content = model.Content; this.Context.SaveChanges(); var data = this.Context.Posts .Where(p => p.Id == post.Id) .Select(PostViewModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public IHttpActionResult Put(int id, EditPostBindingModel post) { var userId = this.User.Identity.GetUserId(); if (userId == null) { return(this.BadRequest("Invalid session token.")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var existingPost = this.SocialNetworkData.Posts .All() .FirstOrDefault(p => p.Id == id); if (existingPost == null) { return(this.NotFound()); } if (existingPost.AuthorId != userId) { return(this.BadRequest("Only post author can edit posts.")); } existingPost.Content = post.PostContent; this.SocialNetworkData.Posts.SaveChanges(); post.Id = existingPost.Id; return(this.Ok(new { id, content = post.PostContent })); }
public IHttpActionResult EditPost(int postId, EditPostBindingModel model) { var post = this.Data.Posts.Find(postId); if (post == null) { return(this.NotFound()); } var loggedUserId = this.User.Identity.GetUserId(); if (loggedUserId != post.UserId) { return(this.Unauthorized()); } if (model == null) { return(this.BadRequest("Model cannot be empty!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } post.Content = model.Content; this.Data.SaveChanges(); var data = this.Data.Posts .Where(p => p.Id == post.Id) .Select(PostDataModel.Create) .FirstOrDefault(); return(this.Ok(data)); }
public IHttpActionResult EditPost(EditPostBindingModel bindingModel, int postId) { if (bindingModel == null) { return this.BadRequest("Invalid data!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } var currentUserId = this.UserIdProvider.GetUserId(); if (post.AuthorId != currentUserId) { return this.BadRequest("You have no permissions to edit this post."); } post.Content = bindingModel.Content; Data.SaveChanges(); return this.Ok(); }
public IHttpActionResult EditPost(int postId, EditPostBindingModel model) { var post = this.Data.Posts.Find(postId); if (post == null) { return this.NotFound(); } var loggedUserId = this.User.Identity.GetUserId(); if (loggedUserId != post.UserId) { return this.Unauthorized(); } if (model == null) { return this.BadRequest("Model cannot be empty!"); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } post.Content = model.Content; this.Data.SaveChanges(); var data = this.Data.Posts .Where(p => p.Id == post.Id) .Select(PostDataModel.Create) .FirstOrDefault(); return this.Ok(data); }
public PostViewModel GetViewModel(EditPostBindingModel model) { return(Mapper.Map <EditPostBindingModel, PostViewModel>(model)); }