public Document Get(string id) { ///////////////////////////////////////////////////// // Get Article by identifier from repository ///////////////////////////////////////////////////// var article = BloggingRepository.GetArticle(Convert.ToInt64(id)); var articleToBlogIncludedResource = ToOneIncludedResource.Create(article, "blog", BloggingRepository.GetArticleToBlog(article.ArticleId)); var articleToAuthorIncludedResource = ToOneIncludedResource.Create(article, "author", BloggingRepository.GetArticleToAuthor(article.ArticleId)); var comments = BloggingRepository.GetArticleToComments(article.ArticleId); var articleToCommentsIncludedResources = ToManyIncludedResources.Create(article, "comments", BloggingRepository.GetArticleToComments(article.ArticleId)); var commentToAuthorIncludedResourceCollection = comments .Select(x => ToOneIncludedResource.Create(x, "author", BloggingRepository.GetCommentToAuthor(x.CommentId))) .ToList(); ///////////////////////////////////////////////////// // Build JSON API document ///////////////////////////////////////////////////// var currentRequestUri = this.Request.GetUri(); using (var documentContext = new BloggingDocumentContext(currentRequestUri)) { var document = documentContext .NewDocument(currentRequestUri) .SetJsonApiVersion(JsonApiVersion.Version10) .Links() .AddUpLink() .AddSelfLink() .LinksEnd() .Resource(article) .Relationships() .AddRelationship("blog", new[] { Keywords.Related }) .AddRelationship("author", new[] { Keywords.Related }) .AddRelationship("comments", new[] { Keywords.Related }) .RelationshipsEnd() .Links() .AddSelfLink() .LinksEnd() .ResourceEnd() .Included() // article => blog (to-one) .Include(articleToBlogIncludedResource) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => author (to-one) .Include(articleToAuthorIncludedResource) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => comments (to-many) .Include(articleToCommentsIncludedResources) .Relationships() .AddRelationship("author", new[] { Keywords.Related }) .RelationshipsEnd() .Links() .AddLink(Keywords.Self) .LinksEnd() .IncludeEnd() // comment => author (to-one) .Include(commentToAuthorIncludedResourceCollection) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() .IncludedEnd() .WriteDocument(); return(document); } }