public void CreateComment_TestNullParameter_ActualFalse()
        {
            //Arrange
            CommentWriteDTO commentWriteDTO = null;

            //Act
            var action = commentService.Create(commentWriteDTO);

            //Assert
            Assert.AreEqual(false, action.Result.status);
        }
        public void CreateComment_TestPostWriteDTODataNull_ActualFalse()
        {
            CommentWriteDTO commentWriteDTO = new CommentWriteDTO
            {
                CommentContent = null,
                DateCreated    = DateTime.Now,
                UserID         = Guid.Empty,
                CommentID      = Guid.Empty
            };

            //Act
            var action = commentService.Create(commentWriteDTO);

            //Assert
            Assert.AreEqual(false, action.Result.status);
        }
        public void CreateComment_Success_ActualTrue()
        {
            //Arrange
            int PostID = PostTest.PostID;

            CommentWriteDTO commentWriteDTO = new CommentWriteDTO
            {
                CommentContent = "Comment Content",
                DateCreated    = DateTime.Now,
                UserID         = UserTest.UserID,
                CommentID      = Guid.NewGuid(),
                PostID         = PostTest.PostID
            };

            //Act
            var action = commentService.Create(commentWriteDTO);

            //Assert
            Assert.AreEqual(true, action.Result.status);
        }
Ejemplo n.º 4
0
        public async Task <dynamic> Create([FromBody] CommentModel commentModel)
        {
            string      accessToken = Request.Headers[HeaderNames.Authorization];
            string      token       = accessToken.Split("Bearer ")[1];
            UserReadDTO userRead    = TokenUtil.GetSubFromToken(token);

            if (ModelState.IsValid)
            {
                CommentWriteDTO commentWrite = new CommentWriteDTO
                {
                    CommentContent = commentModel.CommentContent,
                    PostID         = commentModel.PostID,
                    UserID         = new Guid(userRead.UserID)
                };

                //Khởi Tạo Task Create Comment
                Services.Entities.CustomResponse CreateComment_Task = await _commentService.Create(commentWrite);

                //Nếu Task Done
                if (CreateComment_Task.status)
                {
                    return(new
                    {
                        status = CreateComment_Task.status,
                        code = ReturnCodes.DataCreateSucceeded,
                        message = CreateComment_Task.message,
                        newComment = CreateComment_Task.commentReadDTO
                    });
                }
            }

            return(new
            {
                status = false,
                code = ReturnCodes.ParameterError,
                message = "Dữ Liệu Không Hợp Lệ"
            });
        }
        /*Create Comment A Post*/
        public async Task <CustomResponse> Create(CommentWriteDTO commentWrite)
        {
            CommentReadDTO commentReadDTO;

            if (commentWrite == null || commentWrite.isNullValue())
            {
                return(new CustomResponse(false, "Comment is Null"));
            }

            if (commentWrite.UserID.ToString().Trim().Length == 0)
            {
                return(new CustomResponse(false, "User ID is Empty"));
            }

            if (commentWrite.CommentContent.Trim().Length == 0)
            {
                return(new CustomResponse(false, "Comment Content is Empty"));
            }

            try
            {
                Comment newComment = new Comment
                {
                    CommentID      = Guid.NewGuid(),
                    ContentComment = commentWrite.CommentContent,
                    DateCreate     = DateTime.Now,
                };

                //Check Task's Done When Add a New Comment
                bool AddComment_Task = db.Comments.AddAsync(newComment).IsCompleted;

                //Nếu Done
                if (AddComment_Task)
                {
                    PostComment postComment = new PostComment
                    {
                        CommentID = newComment.CommentID,
                        PostID    = commentWrite.PostID,
                        UserID    = commentWrite.UserID
                    };

                    await db.PostComments.AddAsync(postComment);

                    //Done and Save Change
                    await db.SaveChangesAsync();

                    commentReadDTO                = new CommentReadDTO();
                    commentReadDTO.CommentID      = newComment.CommentID;
                    commentReadDTO.CommentContent = newComment.ContentComment;
                    commentReadDTO.DateCreated    = newComment.DateCreate;
                    commentReadDTO.UserID         = commentWrite.UserID;
                    commentReadDTO.PostID         = commentWrite.PostID;
                    commentReadDTO.UserName       = db.Users.FirstOrDefault(u => u.UserID.Equals(commentWrite.UserID)).UserName;
                }
                //Ngược Lại Có Lỗi Xảy Ra
                else
                {
                    return(new CustomResponse(false, "Có Lỗi Xảy Ra Khi Thêm Comment"));
                }
            }
            catch (Exception e)
            {
                return(new CustomResponse(false, e.Message));
            }

            return(new CustomResponse(commentReadDTO, true, "Thêm Comment Thành Công"));
        }