|
| 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