Ejemplo n.º 1
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Link image to new author
            var author = new Author()
            {
                Name  = "Joe Bloggs",
                Image = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                }
            };

            await sanity.DocumentSet <Author>().Create(author).CommitAsync();
        }
Ejemplo n.º 2
0
        public async Task SanityLocaleT_GetWithLangugeCode_ShouldReturnAT()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var page = new Page
            {
                Id      = Guid.NewGuid().ToString(),
                Options = new SanityLocale <PageOptions>()
            };

            page.Options.Set("en", new PageOptions {
                ShowOnFrontPage = false, Subtitle = "Awesome page"
            });
            page.Options.Set("no", new PageOptions {
                ShowOnFrontPage = true, Subtitle = "Heftig bra side!"
            });

            // Create page
            await sanity.DocumentSet <Page>().Create(page).CommitAsync();

            // Retrieve newly created page
            page = await sanity.DocumentSet <Page>().GetAsync(page.Id);

            var enOptions = page.Options.Get("en");
            var noOptions = page.Options.Get("no");

            Assert.NotNull(enOptions);
            Assert.NotNull(noOptions);
            Assert.Equal("Awesome page", enOptions.Subtitle);
            Assert.Equal("Heftig bra side!", noOptions.Subtitle);
            Assert.False(enOptions.ShowOnFrontPage);
            Assert.True(noOptions.ShowOnFrontPage);
        }
Ejemplo n.º 3
0
        public async Task SanityLocaleString_GetWithLangugeCode_ShouldReturnAValue()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var page = new Page
            {
                Id    = Guid.NewGuid().ToString(),
                Title = new SanityLocaleString(),
            };

            page.Title.Set("en", "My Page");
            page.Title.Set("no", "Min side");

            // Create page
            await sanity.DocumentSet <Page>().Create(page).CommitAsync();

            // Retrieve newly created page
            page = await sanity.DocumentSet <Page>().GetAsync(page.Id);

            var enTitle = page.Title.Get("en");
            var noTitle = page.Title.Get("no");

            Assert.NotNull(enTitle);
            Assert.NotNull(noTitle);
            Assert.Equal("My Page", enTitle);
            Assert.Equal("Min side", noTitle);
        }
 public ProductsController(
     ILogger <ProductsController> logger,
     SanityDataContext context)
 {
     _logger  = logger;
     _context = context;
 }
Ejemplo n.º 5
0
        public static SanityDataContext UseSanityBlockSerializer(this SanityDataContext sanity, SanityOptions options, IImageService imageSvc)
        {
            var serializer = new SanityBlockSerializer(options, imageSvc);

            serializer.Initialize(sanity);
            return(sanity);
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 7
0
 public async Task ConvertTest()
 {
     // this test tries to serialize the Body-element of a Post-document from the Sanity demo-dataset.
     var sanity = new SanityDataContext(Options);
     var posts = await sanity.DocumentSet<Post>().ToListAsync();
     var htmlBuilder = new SanityHtmlBuilder(Options);
     foreach (var post in posts)
     {
         var html = htmlBuilder.BuildAsync(post.Body); // the serialized data
     }
 }
Ejemplo n.º 8
0
 internal void Initialize(SanityDataContext sanity)
 {
     _sanity = sanity;
     _sanity.AddHtmlSerializer("image", SanityImageUtilities.CreateCachedSerializer(_imgService));
     _sanity.AddHtmlSerializer("column2", SerializeDoubleColumns());
     _sanity.AddHtmlSerializer("column3", SerializeTrippleColumns());
     _sanity.AddHtmlSerializer("video", SerializeVideo());
     _sanity.AddHtmlSerializer("imageGallery", SerializeImageGallery());
     _sanity.AddHtmlSerializer("tableItem", SerializeTable());
     _sanity.AddHtmlSerializer("modal", SerializeModal());
 }
Ejemplo n.º 9
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var options = new SanityOptions
            {
                ProjectId = "oouyzg8g",
                Dataset   = "production",
                Token     = "",
                UseCdn    = false
            };

            var sanity = new SanityDataContext(options);

            services.AddSingleton(sanity);
            services.AddControllers();
        }
Ejemplo n.º 10
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Link image to new author
            var author = new Author()
            {
                Name   = "Joe Bloggs",
                Images = new SanityImage[] {
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    }
                }
            };

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

            var retrievedDoc = await sanity.DocumentSet <Author>().Include(a => a.Images).ToListAsync();

            Assert.True(retrievedDoc.FirstOrDefault()?.Images?.FirstOrDefault()?.Asset != null);
        }
Ejemplo n.º 11
0
 public SanityTeamRepository(SanityDataContext sanity, IMemoryCache cache)
 {
     Sanity = sanity;
     Cache  = cache;
 }
Ejemplo n.º 12
0
 public static Task <string> ToHtmlAsync(this object blockContent, SanityDataContext context)
 {
     return(context.HtmlBuilder.BuildAsync(blockContent));
 }
Ejemplo n.º 13
0
 public static string ToHtml(this object blockContent, SanityDataContext context)
 {
     return(context.HtmlBuilder.BuildAsync(blockContent).Result);
 }
 public static Task <string> ToHtmlAsync(this object blockContent, SanityDataContext context)
 {
     return(blockContent.ToHtmlAsync(context, null));
 }
 public static string ToHtml(this object blockContent, SanityDataContext context)
 {
     return(blockContent.ToHtml(context, null));
 }
Ejemplo n.º 16
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <GzipCompressionProviderOptions>(options => options.Level = System.IO.Compression.CompressionLevel.Optimal);
            services.AddResponseCompression();

            services.AddRazorPages();

            services.AddMvc().AddNewtonsoftJson();

            services.AddCors(o =>
            {
                o.AddDefaultPolicy(b =>
                {
                    string[] headers = { HeaderNames.Authorization, HeaderNames.ContentType, HeaderNames.ContentLength };
                    string[] methods = { HttpMethods.Get, HttpMethods.Post, HttpMethods.Patch, HttpMethods.Put, HttpMethods.Delete };
                    string[] origins = { "http://localhost:8080", "https://buk.gg", "https://dev.buk.gg" };
                    b.WithHeaders(headers);
                    b.WithMethods(methods);
                    b.WithOrigins(origins);
                });
            });

            // Infrastructure
            services.AddMemoryCache();
            services.AddOptions();
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            // Toornament
            services.Configure <ToornamentOptions>(Configuration.GetSection("Toornament"));
            services.AddScoped(s => s.GetService <IOptionsSnapshot <ToornamentOptions> >().Value);
            services.AddScoped(s => new ToornamentClient(s.GetRequiredService <ToornamentOptions>(), ToornamentScopes.Organizer.All));

            // Content
            services.Configure <SanityOptions>(Configuration.GetSection("Sanity"));
            services.AddScoped(s => s.GetService <IOptionsSnapshot <SanityOptions> >().Value);
            services.AddScoped(s =>
            {
                var options = s.GetRequiredService <SanityOptions>();
                var sanity  = new SanityDataContext(options);
                sanity.UseSanityBlockSerializer(options, s.GetRequiredService <IImageService>());
                return(sanity);
            });
            services.AddScoped <IPlayerRepository, SanityPlayerRepository>();
            services.AddScoped <ITournamentRepository, SanityTournamentRepository>();
            services.AddScoped <IEventRepository, SanityEventRepository>();
            services.AddScoped <IOrganizationRepository, SanityOrganizationRepository>();
            services.AddScoped <ITeamRepository, SanityTeamRepository>();
            services.AddScoped <PlayerService>();
            services.AddHttpClient();

            if (Env.EnvironmentName == "Development")
            {
                services.AddScoped <ILocalizationService, SanityLocalizationService>();
            }
            else
            {
                services.AddScoped <ILocalizationService, CachedSanityLocalizationService>();
            }

            // Mapping



            // Session
            services.AddScoped <ISessionProvider, SessionProvider>();
            services.AddScoped <IDiscordProvider, DiscordProvider>();

            ConfigureAuthentication(services);
        }
Ejemplo n.º 17
0
 public SanityPlayerRepository(SanityDataContext sanity)
 {
     Sanity = sanity;
 }
Ejemplo n.º 18
0
        public async Task BlockContent_Extensions_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records in single transaction
            await ClearAllDataAsync(sanity);

            // Uplooad image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            // Create post
            var post = new Post
            {
                MainImage = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                },

                Body = new SanityObject[]
                {
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text  = "A bold start!",
                                Marks = new[] { "strong" }
                            }
                        }
                    },
                    new SanityBlock
                    {
                        Children = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 2,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 3,
                        ListItem   = "bullet",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    },
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 1,
                        ListItem   = "number",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                    ,
                    new SanityBlock
                    {
                        SanityType = "block",
                        Level      = 2,
                        ListItem   = "number",
                        Children   = new []
                        {
                            new SanitySpan
                            {
                                Text = "With a great article..."
                            }
                        }
                    }
                }
            };
            var result = (await sanity.DocumentSet <Post>().Create(post).CommitAsync()).Results[0].Document;

            // Serialize block content
            var html = await result.Body.ToHtmlAsync(sanity);

            // Serialize single object
            var imageTag = await result.MainImage.ToHtmlAsync(sanity);

            //test whole content
            Assert.NotNull(html);
            Assert.True(html.IndexOf("<strong>A bold start!</strong>") != -1);
            Assert.True(html.IndexOf("<img") != -1);
            Assert.True(html.IndexOf(image.AssetId) != -1);

            //test image
            Assert.True(imageTag.IndexOf("<img") != -1);
            Assert.True(imageTag.IndexOf(image.AssetId) != -1);
        }
Ejemplo n.º 19
0
        public async Task Contains_Test()
        {
            var sanity = new SanityDataContext(Options);

            await ClearAllDataAsync(sanity);

            var categories = sanity.DocumentSet <Category>();

            // Create categoriues
            var category1 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest conventions!",
                Tags        = new[] { "One", "Two", "Three" },
                Numbers     = new[] { 1, 2, 3 },
                Title       = "Conventions",
                InternalId  = 1
            };
            var category2 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest events!",
                Tags        = new[] { "Four", "Five", "Six" },
                Numbers     = new[] { 4, 5, 6 },
                Title       = "Events",
                InternalId  = 2
            };
            var category3 = new Category
            {
                CategoryId  = Guid.NewGuid().ToString(),
                Description = "Some of world's greatest festivals!",
                Tags        = new[] { "Seven", "Eight", "Nine" },
                Numbers     = new[] { 7, 8, 9 },
                Title       = "Festivals",
                InternalId  = 3
            };
            await categories
            .Create(category1)
            .Create(category2)
            .Create(category3)
            .CommitAsync();

            // Test 1
            // *[title in ["Conventions", "Festivals"]]
            // .Where(p => ids.Contains(p.Id))

            var namesToFind = new List <string> {
                "Conventions", "Festivals"
            };
            var result1 = await categories.Where(c => namesToFind.Contains(c.Title)).ToListAsync();

            Assert.True(result1.Count == 2);

            var ïdsToFind = new List <int> {
                1, 2
            };
            var result2 = await categories.Where(c => ïdsToFind.Contains(c.InternalId)).ToListAsync();

            Assert.True(result2.Count == 2);


            // Test 2
            // *["Two" in tags]
            // .Where(p => p.Ids.Contains(ids))
            var result3 = await categories.Where(c => c.Tags.Contains("Two")).ToListAsync();

            Assert.True(result3.Count == 1);

            var result4 = await categories.Where(c => c.Numbers.Contains(3)).ToListAsync();

            Assert.True(result4.Count == 1);

            var result5 = await categories.Where(c => c.Numbers.Contains(c.InternalId)).ToListAsync();

            Assert.True(result5.Count == 1);
        }
Ejemplo n.º 20
0
 public CachedSanityLocalizationService(SanityDataContext sanity, SanityOptions options, IMemoryCache cache, IImageService imageSvc) : base(sanity, options, imageSvc)
 {
     _cache = cache;
 }
Ejemplo n.º 21
0
 public SanityLocalizationService(SanityDataContext sanity, SanityOptions options, IImageService imageService)
 {
     _sanity = sanity;
 }
Ejemplo n.º 22
0
 public SanityOrganizationRepository(SanityDataContext sanity, IMemoryCache cache)
 {
     Sanity = sanity;
     Cache  = cache;
 }
Ejemplo n.º 23
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");
        }
Ejemplo n.º 24
0
        public async Task Image_Test()
        {
            var sanity = new SanityDataContext(Options);

            // Clear existing records
            await sanity.DocumentSet <Post>().Delete().CommitAsync();

            await sanity.DocumentSet <Author>().Delete().CommitAsync();

            await sanity.DocumentSet <Category>().Delete().CommitAsync();

            // Delete images
            await sanity.Images.Delete().CommitAsync();

            // Upload new image
            var imageUri = new Uri("https://www.sanity.io/static/images/opengraph/social.png");
            var image    = (await sanity.Images.UploadAsync(imageUri)).Document;

            var category = new Category
            {
                CategoryId  = "AUTHORS",
                Description = "Category for popular authors",
                Title       = "Popular Authors",
                MainImage   = new SanityImage
                {
                    Asset = new SanityReference <SanityImageAsset> {
                        Ref = image.Id
                    },
                }
            };
            await sanity.DocumentSet <Category>().Create(category).CommitAsync();

            // Link image to new author
            var author = new Author()
            {
                Name   = "Joe Bloggs",
                Images = new SanityImage[] {
                    new SanityImage
                    {
                        Asset = new SanityReference <SanityImageAsset> {
                            Ref = image.Id
                        },
                    }
                },
                FavoriteCategories = new List <SanityReference <Category> >
                {
                    new SanityReference <Category>
                    {
                        Value = category
                    }
                }
            };

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

            var retrievedDoc = await sanity.DocumentSet <Author>().ToListAsync();

            Assert.True(retrievedDoc.FirstOrDefault()?.Images?.FirstOrDefault()?.Asset?.Value?.Extension != null);

            Assert.True(retrievedDoc.FirstOrDefault()?.FavoriteCategories?.FirstOrDefault().Value?.MainImage?.Asset?.Value?.Extension != null);
        }