public IActionResult AddComment(int postId, int userId, CommentForCreationDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(Messages.ModelNullOrEmpty));
            }
            var result = _postCommentService.PostCommentExist(userId, postId);

            if (result.Data == true)
            {
                return(BadRequest(Messages.OneCommentToOnePost));
            }

            PostComment postComment = new PostComment()
            {
                UserId      = userId,
                PostId      = postId,
                Comment     = model.Comment,
                DateCreated = DateTime.Now
            };

            _postCommentService.Create(postComment);
            return(Ok());
        }
Beispiel #2
0
        public JsonResult CreatePostComment([FromBody] PostCommentCreateApi model)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    if (string.IsNullOrEmpty(model.Text))
                    {
                        Result.Status  = false;
                        Result.Message = "Comment text can not be null ! ";
                        return(BadResponse(Result));
                    }
                    else
                    {
                        var commentPost = _postService.GetById(model.PostId);
                        var appUser     = _userService.FindByUserName(User.Identity.Name);
                        if (appUser == null)
                        {
                            return(BadResponse(new ResultModel
                            {
                                Status = false,
                                Message = "User not found ! "
                            }));
                        }

                        PostComment newComment = new PostComment
                        {
                            PostId    = model.PostId,
                            Text      = model.Text,
                            CreatedBy = appUser.Id
                        };

                        ResultModel result = _postCommentService.Create(newComment);
                        if (result.Status)
                        {
                            scope.Complete();
                            return(OkResponse(new PostCommentListDto
                            {
                                Text = newComment.Text,
                                Id = newComment.Id,
                                PostId = newComment.PostId,
                                CreatedDate = newComment.CreatedDate,
                                CreatedByUserName = appUser.UserName,
                                CreatedByUserPhoto = appUser.UserDetail.ProfilePhotoPath
                            }));
                        }
                        else
                        {
                            Result.Status  = false;
                            Result.Message = "Comment could not be added ! ";
                            return(BadResponse(Result));
                        }
                    }
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    Result.Status  = false;
                    Result.Message = ex.ToString();
                    return(BadResponse(Result));
                }
            }
        }