Skip to content

Commit 64280ea

Browse files
committed
Add SessionStorage to AdvancedSearch demo
1 parent 327da08 commit 64280ea

File tree

3 files changed

+164
-3
lines changed

3 files changed

+164
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"id":"59fc2a73-0e6c-447c-9aae-a33f8fec5a9b","modelId":"nwind","name":"Empolyee of the year","cols":[{"id":"col-6bg4sbcofdzi","cptn":"First name","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.FirstName"}},{"id":"col-q2g4sbcofjcz","cptn":"Last name","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.LastName"}},{"id":"col-erg4sbcofok4","cptn":"Title","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.Title"}},{"id":"col-jcg4sbcofrca","cptn":"Home phone","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.HomePhone"}},{"id":"col-05g4sbcofu6s","cptn":"Unit price Sum","srt":2,"srtidx":0,"expr":{"tag":4,"dtype":8,"kind":0,"func":"SUM","distinct":false,"args":[{"tag":2,"dtype":8,"kind":2,"val":"OrderDetail.UnitPrice"}]}}],"justsortcols":[],"root":{"linking":3,"conds":[{"tag":1,"op":"DateWithinThisYear","exprs":[{"tag":2,"dtype":12,"kind":2,"val":"Order.OrderDate"}]},{"tag":1,"op":"Equal","exprs":[{"tag":2,"dtype":1,"kind":2,"val":"Customer.Country"},{"tag":1,"dtype":1,"kind":0,"val":"USA","txt":"USA"}]}]},"extraData":{},"innerData":{"aggr":{"groups":[],"ugt":false,"urc":false,"csg":false,"aggregates":[],"enabled":false}}}
1+
{"id":"59fc2a73-0e6c-447c-9aae-a33f8fec5a9b","modelId":"nwind","name":"Empolyee of the year","cols":[{"id":"col-6bg4sbcofdzi","cptn":"First name","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.FirstName"}},{"id":"col-q2g4sbcofjcz","cptn":"Last name","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.LastName"}},{"id":"col-erg4sbcofok4","cptn":"Title","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.Title"}},{"id":"col-jcg4sbcofrca","cptn":"Home phone","srt":0,"expr":{"tag":2,"dtype":1,"kind":2,"val":"Employee.HomePhone"}},{"id":"col-05g4sbcofu6s","cptn":"Total sum","srt":2,"srtidx":0,"expr":{"tag":4,"dtype":8,"kind":0,"func":"SUM","distinct":false,"args":[{"tag":2,"dtype":8,"kind":2,"val":"OrderDetail.UnitPrice"}]}}],"justsortcols":[],"root":{"linking":3,"conds":[{"tag":1,"op":"DateWithinThisYear","exprs":[{"tag":2,"dtype":12,"kind":2,"val":"Order.OrderDate"}]},{"tag":1,"op":"Equal","exprs":[{"tag":2,"dtype":1,"kind":2,"val":"Customer.Country"},{"tag":1,"dtype":1,"kind":0,"val":"USA","txt":"USA"}]}]},"extraData":{},"innerData":{"aggr":{"groups":[],"ugt":false,"urc":false,"csg":false,"aggregates":[],"enabled":false}}}

AspNetCore/Razor-Mvc/Razor.AdvancedSearch/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
// EasyQuery static settings
2424
Korzh.EasyQuery.RazorUI.Pages.AdvancedSearch.ExportFormats = new string[] { "pdf", "excel", "excel-html", "csv" };
25-
// Korzh.EasyQuery.RazorUI.Pages.AdvancedSearch.ShowSqlPanel = true; // Uncomment to show SQL panel
25+
// Korzh.EasyQuery.RazorUI.Pages.AdvancedSearch.ShowSqlPanel = true; // Uncomment to show the SQL panel
2626

2727
// Services
2828
builder.Services.AddDbContext<AppDbContext>(options =>
@@ -78,7 +78,16 @@
7878
options.StoreModelInCache = true;
7979
options.StoreQueryInCache = true;
8080

81-
options.UseQueryStore((_) => new FileQueryStore("App_Data"));
81+
//defining different query store depending on configuration
82+
if (string.Compare(configuration.GetValue<string>("QueryStoreMode"), "session", true) == 0) {
83+
options.UseQueryStore(manager => new SessionQueryStore(manager.Services, "App_Data"));
84+
}
85+
else {
86+
options.UseQueryStore(_ => new FileQueryStore(new FileQueryStoreSettings {
87+
DataPath = "App_Data",
88+
FileFormat = "xml"
89+
}));
90+
}
8291

8392
options.UseModelTuner(manager => {
8493
var attr = manager.Model.FindEntityAttr("Order.ShipRegion");
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
using Newtonsoft.Json;
11+
12+
using Korzh.EasyQuery;
13+
using Korzh.EasyQuery.Services;
14+
15+
namespace EqDemo.Services
16+
{
17+
public class SessionQueryStore : IQueryStore
18+
{
19+
private const string _keyPrefixQuery = "query-";
20+
private const string _keyPrefixItem = "items-";
21+
22+
private HttpContext _httpContext;
23+
24+
protected readonly IServiceProvider Services;
25+
26+
private string fileStoreDataPath;
27+
28+
public SessionQueryStore(IServiceProvider services, string initialStoreDataPath = "App_Data")
29+
{
30+
fileStoreDataPath = initialStoreDataPath;
31+
Services = services;
32+
_httpContext = services.GetRequiredService<IHttpContextAccessor>()?.HttpContext
33+
?? throw new ArgumentNullException("IHttpContextAccessor or HttpContext is null.");
34+
}
35+
36+
public virtual async Task<bool> AddQueryAsync(Query query, CancellationToken ct = default)
37+
{
38+
_httpContext.Session.SetString(_keyPrefixQuery + query.Id, await query.SaveToJsonStringAsync());
39+
AddQueryListItem(query.Model.Id, new QueryListItem(query.Id, query.ModelId, query.Name, query.Description));
40+
return true;
41+
}
42+
43+
public Task<IEnumerable<QueryListItem>> GetAllQueriesAsync(string modelId, CancellationToken ct = default)
44+
{
45+
return Task.FromResult(GetQueryListItems(modelId).OrderBy(item => item.name).AsEnumerable());
46+
}
47+
48+
public virtual async Task<bool> LoadQueryAsync(Query query, string queryId, CancellationToken ct = default)
49+
{
50+
var queryJson = _httpContext.Session.GetString(_keyPrefixQuery + queryId);
51+
if (!string.IsNullOrEmpty(queryJson)) {
52+
await query.LoadFromJsonStringAsync(queryJson);
53+
return true;
54+
}
55+
56+
return false;
57+
}
58+
59+
public virtual Task<bool> RemoveQueryAsync(string modelId, string queryId, CancellationToken ct = default)
60+
{
61+
_httpContext.Session.Remove(_keyPrefixQuery + queryId);
62+
RemoveQueryListItem(modelId, queryId);
63+
return Task.FromResult(true);
64+
}
65+
66+
public virtual async Task<bool> SaveQueryAsync(Query query, bool createIfNotExists = true, CancellationToken ct = default)
67+
{
68+
var queryJson = _httpContext.Session.GetString(_keyPrefixQuery + query.Id);
69+
if (!string.IsNullOrEmpty(queryJson)) {
70+
71+
_httpContext.Session.SetString(_keyPrefixQuery + query.Id, await query.SaveToJsonStringAsync());
72+
UpdateQueryListItem(query.Model.Id, new QueryListItem(query.Id, query.ModelId, query.Name, query.Description));
73+
return true;
74+
}
75+
else if (createIfNotExists) {
76+
return await AddQueryAsync(query, ct);
77+
}
78+
79+
return false;
80+
}
81+
82+
private List<QueryListItem> GetQueryListItems(string modelId, CancellationToken ct = default)
83+
{
84+
var json = _httpContext.Session.GetString(_keyPrefixItem + modelId);
85+
if (json == null) {
86+
FileQueryStore initialQueryStore = new FileQueryStore(fileStoreDataPath);
87+
88+
List<QueryListItem> initialQueryList = initialQueryStore.GetAllQueriesAsync(modelId).Result.ToList();
89+
90+
initialQueryList.ForEach(delegate (QueryListItem item) {
91+
_httpContext.Session.SetString(_keyPrefixQuery + item.id, initialQueryStore.GetQueryFileText(modelId, item.id));
92+
});
93+
94+
SaveQueryListItems(modelId, initialQueryList);
95+
96+
return initialQueryList;
97+
}
98+
99+
var queryItemObj = new
100+
{
101+
id = "",
102+
name = "",
103+
desacription = ""
104+
};
105+
106+
List<T> CreateList<T>(T type)
107+
{
108+
return new List<T>();
109+
}
110+
111+
var queryItems = CreateList(queryItemObj);
112+
113+
var result = JsonConvert.DeserializeAnonymousType(json, queryItems);
114+
return result.Select(item => new QueryListItem(item.id, modelId, item.name, item.desacription)).ToList();
115+
}
116+
117+
118+
private void SaveQueryListItems(string modelId, List<QueryListItem> items)
119+
{
120+
_httpContext.Session.SetString(_keyPrefixItem + modelId,
121+
JsonConvert.SerializeObject(items, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
122+
}
123+
124+
private void AddQueryListItem(string modelId, QueryListItem item)
125+
{
126+
var items = GetQueryListItems(modelId);
127+
items.Add(item);
128+
SaveQueryListItems(modelId, items);
129+
}
130+
131+
private void UpdateQueryListItem(string modelId, QueryListItem item)
132+
{
133+
var items = GetQueryListItems(modelId);
134+
foreach (var it in items) {
135+
if (it.id == item.id) {
136+
it.name = item.name;
137+
it.description = item.description;
138+
break;
139+
}
140+
}
141+
SaveQueryListItems(modelId, items);
142+
143+
}
144+
145+
private void RemoveQueryListItem(string modelId, string itemId)
146+
{
147+
var items = GetQueryListItems(modelId);
148+
items.Remove(items.FirstOrDefault(item => item.id == itemId));
149+
SaveQueryListItems(modelId, items);
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)