public Comment(BlogEntry blogEntry, string name, string emailAddress, string commentText) { BlogEntry = blogEntry; Name = name; EmailAddress = emailAddress; CommentText = commentText; DateWritten = DateTime.Now; var commentValidator = new CommentValidator(); commentValidator.ValidateAndThrow(this); }
private Comment CreateComment(Document inDocument) { using var documentContext = this.ApiServiceContext.CreateApiDocumentContext(inDocument); var inComment = documentContext.GetResource <Comment>(); var apiCommentResource = inDocument.GetResource(); if (apiCommentResource?.Relationships != null) { // Author foreign key (optional) if (apiCommentResource.Relationships.TryGetRelationship("author", out var apiCommentAuthorRelationship)) { if (apiCommentAuthorRelationship.IsToOneRelationship()) { var apiCommentAuthorToOneRelationship = (ToOneRelationship)apiCommentAuthorRelationship; var apiCommentAuthorId = Convert.ToInt64(apiCommentAuthorToOneRelationship.Data.Id); inComment.AuthorId = apiCommentAuthorId; } } // Article foreign key (required) if (apiCommentResource.Relationships.TryGetRelationship("article", out var apiCommentArticleRelationship)) { if (apiCommentArticleRelationship.IsToOneRelationship()) { var apiCommentArticleToOneRelationship = (ToOneRelationship)apiCommentArticleRelationship; var apiCommentArticleId = Convert.ToInt64(apiCommentArticleToOneRelationship.Data.Id); inComment.ArticleId = apiCommentArticleId; } } } var validator = new CommentValidator(); validator.ValidateAndThrow(inComment); var outComment = this.BloggingRepository.CreateComment(inComment); return(outComment); }