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); } }
public Document GetCollection() { ///////////////////////////////////////////////////// // Get all Articles from repository ///////////////////////////////////////////////////// var articles = BloggingRepository.GetArticles().SafeToList(); var articleToBlogIncludedResourceCollection = articles .Select(x => ToOneIncludedResource.Create(x, "blog", BloggingRepository.GetArticleToBlog(x.ArticleId))) .ToList(); var articleToAuthorIncludedResourceCollection = articles .Select(x => ToOneIncludedResource.Create(x, "author", BloggingRepository.GetArticleToAuthor(x.ArticleId))) .ToList(); var articleToCommentsIncludedResourcesCollection = articles .Select(x => ToManyIncludedResources.Create(x, "comments", BloggingRepository.GetArticleToComments(x.ArticleId))) .ToList(); // Get all distinct comments used in all the articles. var comments = articles .SelectMany(x => BloggingRepository.GetArticleToComments(x.ArticleId)) .GroupBy(x => x.CommentId) .Select(x => x.First()) .ToList(); 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() .ResourceCollection(articles) .Relationships() .AddRelationship("blog", new[] { Keywords.Related }) .AddRelationship("author", new[] { Keywords.Related }) .AddRelationship("comments", new[] { Keywords.Related }) .RelationshipsEnd() .Links() .AddSelfLink() .LinksEnd() .ResourceCollectionEnd() .Included() // article => blog (to-one) .Include(articleToBlogIncludedResourceCollection) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => author (to-one) .Include(articleToAuthorIncludedResourceCollection) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => comments (to-many) .Include(articleToCommentsIncludedResourcesCollection) .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); } }
public IActionResult GetBlogToArticles(string id) { ///////////////////////////////////////////////////// // Get Blog to related Articles by Blog identifier from repository ///////////////////////////////////////////////////// var blogToArticles = this.BloggingRepository.GetBlogToArticles(Convert.ToInt64(id)).ToList(); var articleToBlogIncludedResourceCollection = blogToArticles.Select(x => ToOneIncludedResource.Create(x, "blog", this.BloggingRepository.GetArticleToBlog(x.ArticleId))) .ToList(); var articleToAuthorIncludedResourceCollection = blogToArticles.Select(x => ToOneIncludedResource.Create(x, "author", this.BloggingRepository.GetArticleToAuthor(x.ArticleId))) .ToList(); var articleToCommentsIncludedResourcesCollection = blogToArticles.Select(x => ToManyIncludedResources.Create(x, "comments", this.BloggingRepository.GetArticleToComments(x.ArticleId))) .ToList(); // Get all distinct comments used in all the articles. var comments = blogToArticles.SelectMany(x => this.BloggingRepository.GetArticleToComments(x.ArticleId)) .GroupBy(x => x.CommentId) .Select(x => x.First()) .ToList(); var commentToAuthorIncludedResourceCollection = comments.Select(x => ToOneIncludedResource.Create(x, "author", this.BloggingRepository.GetCommentToAuthor(x.CommentId))) .ToList(); ///////////////////////////////////////////////////// // Build JSON API document ///////////////////////////////////////////////////// var currentRequestUri = this.GetCurrentRequestUri(); using var documentContext = this.ApiServiceContext.CreateApiDocumentContext(); var document = documentContext .NewDocument(currentRequestUri) .SetJsonApiVersion(JsonApiVersion.Version10) .Links() .AddUpLink() .AddSelfLink() .LinksEnd() .ResourceCollection(blogToArticles) .Relationships() .AddRelationship("blog", new[] { Keywords.Related }) .AddRelationship("comments", new[] { Keywords.Related }) .RelationshipsEnd() .Links() .AddSelfLink() .LinksEnd() .ResourceCollectionEnd() .Included() // article => blog (to-one) .Include(articleToBlogIncludedResourceCollection) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => author (to-one) .Include(articleToAuthorIncludedResourceCollection) .Links() .AddSelfLink() .LinksEnd() .IncludeEnd() // article => comments (to-many) .Include(articleToCommentsIncludedResourcesCollection) .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(this.Ok(document)); }