public IActionResult AddCommentToPost(AddCommentDTO addCommentDTO) { try { var CurrentUser = _context.Users.Find(userManager.GetUserId(User)); if (CurrentUser is null) { return(RedirectToAction("Login", "Accounts")); } var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id); if (ModelState.IsValid) { var existingPost = _postStore.ViewPost(_currentpost.Id); if (existingPost is null) { return(RedirectToAction("NotFound", "Accounts")); } var comment = new Comment() { Content = addCommentDTO.Content, PostId = _currentpost.Id, UserId = CurrentUser.Id, DatePosted = DateTime.Now }; existingPost.Comments.Add(comment); _commentStore.AddComment(comment); if (existingPost.UserId != CurrentUser.Id) { var Notification = new Notification() { Message = $"{CurrentUser.UserName} has commented on your post '{existingPost.Content}' ", NotificationBoxId = _notificationBox.GetNotificationBoxByUserId(existingPost.UserId).Id, RecieverUsername = _userStore.GetByIdentityUserId(existingPost.UserId).Username, NotificationDate = DateTime.Now }; _notificationBox.SendNotifcation(Notification); } _context.SaveChanges(); return(RedirectToAction("ViewPost", "Posts", new { id = existingPost.Id })); } else { return(View(addCommentDTO)); } } catch (Exception ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts)); } }
public ActionResult <Comment> AddComment(Comment comment) { comment.Id = commentStore.AddComment(comment); return(CreatedAtAction(nameof(GetComment), new { comment.Id }, comment)); }