-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
250 lines (211 loc) · 8.18 KB
/
Copy pathProgram.cs
File metadata and controls
250 lines (211 loc) · 8.18 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using BibeCatalogue.Services;
using FluentMigrator.Runner;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
// Configurazione sessioni
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.Name = "BibeCatalogue.Session";
});
// HttpContextAccessor per le sessioni
builder.Services.AddHttpContextAccessor();
// Registrazione servizi personalizzati
builder.Services.AddScoped<DatabaseConnectionService>();
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<CourseService>();
builder.Services.AddScoped<SessionService>();
builder.Services.AddScoped<DatabaseMigrationService>();
// Configurazione FluentMigrator
builder.Services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer()
.WithGlobalConnectionString(builder.Configuration.GetConnectionString("DefaultConnection"))
.ScanIn(Assembly.GetExecutingAssembly()).For.Migrations())
.AddLogging(lb => lb.AddFluentMigratorConsole());
// Configurazione logging
builder.Services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
});
var app = builder.Build();
// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
app.UseHttpsRedirection();
}
// Non usare HTTPS redirection in Development quando si usa Docker
app.UseStaticFiles();
// Abilitazione sessioni
app.UseSession();
// Disabilita antiforgery per l'applicazione demo
// app.UseAntiforgery();
// Health check endpoint
app.MapGet("/health", () => Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow }));
// Home redirect to login
app.MapGet("/", () => Results.Redirect("/login"));
// Login page
app.MapGet("/login", async (HttpContext context) =>
{
var loginTemplate = await File.ReadAllTextAsync("Templates/login.html");
var errorMessage = "";
var emailValue = "";
if (context.Request.Query.ContainsKey("error"))
{
errorMessage = "<div class='alert alert-danger'><i class='bi bi-exclamation-triangle'></i> Credenziali non valide. Riprova.</div>";
}
if (context.Request.Query.ContainsKey("email"))
{
emailValue = context.Request.Query["email"].ToString();
}
var html = loginTemplate
.Replace("{{ERROR_MESSAGE}}", errorMessage)
.Replace("{{EMAIL_VALUE}}", emailValue);
return Results.Content(html, "text/html");
});
// Login endpoint
app.MapPost("/login", async (HttpContext context, UserService userService, SessionService sessionService) =>
{
var form = await context.Request.ReadFormAsync();
var email = form["email"].ToString();
var password = form["password"].ToString();
var user = await userService.AuthenticateAsync(email, password);
if (user != null)
{
sessionService.SetUser(user);
return Results.Redirect("/courses");
}
return Results.Redirect($"/login?error=invalid&email={Uri.EscapeDataString(email)}");
});
// Courses page
app.MapGet("/courses", async (HttpContext context, SessionService sessionService, CourseService courseService) =>
{
var currentUser = sessionService.GetUser();
if (currentUser == null)
{
return Results.Redirect("/login");
}
// Get search parameter
var searchTerm = context.Request.Query["search"].ToString();
var isSearching = !string.IsNullOrWhiteSpace(searchTerm);
// Get courses (filtered if search term provided)
var courses = await courseService.GetUserCoursesAsync(currentUser.Id, searchTerm);
var allCourses = isSearching ? await courseService.GetUserCoursesAsync(currentUser.Id) : courses;
var coursesTemplate = await File.ReadAllTextAsync("Templates/courses.html");
// Build courses HTML
var coursesHtml = "";
if (courses.Any())
{
coursesHtml = "<div class='row'>";
foreach (var course in courses)
{
var resultClass = course.Result >= 18 ? "success" : "danger";
var resultIcon = course.Result >= 18 ? "check-circle" : "x-circle";
// Highlight search term in title if searching
var displayTitle = course.Title;
if (isSearching && !string.IsNullOrWhiteSpace(searchTerm))
{
displayTitle = course.Title.Replace(searchTerm, $"<mark>{searchTerm}</mark>", StringComparison.OrdinalIgnoreCase);
}
coursesHtml += $@"
<div class='col-md-6 mb-3'>
<div class='card'>
<div class='card-body'>
<h5 class='card-title'>{displayTitle}</h5>
<p class='card-text'>
<small class='text-muted'>
<i class='bi bi-calendar'></i> {course.StartDate:dd/MM/yyyy} - {course.EndDate:dd/MM/yyyy}
</small>
</p>
<div class='d-flex justify-content-between align-items-center'>
<span class='badge bg-{resultClass}'>
<i class='bi bi-{resultIcon}'></i> Voto: {course.Result}/30
</span>
<small class='text-muted'>{course.Result} punti</small>
</div>
</div>
</div>
</div>";
}
coursesHtml += "</div>";
}
else
{
if (isSearching)
{
coursesHtml = $@"
<div class='alert alert-warning'>
<i class='bi bi-search'></i> Nessun corso trovato per '<strong>{searchTerm}</strong>'.
<br><small>Prova con un termine di ricerca diverso.</small>
</div>";
}
else
{
coursesHtml = "<div class='alert alert-info'><i class='bi bi-info-circle'></i> Nessun corso trovato.</div>";
}
}
// Build search info
var searchInfo = "";
if (isSearching)
{
var totalCourses = allCourses.Count();
var foundCourses = courses.Count();
searchInfo = $@"
<div class='alert alert-light border'>
<i class='bi bi-info-circle'></i>
Trovati <strong>{foundCourses}</strong> corsi su <strong>{totalCourses}</strong> per '<strong>{searchTerm}</strong>'
</div>";
}
// Build clear button
var clearButton = "";
if (isSearching)
{
clearButton = @"
<a href='/courses' class='btn btn-outline-secondary'>
<i class='bi bi-x-circle'></i> Cancella
</a>";
}
// Replace template placeholders
var html = coursesTemplate
.Replace("{{USER_NAME}}", $"{currentUser.FirstName} {currentUser.LastName}")
.Replace("{{COURSES_CONTENT}}", coursesHtml)
.Replace("{{SEARCH_VALUE}}", searchTerm ?? "")
.Replace("{{SEARCH_INFO}}", searchInfo)
.Replace("{{CLEAR_BUTTON}}", clearButton);
return Results.Content(html, "text/html");
});
// Logout endpoint
app.MapGet("/logout", (HttpContext context, SessionService sessionService) =>
{
sessionService.ClearUser();
return Results.Redirect("/login");
});
// Keep test page for debugging
app.MapGet("/test", () => Results.File("wwwroot/test.html", "text/html"));
// HTML endpoints only - no Blazor components needed
// Esecuzione migrazioni database all'avvio
try
{
using (var scope = app.Services.CreateScope())
{
var migrationService = scope.ServiceProvider.GetRequiredService<DatabaseMigrationService>();
await migrationService.MigrateAsync();
}
}
catch (Exception ex)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Error running database migrations during startup");
// In un ambiente di produzione, potresti voler fermare l'applicazione se le migrazioni falliscono
// throw;
}
app.Run();