public async Task <IActionResult> GetPostAsync(string id) { var responseDocument = new JsonApiPostDocument(); if (!(ObjectId.TryParse(id, out var postId))) { return(this.NotFound(responseDocument)); } var post = await this.forumManager.FindByIdAsync(postId); if (post is null) { return(this.NotFound(responseDocument)); } responseDocument.Data = JsonApiPostResource.Create(post); foreach (var query in this.Request.Query) { switch (query.Key) { case "include": { var userDictionary = new Dictionary <ObjectId, JsonApiUserResource>(); foreach (var value in query.Value) { switch (value) { case "owner": { var resource = responseDocument.Data; if (!(resource.Relationships is null || resource.Relationships.Owner is 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(user) as JsonApiUserResource; } } } break; } } if (userDictionary.Count > 0) { responseDocument.Included = new List <IJsonApiResource>(); foreach (var value in userDictionary.Values) { responseDocument.Included.Add(value); } } } break; } } responseDocument.Links = JsonApiPostResource.CreateLinks(post.Id); return(this.Ok(responseDocument)); }
public async Task <IActionResult> GetPostsAsync() { var responseDocument = new JsonApiMultiResourceDocument(); var topicIdQuery = this.Request.Query.FirstOrDefault(x => x.Key.ToLowerInvariant() == "topicId".ToLowerInvariant()); if (!(topicIdQuery.Equals(default(KeyValuePair <string, StringValues>)))) { var topicIdString = topicIdQuery.Value.FirstOrDefault(); if (!(ObjectId.TryParse(topicIdString, out var topicId))) { responseDocument.Data = new List <IJsonApiResourceIdentifier>(); responseDocument.Links = new JsonApiLinks { Self = $"/posts?topicId={topicId}" }; return(this.Ok(responseDocument)); } var posts = await this.forumManager.FindAsync(x => x.Id == topicId || x.ParentId == topicId); responseDocument.Data = posts.Select(x => JsonApiPostResource.Create(x) as IJsonApiResourceIdentifier).ToList(); responseDocument.Links = new JsonApiLinks { Self = $"/posts?topicId={topicId}" }; } else { var posts = await this.forumManager.ForumStore.QueryablePosts.ToListAsync(); responseDocument.Data = posts.Select(x => JsonApiPostResource.Create(x) as IJsonApiResourceIdentifier).ToList(); responseDocument.Links = new JsonApiLinks { Self = "/posts" }; } 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 (JsonApiPostResource 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)); }