示例#1
0
        public async Task <IActionResult> GetTagsAsync()
        {
            var responseDocument = new JsonApiMultiResourceDocument();
            var allTags          = await this.tagManager.Store.QueryableTags.ToListAsync();

            responseDocument.Data = allTags.Select(x => JsonApiTagResource.Create(x) as IJsonApiResourceIdentifier).ToList();
            return(this.Ok(responseDocument));
        }
示例#2
0
        public async Task <IActionResult> GetLinksAsync()
        {
            var responseDocument = new JsonApiMultiResourceDocument();
            var result           = await this.repository.FindAsync(x => true);

            responseDocument.Data = result.Data.Select(x => JsonApiLinkResource.Create(x) as IJsonApiResourceIdentifier).ToList();
            return(this.Ok(responseDocument));
        }
示例#3
0
        public async Task <IActionResult> GetUsersAsync()
        {
            var responseDocument = new JsonApiMultiResourceDocument();
            var allUsers         = await this.userManager.Users.ToListAsync();

            var currentUser = await this.userManager.GetUserAsync(this.User);

            responseDocument.Data = allUsers.Select(x => x.GetJsonApiResourceFor(currentUser) as IJsonApiResourceIdentifier).ToList();
            return(this.Ok(responseDocument));
        }
示例#4
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));
        }
示例#5
0
        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));
        }