public Document GetCommentToAuthor(string id) { ///////////////////////////////////////////////////// // Get Comment to related Author by Comment identifier from repository ///////////////////////////////////////////////////// var commentToAuthor = BloggingRepository.GetCommentToAuthor(Convert.ToInt64(id)); ///////////////////////////////////////////////////// // 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(commentToAuthor) .Relationships() .AddRelationship("articles", new[] { Keywords.Related }) .AddRelationship("comments", new[] { Keywords.Related }) .RelationshipsEnd() .Links() .AddSelfLink() .LinksEnd() .ResourceEnd() .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 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); } }