public async Task <IActionResult> Comment(PostCommentViewModel vm) { if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(User.Identity.Name); if (user == null) { return(NotFound()); } var post = await _postRepository.GetPostByIdAsync(vm.PostId); if (post == null) { return(NotFound()); } var comment = new PostComment { Name = vm.Name, Body = vm.Body, Post = post }; comment.Owner = user; comment.Date = DateTime.UtcNow; await _postRepository.AddCommentAsync(comment); return(RedirectToAction(nameof(Details), new { id = comment.Post.Id })); } return(View(vm)); }
// GET: Comment/Index/1 public ActionResult Index(int Id) { Post post = _postRepository.GetPostById(Id); // Added after demo: Kicks 404 when user tries to enter invalid post id in url if (post == null) { return(NotFound()); } List <Comment> comments = _commentRepository.GetCommentsByPost(Id); // Added after demo: Kicks 404 when user tries to enter invalid post id in url if (comments == null) { return(NotFound()); } PostCommentViewModel vm = new PostCommentViewModel() { Post = post, Comments = comments, CurrentUserProfileId = GetCurrentUserProfileId() }; return(View(vm)); }
public async Task <IActionResult> EditComment(long?id) { if (id == null) { return(NotFound()); } var comment = await _postRepository.GetPostCommentByIdAsync(id.Value); if (comment == null) { return(NotFound()); } if (comment.Owner.Email != User.Identity.Name) { return(Unauthorized()); } var postComment = new PostCommentViewModel { Id = comment.Id, PostId = comment.Post.Id, Body = comment.Body, Name = comment.Name, Date = comment.Date, Owner = comment.Owner, Post = comment.Post, OwnerId = comment.Owner.Id }; return(View(postComment)); }
public ActionResult Comment(int id, PostCommentViewModel newPostComment) { if (newPostComment != null && ModelState.IsValid) { var laptop = this.Data.Laptops.GetById(id); if (laptop == null) { return(HttpNotFound("Laptop not found...")); } var currentUserId = this.User.Identity.GetUserId(); var newCommentContent = newPostComment.Content.Trim(); var newCommentEntity = new Comment() { Content = newCommentContent, AuthorId = currentUserId }; laptop.Comments.Add(newCommentEntity); this.Data.SaveChanges(); var currentUserUserName = this.User.Identity.GetUserName(); var newCommentViewModel = new CommentViewModel() { Content = newCommentContent, Author = currentUserUserName }; return(PartialView("_Comment", newCommentViewModel)); } var errorMessages = this.GetErrors(ModelState); return(PartialView("_Error", errorMessages)); }
public ActionResult Index(int?id, int?PostId) { var viewModel = new PostCommentViewModel(); viewModel.Posts = _db.Post .Include(p => p.Comments) //.Include(p=> p.Movie) .OrderByDescending(p => p.DateEdited); if (id != null) { ViewBag.PostID = id.Value; viewModel.Comments = viewModel.Posts.Where( c => c.PostId == id.Value).Single().Comments; } if (PostId != null) { ViewBag.PostID = PostId.Value; var selectedPost = viewModel.Posts.Where(x => x.PostId == PostId).Single(); _db.Entry(selectedPost).Collection(x => x.Comments).Load(); ViewBag.Post = viewModel.Post.Movie.Title; foreach (Comment comment in selectedPost.Comments) { _db.Entry(comment).Reference(x => x.Post).Load(); } viewModel.Comments = selectedPost.Comments; } return(View(viewModel)); }
public ActionResult PostComment(PostCommentViewModel comment) { if (comment != null && this.ModelState.IsValid) { var newComment = this.Mapper.Map<Comment>(comment); var userId = this.User.Identity.GetUserId(); var currentUser = this.manager.Users.FirstOrDefault(x => x.Id == userId); newComment.User = currentUser; var advertisement = this.advertisements.GetById(comment.AdvertisementId).FirstOrDefault(); if (advertisement == null) { throw new HttpException(404, "Advertisement not found"); } advertisement.Comments.Add(newComment); this.advertisements.Save(); var viewModel = this.Mapper.Map<CommentViewModel>(newComment); return this.PartialView("_CommentPartial", viewModel); } throw new HttpException(400, "Invalid comment"); }
public ActionResult PostComment(PostCommentViewModel comment) { if (comment != null && this.ModelState.IsValid) { var databaseComment = new Comment { Content = comment.Content, PostId = comment.PostId, Author = this.CurrentUser }; var post = this.posts.GetById(comment.PostId); if (post == null) { throw new HttpException(404, "Post not found"); } post.Comments.Add(databaseComment); this.posts.Update(); var viewModel = this.Mapper.Map <CommentViewModel>(databaseComment); return(this.PartialView("_CommentPartial", viewModel)); } throw new HttpException(400, "Invalid comment!"); }
public ActionResult PostComment(PostCommentViewModel model) { if (ModelState.IsValid) { var db = new ApplicationDbContext(); var user = db.Users.FirstOrDefault(u => u.UserName.ToLower() == User.Identity.Name.ToLower()); this.Data.Comments.Add(new Comment() { AuthorId = user.Id, Content = model.Content, TicketId = model.TicketId, }); this.Data.SaveChanges(); this.Success("Comment posted"); var viewModel = new CommentViewModel { AuthorName = user.UserName, Content = model.Content, AuthorPoints = user.Points }; return(PartialView("_CommentPartial", viewModel)); } return(new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, ModelState.Values.First().ToString())); }
public ActionResult PostComments(int? id,int count=5) { if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var skip = count - 5; var postCommentCount = _db.PostComments.Where(e => e.PostID == id).ToList().Count; var postCommentsList = (from t1 in _db.Users join t2 in _db.PostComments on t1.Id equals t2.UserId where t2.PostID==id select new PostCommentModel { PostComment = t2.PostComment, PostCommentDate = t2.PostCommentDate, UserName = t1.UserFirstName, UserSurname = t1.UserSurname, UserProfilePhoto = t1.UserProfilePhoto, UserId = t1.Id }).OrderByDescending(e => e.PostCommentDate).Skip(skip).Take(5).ToList(); var postCommentViewModel = new PostCommentViewModel { Count = count, CommentCount = postCommentCount, RemainingCommentCount = postCommentCount - count, PostId = (int)id, PostCommentModel = postCommentsList }; return PartialView("postComments",postCommentViewModel); }
public ActionResult PostComment(PostCommentViewModel comment) { if (comment != null && ModelState.IsValid) { var userId = User.Identity.GetUserId(); var userName = User.Identity.GetUserName(); this.Data.Comments.Add(new Comment { AuthorId = userId, TicketId = comment.TicketId, Content = comment.Content }); this.Data.SaveChanges(); var commentModel = new CommentViewModel { Content = comment.Content, AuthorUsername = userName }; return PartialView("_CommentPartial", commentModel); } return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, ModelState.Values.First().ToString()); }
public void Call_CreateReiew_IfCreatorId_IsDifferentThan_ReviewsUserId() { // Arrange var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedReviewService = new Mock <IReviewService>(); var creatorId = "creator123"; var reviewUserId = "reviewsuser123"; var controller = new ReviewController(mockedMappingProvider.Object, mockedReviewService.Object); controller.GetLoggedUserId = () => creatorId; var viewModel = new PostCommentViewModel(); viewModel.ReviewedUserId = reviewUserId; var review = new Review(); review.CreatorId = creatorId; review.ReviewedUserId = reviewUserId; mockedMappingProvider.Setup(x => x.Map <PostCommentViewModel, Review>(viewModel)) .Returns(review); // Act and Assert controller.WithCallTo(x => x.PostComment(viewModel)) .ShouldRenderPartialView("_Comment"); mockedReviewService.Verify(x => x.CreateReview(review), Times.Once); }
public ActionResult PostComment(PostCommentViewModel model) { if (!ModelState.IsValid) { var post = db.Posts.Find(model.PostId); if (post.ParentId != 0) { return(RedirectToAction("Details", "Posts", new { id = post.ParentId })); } return(RedirectToAction("Details", "Posts", new { id = model.PostId })); } model.OwnerId = User.Identity.GetUserId(); var result = comment.PostComment(model); if (result != 0) { Post post = db.Posts.Find(result); if (post.ParentId == 0) { return(RedirectToAction("Details", "Posts", new { id = result })); } else { return(RedirectToAction("Details", "Posts", new { id = post.ParentId })); } } return(RedirectToAction("Details", "Posts", new { id = model.PostId })); }
//private readonly IHttpContextAccessor _httpContextAccessor; //public OtherClass(IHttpContextAccessor httpContextAccessor) //{ // _httpContextAccessor = httpContextAccessor; //} //public void YourMethodName() //{ // var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // // or // var userId = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); //} #region PostComments public IActionResult Comment(long id) { var postComment = new PostCommentViewModel { PostId = id }; return(View(postComment)); }
public ActionResult Comment(PostCommentViewModel comment) { var client = ServiceHelper.GetServiceClientLoggedIn(); client.PostComment(comment.Id, comment.Content); //Back to the ad return(RedirectToAction("Display", new { id = comment.Id })); }
public ViewResult Details(int PostId) { PostCommentViewModel postCommentViewModel = new PostCommentViewModel() { Post = pd.GetPost(PostId), Comment = pd.GetCommentByPostId(PostId) }; return(View(postCommentViewModel)); }
public ActionResult PostComment(PostCommentViewModel comment) { if (comment != null && ModelState.IsValid) { CommentViewModel commentVM = this.commentServices.CreatNewComment(comment); return(PartialView("_CommentPartial", commentVM)); } throw new HttpException(400, "Invalid comment!"); }
public async Task <IActionResult> SendAsync([FromRoute] Guid postId, [FromBody] PostCommentViewModel comment) { if (comment == null) { throw new ArgumentNullException(nameof(comment)); } await _commentsRepository.CreatePostCommentAsync(this.GetUserLoginInfo(), postId, comment.Text, DateTimeOffset.UtcNow); return(StatusCode(201)); }
//public ActionResult Details(int? id) public ActionResult Details(int id) { Post post = db.Posts.Find(id); if (post == null) { return(HttpNotFound()); } var viewModel = new PostCommentViewModel(id); return(View(viewModel)); }
public async Task <IActionResult> EditComment(long id, PostCommentViewModel vm) { if (id != vm.Id) { return(NotFound()); } if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(User.Identity.Name); if (user == null || user.Id != vm.OwnerId) { return(NotFound()); } var post = await _postRepository.GetByIdAsync(vm.PostId); if (post == null) { return(NotFound()); } var comment = new PostComment { Id = vm.Id, Name = vm.Name, Body = vm.Body, Owner = user, Date = vm.Date, UpdatedDate = DateTime.UtcNow, Post = post }; try { await _postRepository.UpdateCommentAsync(comment); } catch (DbUpdateConcurrencyException) { if (!await PostCommentExists(comment.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details), new { id = comment.Post.Id })); } return(View(vm)); }
public async Task <IActionResult> Read(int id) { Post post = await unitOfWork.Posts .SearchFor(p => p.Id == id) .Include(p => p.Comments) .SingleOrDefaultAsync(); PostCommentViewModel postCommentViewModel = new PostCommentViewModel() { Post = post }; return(View(postCommentViewModel)); }
public ViewResult MyPost(int id) { //Id = id; //var post = pd.GetPostById(id); //var sharepsot = pd.GetSharePostByPostId(id); PostCommentViewModel postCommentViewModel = new PostCommentViewModel() { Post = pd.GetPostById(id), sharePosts = pd.GetSharePostByUserId(id) }; return(View(postCommentViewModel)); }
public ActionResult GetPostComment(string reviewFor) { if (this.GetLoggedUserId() == reviewFor) { return(new EmptyResult()); } var model = new PostCommentViewModel() { ReviewedUserId = reviewFor }; return(this.PartialView("_PostComment", model)); }
// GET: Comment/Index/1 public ActionResult Index(int Id) { Post post = _postRepository.GetPostById(Id); List <Comment> comments = _commentRepository.GetCommentsByPost(Id); PostCommentViewModel vm = new PostCommentViewModel() { Post = post, Comments = comments, CurrentUserProfileId = GetCurrentUserProfileId() }; return(View(vm)); }
public void ReturnEmpyResult_WhenModelStateIsNotValid() { // Arrange var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedReviewService = new Mock <IReviewService>(); var controller = new ReviewController(mockedMappingProvider.Object, mockedReviewService.Object); controller.ModelState.AddModelError("", ""); var viewModel = new PostCommentViewModel(); // Act and Assert controller.WithCallTo(x => x.PostComment(viewModel)) .ShouldReturnEmptyResult(); }
public async System.Threading.Tasks.Task <ActionResult> SaveCommentAsync(PostCommentViewModel viewModel) { if (ModelState.IsValid) { var comment = _mapper.Map <Comment>(viewModel); var user = await _userManager.GetUserAsync(HttpContext.User); comment.UserId = user.UserId; comment.IsActive = true; if (_commentService.Insert(comment)) { return(Json(true)); } } return(Json(false)); }
public ActionResult Create(PostCommentViewModel theComment) { var user = this.PersistenceContext.Users.FirstOrDefault(u => u.UserName == theComment.CommentAuthor); var post = this.PersistenceContext.Posts.FirstOrDefault(p => p.Id == theComment.CommentedPostID); PostComment comment = new Models.PostComment { Content = theComment.Content, CommentedPost = post, CommentAuthor = user, }; this.PersistenceContext.PostsComments.Add(comment); this.PersistenceContext.SaveChanges(); return(RedirectToAction("Index", "Post")); }
public IHttpActionResult PostComment(PostCommentViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Comments comment = new Comments { CommentDate = DateTime.Now, NewsFK = model.NewsFK, // Jornalista 1 por defeito. // As dependências da minha base de dados requeriam que fosse // criado um utilizador por comment. Para evitar isso, vou atribui // os novos comments ao utilizador 1 UserProfileFK = model.UserProfileID, Content = model.Content, }; db.Comments.Add(comment); try { db.SaveChanges(); } catch (DbUpdateException) { return(Conflict()); } // Recolhe o comment que foi gravado comment = db.Comments.OrderByDescending(c => c.CommentDate).First(); // Porque ainda o utilizador ainda não está associado no comment.UserProfile UsersProfile user = db.UsersProfile.Find(model.UserProfileID); GetCommentViewModel response = new GetCommentViewModel { ID = comment.ID, User = user.Name, Date = comment.CommentDate.ToString("MM-dd-yyyy"), UserID = user.ID, Content = comment.Content }; return(CreatedAtRoute("DefaultApi", new { id = comment.ID }, response)); }
public ActionResult AllByPostId(int id) { var comments = this.postCommentService .GetAllAndDeleted() .Where(c => c.PostId == id) .OrderByDescending(p => p.CreatedOn) .ToList(); var commentsModel = mappingService.Map <IList <PostComment>, ICollection <PostCommentAnnotationViewModel> >(comments); var postCommentsViewModel = new PostCommentViewModel() { Comments = commentsModel }; return(this.View(postCommentsViewModel)); }
public CommentViewModel CreatNewComment(PostCommentViewModel comment) { Comment newComment = Mapper.Map <Comment>(comment); newComment.Author = this.Data.Users.GetById(HttpContext.Current.User.Identity.GetUserId()); Ticket ticket = this.Data.Tickets.GetById(comment.TicketId); if (ticket == null) { throw new HttpException(404, "Ticket not found!"); } ticket.Comments.Add(newComment); this.Data.SaveChanges(); CommentViewModel comentVM = Mapper.Map <CommentViewModel>(newComment); return(comentVM); }
// GET: CommentsController public ActionResult Index(int id) { Post post = _postRepository.GetPublishedPostById(id); List <Comment> comments = _commentRepository.GetAllCommentsByPostId(id); if (post == null || comments == null) { return(NotFound()); } PostCommentViewModel vm = new PostCommentViewModel { Post = post, Comments = comments }; return(View(vm)); }
public async Task <IActionResult> PostComment(PostCommentViewModel vm) { if (!ModelState.IsValid) { return(RedirectToAction("Post", new { id = vm.PostId, slug = vm.PostSlug })); } else { var user = await _userManager.GetUserAsync(HttpContext.User); if (vm.MainCommentId == 0) { var comment = new PostMainComment { PostId = vm.PostId, PostSlug = vm.PostSlug, Message = vm.Message, CreatedDate = DateTime.Now, User = user }; _repo.AddPostMainComment(comment); string notifyMailText = EmailHelper.BuildTemplate(_templatesPath, "NewCommentTemplate.html"); notifyMailText = notifyMailText.Replace("[username]", user.UserName).Replace("[slug]", vm.PostSlug).Replace("[message]", vm.Message); await _emailService.SendAsync("*****@*****.**", "New Post Main Comment Added", notifyMailText, true); } else { var comment = new PostSubComment { PostMainCommentId = vm.MainCommentId, Message = vm.Message, CreatedDate = DateTime.Now, User = user }; _repo.AddPostSubComment(comment); string notifyMailText = EmailHelper.BuildTemplate(_templatesPath, "NewCommentTemplate.html"); notifyMailText = notifyMailText.Replace("[username]", user.UserName).Replace("[slug]", vm.MainCommentId.ToString()).Replace("[message]", vm.Message); await _emailService.SendAsync("*****@*****.**", "New Post Sub Comment Added", notifyMailText, true); } await _repo.SaveChangesAsync(); return(RedirectToAction("Post", new { id = vm.PostId, slug = vm.PostSlug })); } }
public ActionResult PostComment(PostCommentViewModel comment) { if (comment!=null && ModelState.IsValid) { var dbComment = Mapper.Map<Comment>(comment); dbComment.Author = this.UserProfile; var ticket = this.Data.Tickets.GetById(comment.TicketId); if (ticket == null) { throw new HttpException(404, "Ticket not found"); } ticket.Comments.Add(dbComment); this.Data.SaveChanges(); var viewModel = Mapper.Map<CommentViewModel>(dbComment); return PartialView("_CommentPartial", viewModel); } throw new HttpException(400, "Invalid comment"); }