Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (106 loc) · 4.49 KB
/
Copy pathProgram.cs
File metadata and controls
121 lines (106 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Markdig;
using Microsoft.Data.Sqlite;
using EvolveDb;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using DesenvolvedorNET.Repositories;
using DesenvolvedorNET.Db;
using Microsoft.EntityFrameworkCore;
using DesenvolvedorNET.Models;
using Microsoft.AspNetCore.Identity;
namespace DesenvolvedorNET
{
public class Program
{
public static void Main(string[] args)
{
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
var outputPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory());
if (!System.IO.Directory.Exists(outputPath))
{
System.IO.Directory.CreateDirectory(outputPath);
}
var cnx = new SqliteConnection($@"Data Source={System.IO.Path.Combine(outputPath, "DesenvolvedorNET.db")}");
var evolve = new Evolve(cnx)
{
Locations = new[] { "Db" },
IsEraseDisabled = true,
};
evolve.Migrate();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMvc(opttions =>
{
opttions.EnableEndpointRouting = false;
});
//configure the connection string to database
builder.Services.AddDbContext<EmpregadosContext>(options =>
{
options.UseSqlite($@"Data Source={System.IO.Path.Combine(outputPath, "DesenvolvedorNET.db")}");
});
//configure the connection string to database estoque
builder.Services.AddDbContext<EstoqueDbContext>(options =>
{
options.UseSqlite($@"Data Source={System.IO.Path.Combine(outputPath, "Estoque.db")}");
});
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<EstoqueDbContext>();
builder.Services.ConfigureApplicationCookie(options =>
{
options.Events.OnRedirectToLogin = context =>
{
var returnUrl = context.Request.Path.Value;
if (context.Request.Path.StartsWithSegments("/estoque"))
{
context.RedirectUri = $"/estoque/usuario/login";
}
else
{
context.RedirectUri = $"/Account/Login";
}
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
};
});
var app = builder.Build();
//migrate dbcontext
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<EmpregadosContext>();
db.Database.Migrate();
db.SaveChanges();
var dbEstoque = scope.ServiceProvider.GetRequiredService<EstoqueDbContext>();
dbEstoque.Database.Migrate();
dbEstoque.SaveChanges();
}
IHostEnvironment env = app.Services.GetService<IHostEnvironment>();
if (env.IsDevelopment())
{
//create a DeveloperExceptionPageOptions with default values
DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions();
developerExceptionPageOptions.SourceCodeLineCount = 6;
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
//create a StaticFileoptions with default values
StaticFileOptions staticFileOptions = new StaticFileOptions();
staticFileOptions.DefaultContentType = "None";
staticFileOptions.ServeUnknownFileTypes = false;
app.UseStaticFiles(staticFileOptions);
app.MapAreaControllerRoute(
name: "Estoque",
areaName: "Estqoue",
pattern: "Estoque/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
}
}