public async Task <IActionResult> GetTopicsAsync() { var responseDocument = new JsonApiMultiResourceDocument(); var topics = await this.forumManager.FindAndIncludeTopicInfoAsync(); responseDocument.Data = topics.Select(x => JsonApiTopicInfoResource.Create(x) as IJsonApiResourceIdentifier).ToList(); responseDocument.Links = new JsonApiLinks { Self = "/topics" }; var includeQuery = this.Request.Query.FirstOrDefault(x => x.Key == "include"); if (!(includeQuery.Equals(default(KeyValuePair <string, StringValues>)))) { var userDictionary = new Dictionary <ObjectId, JsonApiUserResource>(); var currentUser = await this.userManager.GetUserAsync(this.User); foreach (var value in includeQuery.Value) { if (value == "owner") { foreach (JsonApiTopicInfoResource resource in responseDocument.Data) { if (resource.Relationships != null && resource.Relationships.Owner != null) { var userId = ObjectId.Parse(resource.Relationships.Owner.Data.Id); if (!(userDictionary.ContainsKey(userId))) { var user = await this.userManager.FindByIdAsync(userId.ToString()); userDictionary[userId] = user.GetJsonApiResourceFor(currentUser) as JsonApiUserResource; } } } } } if (userDictionary.Count > 0) { if (responseDocument.Included is null) { responseDocument.Included = new List <IJsonApiResource>(); } foreach (var value in userDictionary.Values) { responseDocument.Included.Add(value); } } } return(this.Ok(responseDocument)); }
public static JsonApiPostResource Create(ForumPost post) { var resource = new JsonApiPostResource { Attributes = new JsonApiPostAttributes { Body = post.Body, Created = post.Id.CreationTime, Statistics = new JsonApiPostStatisticsAttribute { CommentCount = post.Statistics.CommentCount, DownvoteCount = post.Statistics.DownvoteCount, UpvoteCount = post.Statistics.UpvoteCount }, Title = post.Title, Type = post.Type, Updated = post.DateLastModified?.DateTimeOffset.UtcDateTime, }, Id = post.Id.ToString(), Links = CreateLinks(post.Id), Relationships = new JsonApiPostRelationships { Owner = JsonApiUserResource.CreateRelationship(post.OwnerId), }, }; if (post.ParentId != default) { resource.Relationships.Parent = JsonApiTopicInfoResource.CreateRelationship(post.ParentId); } if (post.Tags != null && post.Tags.Count > 0) { resource.Relationships.Tags = JsonApiTagResource.CreateRelationshipMany(post.Tags); } return(resource); }