Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Samples to showcase tag indexing #439

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/YesSql.Bench/YesSql.Bench.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down
3 changes: 1 addition & 2 deletions samples/YesSql.Samples.FullText/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static async Task Main(string[] args)
}

var configuration = new Configuration()
.UseSqLite($"Data Source={filename};Cache=Shared")
;
.UseSqLite($"Data Source={filename};Cache=Shared");

var store = await StoreFactory.CreateAndInitializeAsync(configuration);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down
9 changes: 9 additions & 0 deletions samples/YesSql.Samples.Hi/Indexes/BlogPostByTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using YesSql.Indexes;

namespace YesSql.Samples.Hi.Indexes
{
public class BlogPostByTag : MapIndex
{
public string Tag { get; set; }
}
}
5 changes: 5 additions & 0 deletions samples/YesSql.Samples.Hi/Indexes/BlogPostIndexProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using YesSql.Indexes;
using YesSql.Samples.Hi.Models;
Expand Down Expand Up @@ -37,6 +38,10 @@ public override void Describe(DescribeContext<BlogPost> context)
return index.Count > 0 ? index : null;
}
);

// for each BlogPost, create BlogPostByTag index
context.For<BlogPostByTag>()
.Map(blogPost => blogPost.Tags.Select(tag => new BlogPostByTag { Tag = tag }));
}
}
}
75 changes: 61 additions & 14 deletions samples/YesSql.Samples.Hi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using YesSql.Provider.SqlServer;
using YesSql.Samples.Hi.Indexes;
Expand All @@ -18,9 +18,7 @@ static async Task MainAsync(string[] args)
{
var store = await StoreFactory.CreateAndInitializeAsync(
new Configuration()
.UseSqlServer(@"Data Source =.; Initial Catalog = yessql; Integrated Security = True")
lnaie marked this conversation as resolved.
Show resolved Hide resolved
.SetTablePrefix("Hi")
);
.UseSqlServer(@"Data Source =.; Initial Catalog = yessql; Integrated Security = True"));

using (var connection = store.Configuration.ConnectionFactory.CreateConnection())
{
Expand All @@ -35,29 +33,50 @@ static async Task MainAsync(string[] args)
.CreateReduceIndexTable<BlogPostByDay>(table => table
.Column<int>("Count")
.Column<int>("Day")
);
)
.CreateMapIndexTable<BlogPostByTag>(table => table
.Column<string>("Tag")
);

transaction.Commit();
}
};
}

// register available indexes
store.RegisterIndexes<BlogPostIndexProvider>();

// creating a blog post
var post = new BlogPost
var post1 = new BlogPost
{
Title = "Hello YesSql",
Author = "Bill",
Content = "Hello",
Content = "Hello Bill!",
PublishedUtc = DateTime.UtcNow,
Tags = new[] { "Hello", "YesSql", "Test" }
};
var post2 = new BlogPost
{
Title = "Bye YesSql",
Author = "Bill",
Content = "Bye Bill!",
PublishedUtc = DateTime.UtcNow,
Tags = new[] { "Hello", "YesSql" }
Tags = new[] { "Bye", "YesSql", "Test" }
};
var post3 = new BlogPost
{
Title = "Other blog title",
Author = "Scott",
Content = "This is also content.",
PublishedUtc = DateTime.UtcNow,
Tags = new[] { "YesSql", "Test", "Blog", "Content" }
};

// saving the post to the database
using (var session = store.CreateSession())
{
session.Save(post);
session.Save(post1);
session.Save(post2);
session.Save(post3);

await session.SaveChangesAsync();
}
Expand All @@ -66,8 +85,9 @@ static async Task MainAsync(string[] args)
using (var session = store.CreateSession())
{
var p = await session.Query().For<BlogPost>().FirstOrDefaultAsync();
Console.WriteLine(p.Title); // > Hello YesSql
Console.WriteLine($"First blog: '{p.Title}'"); // > Hello YesSql
}
Console.WriteLine("");

// loading blog posts by author
using (var session = store.CreateSession())
Expand All @@ -76,9 +96,10 @@ static async Task MainAsync(string[] args)

foreach (var p in ps)
{
Console.WriteLine(p.Author); // > Bill
Console.WriteLine($"'{p.Title}' by {p.Author}"); // > Bill
}
}
Console.WriteLine("");

// loading blog posts by day of publication
using (var session = store.CreateSession())
Expand All @@ -87,9 +108,10 @@ static async Task MainAsync(string[] args)

foreach (var p in ps)
{
Console.WriteLine(p.PublishedUtc); // > [Now]
Console.WriteLine($"'{p.Title}' published on {p.PublishedUtc}"); // > [Now]
}
}
Console.WriteLine("");

// counting blog posts by day
using (var session = store.CreateSession())
Expand All @@ -98,9 +120,34 @@ static async Task MainAsync(string[] args)

foreach (var day in days)
{
Console.WriteLine(day.Day + ": " + day.Count); // > [Today]: 1
Console.WriteLine($"Blogs count on {day.Day}: {day.Count}"); // > [Today]: 1
}
}
Console.WriteLine("");

// load blog posts with tag "Hello"
using (var session = store.CreateSession())
{
var ps = await session.Query<BlogPost, BlogPostByTag>(x => x.Tag == "Hello").ListAsync();

foreach (var p in ps)
{
Console.WriteLine($"'{p.Title}' has 'Hello' tag. Tag list: " + string.Join(", ", p.Tags));
}
}
Console.WriteLine("");

// load blog posts with tag "Test"
using (var session = store.CreateSession())
{
var ps = await session.Query<BlogPost, BlogPostByTag>(x => x.Tag == "Test").ListAsync();

foreach (var p in ps)
{
Console.WriteLine($"'{p.Title}' has 'Test' tag. Tag list: " + string.Join(", ", p.Tags));
}
}
Console.WriteLine("");
}
}
}
3 changes: 1 addition & 2 deletions samples/YesSql.Samples.Hi/YesSql.Samples.Hi.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<DebugType>portable</DebugType>
Expand All @@ -17,7 +17,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\YesSql.Core\YesSql.Core.csproj" />
<ProjectReference Include="..\..\src\YesSql.Provider.SqlServer\YesSql.Provider.SqlServer.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<AssemblyName>YesSql.Samples.Performance</AssemblyName>
Expand Down
15 changes: 14 additions & 1 deletion samples/YesSql.Samples.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using YesSql.Samples.Web.ModelBinding;
using YesSql.Samples.Web.ViewModels;
using YesSql.Samples.Web.Services;
using System.Linq;

namespace YesSql.Samples.Web.Controllers
{
Expand Down Expand Up @@ -61,7 +62,19 @@ public async Task<IActionResult> Index([ModelBinder(BinderType = typeof(QueryFil
{
new SelectListItem("Newest", BlogPostSort.Newest.ToString(), search.SelectedSort == BlogPostSort.Newest),
new SelectListItem("Oldest", BlogPostSort.Oldest.ToString(), search.SelectedSort == BlogPostSort.Oldest)
};
};

var tags = (BlogPostTags[])Enum.GetValues(typeof(BlogPostTags));
search.Tags = new List<SelectListItem>();
foreach(var value in tags)
{
var name = value.ToString();
search.Tags.Add(new SelectListItem(
value == BlogPostTags.Default ? "Select..." : name,
value == BlogPostTags.Default ? "" : name,
search.SelectedTag == value)
);
}

var vm = new BlogPostViewModel
{
Expand Down
15 changes: 14 additions & 1 deletion samples/YesSql.Samples.Web/Indexes/BlogPostIndexProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Linq;
using YesSql.Indexes;
using YesSql.Samples.Web.Models;

Expand All @@ -11,9 +12,15 @@ public class BlogPostIndex : MapIndex
public string Author { get; set; }

public string Content { get; set; }

public DateTime PublishedUtc { get; set; }

public bool Published { get; set; }
}

public class BlogPostByTag : MapIndex
{
public string Tag { get; set; }
}

public class BlogPostIndexProvider : IndexProvider<BlogPost>
Expand All @@ -30,6 +37,12 @@ public override void Describe(DescribeContext<BlogPost> context)
PublishedUtc = blogPost.PublishedUtc,
Published = blogPost.Published
});


// for each BlogPost, create BlogPostByTag index
context
.For<BlogPostByTag>()
.Map(blogPost => blogPost.Tags.Select(tag => new BlogPostByTag { Tag = tag }));
}
}
}
1 change: 1 addition & 0 deletions samples/YesSql.Samples.Web/Models/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class BlogPost
public string Content { get; set; }

public DateTime PublishedUtc { get; set; }

public bool Published { get; set; }

public string[] Tags { get; set; }
Expand Down
48 changes: 44 additions & 4 deletions samples/YesSql.Samples.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -113,6 +113,42 @@ public void ConfigureServices(IServiceCollection services)
})
.AlwaysRun()
)
.WithNamedTerm("tag", b => b
.OneCondition((val, query) =>
{
if (Enum.TryParse<BlogPostTags>(val, true, out var e))
{
switch (e)
{
case BlogPostTags.Default:
break;
default:
query.With<BlogPostByTag>(x => x.Tag == val);
break;
}
}

return query;
})
.MapTo<Filter>((val, model) =>
{
if (Enum.TryParse<BlogPostTags>(val, true, out var e))
{
model.SelectedTag = e;
}
})
.MapFrom<Filter>((model) =>
{
if (model.SelectedTag != BlogPostTags.Default)
{
return (true, model.SelectedTag.ToString());
}

return (false, String.Empty);

})
.AlwaysRun()
)
.WithDefaultTerm("title", b => b
.ManyCondition(
((val, query) => query.With<BlogPostIndex>(x => x.Title.Contains(val))),
Expand Down Expand Up @@ -148,8 +184,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
.Column<string>("Content")
.Column<DateTime>("PublishedUtc")
.Column<bool>("Published")
)
.CreateMapIndexTable<BlogPostByTag>(table => table
.Column<string>("Tag")
);


transaction.Commit();
}
}
Expand All @@ -163,7 +203,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Content = "Steves first post",
PublishedUtc = DateTime.UtcNow,
Published = false,
Tags = Array.Empty<string>()
Tags = new[] { "Steve", "beach", "sand", "first" }
});

session.Save(new BlogPost
Expand All @@ -173,7 +213,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Content = "Bill first post",
PublishedUtc = DateTime.UtcNow,
Published = true,
Tags = Array.Empty<string>()
Tags = new[] { "Bill", "beach", "first", "sand" }
});

session.Save(new BlogPost
Expand All @@ -183,7 +223,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Content = "Pauls first post",
PublishedUtc = DateTime.UtcNow,
Published = true,
Tags = Array.Empty<string>()
Tags = new[] { "Paul", "snow", "first", "mountain", "lake" }
});

session.SaveChangesAsync().GetAwaiter().GetResult();
Expand Down
Loading