/*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")); }
public CustomResponse(CommentReadDTO commentReadDTO, bool status, string message) { this.commentReadDTO = commentReadDTO; this.status = status; this.message = message; }