/// <summary>
 /// Adapts a CommentFormViewModel to a PageComment.
 /// </summary>
 /// <param name="formViewModel">The comment form view model.</param>
 /// <returns>PageComment</returns>
 private PageComment AdaptCommentFormViewModelToSocialComment(CommentFormViewModel formViewModel)
 {
     return(new PageComment
     {
         Target = _pageRepository.GetPageId(formViewModel.CurrentPageLink),
         Body = formViewModel.Body,
         AuthorId = _userRepository.GetUserId(User)
     });
 }
        /// <summary>
        /// Validates the comment form.
        /// </summary>
        /// <param name="formViewModel">The comment form view model.</param>
        /// <returns>Returns a list of validation errors.</returns>
        private List <string> ValidateCommentForm(CommentFormViewModel formViewModel)
        {
            var errors = new List <string>();

            // Make sure the comment body has some text
            if (string.IsNullOrWhiteSpace(formViewModel.Body))
            {
                errors.Add(BodyValidationErrorMessage);
            }

            return(errors);
        }
        /// <summary>
        /// Adds the comment in the CommentFormViewModel to the Episerver Social repository.
        /// </summary>
        /// <param name="formViewModel">The submitted comment form view model.</param>
        /// <returns>The added PageComment</returns>
        private PageComment AddComment(CommentFormViewModel formViewModel)
        {
            var         newComment   = AdaptCommentFormViewModelToSocialComment(formViewModel);
            PageComment addedComment = null;

            try
            {
                addedComment = _commentRepository.Add(newComment);
                AddMessage(MessageKey, new MessageViewModel(SubmitSuccessMessage, SuccessMessage));
            }
            catch (SocialRepositoryException ex)
            {
                AddMessage(MessageKey, new MessageViewModel(ex.Message, ErrorMessage));
            }

            return(addedComment);
        }
        public ActionResult Submit(CommentFormViewModel formViewModel)
        {
            var errors = ValidateCommentForm(formViewModel);

            if (errors.Count() == 0)
            {
                var addedComment = AddComment(formViewModel);
                if (addedComment != null && formViewModel.SendActivity)
                {
                    AddCommentActivity(addedComment);
                }
            }
            else
            {
                // Flag the CommentBody model state with validation error
                AddMessage(MessageKey, new MessageViewModel(errors.First(), ErrorMessage));
            }

            return(Redirect(UrlResolver.Current.GetUrl(formViewModel.CurrentPageLink)));
        }