Example #1
0
        public async Task <IActionResult> CreateAsync([FromBody] CommentCreateModel commentCreateModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (commentCreateModel == null)
            {
                return(BadRequest());
            }

            if (commentCreateModel.Content.Length > 8000)
            {
                return(BadRequest());
            }

            var comment        = CommentMapper.ConvertToCommentDTO(commentCreateModel);
            var createdComment = await _commentService.CreateAsync(comment);

            var commentViewModel = CommentMapper.ConvertToCommentViewModel(createdComment);


            var post = await _postService.GetAsync(comment.PostId);

            var commentators = (await _commentService.FindValidAsync(c =>
                                                                     c.PostId == comment.PostId &&
                                                                     c.User.Email != post.UserDTO.Email &&
                                                                     c.UserId != comment.UserId))
                               .Select(c => new CommentEmailModel()
            {
                Email     = c.UserDTO.Email,
                FirstName = c.UserDTO.FirstName,
                LastName  = c.UserDTO.LastName
            }).Distinct().ToList();

            _emailSender.PostCommentNotificationAsync(commentViewModel, post, commentators);
            return(Ok(commentViewModel));
        }