Exemplo n.º 1
0
        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));
        }