|
1 | | -using Microsoft.AspNetCore.Hosting; |
2 | | -using Microsoft.Extensions.Configuration; |
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
3 | 4 | using Microsoft.Extensions.Hosting; |
4 | | -using Microsoft.Extensions.Logging; |
5 | | -using System; |
6 | | -using System.Collections.Generic; |
7 | | -using System.Linq; |
8 | | -using System.Threading.Tasks; |
9 | | - |
10 | | -namespace EqDemo |
11 | | -{ |
12 | | - public class Program |
13 | | - { |
14 | | - public static void Main(string[] args) |
15 | | - { |
16 | | - CreateHostBuilder(args).Build().Run(); |
17 | | - } |
18 | | - |
19 | | - public static IHostBuilder CreateHostBuilder(string[] args) => |
20 | | - Host.CreateDefaultBuilder(args) |
21 | | - .ConfigureWebHostDefaults(webBuilder => |
22 | | - { |
23 | | - webBuilder.UseStartup<Startup>(); |
24 | | - }); |
25 | | - } |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using EasyData.Export; |
| 7 | +using Korzh.EasyQuery.Services; |
| 8 | +using Korzh.EasyQuery.Db; |
| 9 | + |
| 10 | +using EqDemo; |
| 11 | +using EqDemo.Services; |
| 12 | +using EasyData; |
| 13 | + |
| 14 | +var builder = WebApplication.CreateBuilder(args); |
| 15 | + |
| 16 | +// Only load env vars that begin with ADMIN_ |
| 17 | +builder.Configuration.AddEnvironmentVariables(prefix: "AdvancedSearch"); |
| 18 | + |
| 19 | +// Configuration |
| 20 | +var configuration = builder.Configuration; |
| 21 | +var dbConnectionString = configuration.GetConnectionString("EqDemoDbLite"); |
| 22 | + |
| 23 | +// EasyQuery static settings |
| 24 | +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 |
| 26 | + |
| 27 | +// Services |
| 28 | +builder.Services.AddDbContext<AppDbContext>(options => |
| 29 | + options.UseSqlite(dbConnectionString) |
| 30 | + // options.UseSqlServer(dbConnectionString); |
| 31 | +); |
| 32 | + |
| 33 | +builder.Services.AddDistributedMemoryCache(); |
| 34 | +builder.Services.AddSession(); |
| 35 | + |
| 36 | +builder.Services.AddEasyQuery() |
| 37 | + .UseSqlManager() |
| 38 | + .AddDefaultExporters() |
| 39 | + .AddDataExporter<PdfDataExporter>("pdf") |
| 40 | + .AddDataExporter<ExcelDataExporter>("excel") |
| 41 | + .UseSessionCache() |
| 42 | + .RegisterDbGate<Korzh.EasyQuery.DbGates.SqLiteGate>(); |
| 43 | + // .RegisterDbGate<Korzh.EasyQuery.DbGates.SqlServerGate>(); |
| 44 | + |
| 45 | +builder.Services.AddRazorPages(); |
| 46 | + |
| 47 | +// To support non-Unicode code pages in PDF Exporter |
| 48 | +System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); |
| 49 | + |
| 50 | +var app = builder.Build(); |
| 51 | + |
| 52 | +// Pipeline |
| 53 | +if (app.Environment.IsDevelopment()) { |
| 54 | + app.UseDeveloperExceptionPage(); |
| 55 | + app.UseMigrationsEndPoint(); |
26 | 56 | } |
| 57 | +else { |
| 58 | + app.UseExceptionHandler("/Error"); |
| 59 | + app.UseHsts(); |
| 60 | +} |
| 61 | + |
| 62 | +app.UseHttpsRedirection(); |
| 63 | +app.UseStaticFiles(); |
| 64 | + |
| 65 | +app.UseRouting(); |
| 66 | + |
| 67 | +app.UseAuthentication(); |
| 68 | +app.UseAuthorization(); |
| 69 | + |
| 70 | +app.UseSession(); |
| 71 | + |
| 72 | +app.MapEasyQuery(options => { |
| 73 | + options.DefaultModelId = "nwind"; |
| 74 | + options.BuildQueryOnSync = true; |
| 75 | + options.SaveNewQuery = false; |
| 76 | + options.ConnectionString = dbConnectionString; |
| 77 | + options.UseDbContext<AppDbContext>(); |
| 78 | + options.StoreModelInCache = true; |
| 79 | + options.StoreQueryInCache = true; |
| 80 | + |
| 81 | + options.UseQueryStore((_) => new FileQueryStore("App_Data")); |
| 82 | + |
| 83 | + options.UseModelTuner(manager => { |
| 84 | + var attr = manager.Model.FindEntityAttr("Order.ShipRegion"); |
| 85 | + attr.Operations.RemoveByIDs(manager.Model, "StartsWith,Contains"); |
| 86 | + attr.DefaultEditor = new CustomListValueEditor("Lookup", "Lookup"); |
| 87 | + |
| 88 | + var catNameAttr = manager.Model.FindEntityAttr("Category.CategoryName"); |
| 89 | + var catIdAttr = manager.Model.FindEntityAttr("Product.Category"); |
| 90 | + catIdAttr.Entity.Attributes.Add(catNameAttr); |
| 91 | + catNameAttr.UseInConditions = false; |
| 92 | + catIdAttr.UseInResult = false; |
| 93 | + catIdAttr.LookupAttr = catNameAttr; |
| 94 | + }); |
| 95 | + |
| 96 | + options.UseSqlFormats(FormatType.Sqlite, formats => { |
| 97 | + formats.UseDbName = false; |
| 98 | + formats.UseSchema = false; |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +app.MapRazorPages(); |
| 103 | +app.MapControllers(); |
| 104 | + |
| 105 | +// Init demo database (if necessary) |
| 106 | +app.EnsureDbInitialized(dbConnectionString, app.Environment); |
| 107 | + |
| 108 | +app.Run(); |
0 commit comments