Exemplo n.º 1
0
        public Document Get(string id)
        {
            /////////////////////////////////////////////////////
            // Get Blog by identifier from repository
            /////////////////////////////////////////////////////
            var blog = BloggingRepository.GetBlog(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(blog)
                               .Relationships()
                               .AddRelationship("articles", new[] { Keywords.Related })
                               .RelationshipsEnd()
                               .Links()
                               .AddSelfLink()
                               .LinksEnd()
                               .ResourceEnd()
                               .WriteDocument();

                return(document);
            }
        }
        public Document GetArticleToComments(string id)
        {
            /////////////////////////////////////////////////////
            // Get Article to related Comments by Article identifier from repository
            /////////////////////////////////////////////////////
            var articleToComments = BloggingRepository.GetArticleToComments(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()
                               .ResourceCollection(articleToComments)
                               .Relationships()
                               .AddRelationship("article", new[] { Keywords.Related })
                               .AddRelationship("author", new[] { Keywords.Related })
                               .RelationshipsEnd()
                               .Links()
                               .AddSelfLink()
                               .LinksEnd()
                               .ResourceCollectionEnd()
                               .WriteDocument();

                return(document);
            }
        }
        public async Task Delete_by_URL()
        {
            try
            {
                // Insert seed data into the database using one instance of the context
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);

                    await repository.AddAsync(new Blog { Url = "http://sample.com/foobar" });

                    await repository.SaveAsync();
                }

                // Use a clean instance of the context to run the test
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);
                    var result     = (await repository.FindByAsync(x => x.Url == "http://sample.com/foobar")).FirstOrDefault();
                    repository.Delete(result);

                    await repository.SaveAsync();

                    Assert.Empty((await repository.FindByAsync(x => x.Url == "http://sample.com/foobar")));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 4
0
        public Document GetCollection()
        {
            /////////////////////////////////////////////////////
            // Get all Blogs from repository
            /////////////////////////////////////////////////////
            var blogs = BloggingRepository.GetBlogs();

            /////////////////////////////////////////////////////
            // 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(blogs)
                               .Relationships()
                               .AddRelationship("articles", new[] { Keywords.Related })
                               .RelationshipsEnd()
                               .Links()
                               .AddSelfLink()
                               .LinksEnd()
                               .ResourceCollectionEnd()
                               .WriteDocument();

                return(document);
            }
        }
        public async Task Find_searches_URL()
        {
            try
            {
                // Insert seed data into the database using one instance of the context
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);

                    await repository.AddAsync(new Blog { Url = "http://sample.com/cats" });

                    await repository.AddAsync(new Blog { Url = "http://sample.com/catfish" });

                    await repository.AddAsync(new Blog { Url = "http://sample.com/dogs" });

                    await repository.SaveAsync();
                }

                // Use a clean instance of the context to run the test
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);
                    var result     = await repository.FindByAsync(b => b.Url.Contains("cat"));

                    Assert.Equal(2, result.Count());
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task Add_writes_to_database()
        {
            try
            {
                // Run the test against one instance of the context
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);

                    await repository.AddAsync(new Blog { Url = "http://sample.com", /*BlogId = 1,*/ Post = null });

                    await repository.SaveAsync();
                }

                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    Assert.Equal(1, await context.Blogs.CountAsync(x => x.Url == "http://sample.com"));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 7
0
        protected ApiControllerBase(IApiServiceContext apiServiceContext, BloggingRepository bloggingRepository)
            : base(apiServiceContext)
        {
            Contract.Requires(apiServiceContext != null);

            this.BloggingRepository = bloggingRepository;
        }
        public async Task Edit()
        {
            try
            {
                // Insert seed data into the database using one instance of the context
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);

                    await repository.AddAsync(new Blog { Url = "http://some.address.com/foo" });

                    await repository.SaveAsync();
                }

                // Use a clean instance of the context to run the test
                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);
                    var result     = (await repository.FindByAsync(x => x.Url == "http://some.address.com/foo")).FirstOrDefault();
                    Assert.NotNull(result);

                    result.Url = "http://domain.com/bar";
                    repository.Edit(result);

                    await repository.SaveAsync();
                }

                using (var context = new BloggingContext(DBFixture.Setup.DbOpts))
                {
                    var repository = new BloggingRepository(context);
                    var result     = await repository.FindByAsync(x => x.Url.StartsWith("http://domain.com"));

                    Assert.Equal("http://domain.com/bar", result.First().Url);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        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);
            }
        }
 public CommentsController(IApiServiceContext apiServiceContext, BloggingRepository bloggingRepository)
     : base(apiServiceContext, bloggingRepository)
 {
 }
Exemplo n.º 12
0
 public ArticlesController(IApiServiceContext apiServiceContext, BloggingRepository bloggingRepository)
     : base(apiServiceContext, bloggingRepository)
 {
 }
 public PeopleController(IApiServiceContext apiServiceContext, BloggingRepository bloggingRepository)
     : base(apiServiceContext, bloggingRepository)
 {
 }