Example #1
0
        public async Task ClearAllDataAsync(SanityDataContext sanity)
        {
            // Clear existing records in single transaction
            sanity.DocumentSet <Post>().Delete();
            sanity.DocumentSet <Author>().Delete();
            sanity.DocumentSet <Category>().Delete();
            await sanity.CommitAsync();

            await sanity.Images.Delete().CommitAsync();
        }
Example #2
0
        public async Task CRUD_Test()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            // Create new category
            var categories = sanity.DocumentSet <Category>();

            var category1 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest conventions!",
                Title       = "Convention"
            };
            var category2 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Excitement, fun and relaxation...",
                Title       = "Resort"
            };
            var category3 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "World class hotel rooms, restaurants and facilities...",
                Title       = "Hotel"
            };

            // Chained mutations
            categories.Create(category1).Create(category2).Create(category3);


            // Create new author
            var author = new Author
            {
                Id   = Guid.NewGuid().ToString(),
                Name = "Joe Bloggs",
                Slug = new SanitySlug("joe"),
                FavoriteCategories = new List <SanityReference <Category> >()
                {
                    new SanityReference <Category>
                    {
                        Value = category1
                    }
                }
            };

            sanity.DocumentSet <Author>().Create(author);

            // Create post
            var post = new Post
            {
                Title       = "Welcome to Oslofjord Convention Center!",
                PublishedAt = DateTimeOffset.Now,
                Categories  = new List <SanityReference <Category> >
                {
                    new SanityReference <Category>
                    {
                        Ref = category1.CategoryId
                    },
                    new SanityReference <Category>
                    {
                        Ref = category2.CategoryId
                    },
                    new SanityReference <Category>
                    {
                        Ref = category3.CategoryId
                    }
                },
                Author = new SanityReference <Author>
                {
                    Value = author
                },
                Body = new[]
                {
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text  = "A bold start!",
                                Marks = new[] { "strong" }
                            }
                        }
                    },
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                }
            };

            sanity.DocumentSet <Post>().Create(post);

            // Save all changes in one transaction
            var result = await sanity.CommitAsync();


            // Retreive all categories (recursive structure)
            var allCategories = await sanity.DocumentSet <Category>().ToListAsync();

            Assert.True(allCategories.Count >= 3);


            // LINQ Query
            var count = (sanity.DocumentSet <Post>().Count());
            var query = sanity.DocumentSet <Post>()
                        .Include(p => p.Author)
                        .Include(p => p.DereferencedAuthor, "author")
                        .Include(p => p.Author.Value.Images)
                        .Include(p => p.Categories)
                        .Include(p => p.Author.Value.FavoriteCategories)
                        .Where(p => p.PublishedAt >= DateTime.Today);

            // Execute Query
            var results = await query.ToListAsync();

            var transformedResult = sanity.DocumentSet <Post>().Where(p => !p.IsDraft()).Select(p => new { p.Title, p.Author.Value.Name }).ToList();

            Assert.True(count > 0);
            Assert.True(results.Count > 0);
            Assert.NotNull(results[0].Author?.Value);
            Assert.NotNull(results[0].DereferencedAuthor);
            Assert.Equal("Joe Bloggs", results[0].Author.Value.Name);

            // Update test
            var postToUpdate = results[0];

            postToUpdate.Title = "New title";

            await sanity.DocumentSet <Post>().Update(postToUpdate).CommitAsync();

            var updatedItem = await sanity.DocumentSet <Post>().GetAsync(postToUpdate.Id);

            Assert.True(updatedItem.Title == "New title");
        }