-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
314 lines (260 loc) · 9.19 KB
/
Copy pathProgram.cs
File metadata and controls
314 lines (260 loc) · 9.19 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using LyricalLearning.data;
using LyricalLearning.Models;
using Microsoft.AspNetCore.Identity;
using Email.API.Options;
using email.API;
using Email.API.IMailService;
using email.API.GmailServices;
using Email.API;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddAzureWebAppDiagnostics(); // Ensure this NuGet package is installed
builder.Logging.AddDebug(); // Add Debug logger as well
builder.Logging.SetMinimumLevel(LogLevel.Trace); // Set to Trace to capture everything
// Add Razor Pages (or other services)
builder.Services.AddRazorPages();
// Song Data SQL Connection String
builder.Services.AddDbContext<SongsDbContext>(options =>
options.UseSqlServer(Environment.GetEnvironmentVariable("SQL_DEFAULT_CONNECTION")));
// User Login SQL Connection String
builder.Services.AddDbContext<UsersDbContext>(options =>
options.UseSqlServer(Environment.GetEnvironmentVariable("SQL_USERS_CONNECTION")));
// User Login Conditions
builder.Services.AddIdentity<Users, IdentityRole>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.Password.RequiredLength = 5;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = true;
options.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddEntityFrameworkStores<UsersDbContext>()
.AddDefaultTokenProviders();
// Define how to handle cookies
builder.Services.ConfigureApplicationCookie(options => {
options.LoginPath = "/Login"; // redirect unauthenticated users
options.AccessDeniedPath = "/AccessDenied"; // Redirect from pages that require authentification
options.Cookie.HttpOnly = true; // Ensure the cookies cannot be sent over client side JS
options.ExpireTimeSpan = TimeSpan.FromMinutes(1); // timeout for non-persistent cookies (e.g. account when remember me not clicked)
options.SlidingExpiration = true; // Don't expire while active
});
// Enforce cookie consent for site usage
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
#if DEBUG
options.MinimumSameSitePolicy = SameSiteMode.Lax;
#else
options.MinimumSameSitePolicy = SameSiteMode.None;
#endif
});
builder.Services.Configure<GmailOptions>(builder.Configuration.GetSection(GmailOptions.GmailOptionsKey));
builder.Services.AddScoped<IMailService, GmailService>();
builder.Configuration.AddEnvironmentVariables();
builder.Configuration["GmailOptions:Password"] =
Environment.GetEnvironmentVariable("GMAIL_APP_PASSWORD")
?? throw new InvalidOperationException("GMAIL_APP_PASSWORD not set.");
builder.Services.Configure<GmailOptions>(builder.Configuration.GetSection("GmailOptions"));
var app = builder.Build();
app.MapGet("/", context =>
{
if (context.User.Identity != null && context.User.Identity.IsAuthenticated) {
context.Response.Redirect("/Index"); // if logged in
}
else {
context.Response.Redirect("/Login"); // if not logged in
}
return Task.CompletedTask;
});
// Remember which parts of the song are served with unique session IDs
var usedWords = new Dictionary<Guid, List<int>>();
var usedSentences = new Dictionary<Guid, List<int>>();
var usedParagraphs = new Dictionary<Guid, List<int>>();
// Route for loading song names
app.MapGet("/api/songs", (SongsDbContext db) =>
{
var songs = db.SentenceWords
.GroupBy(sw => new { sw.Song_Id, sw.Song_Name })
.Select(g => new
{
Id = g.Key.Song_Id,
Title = g.Key.Song_Name
})
.OrderBy(s => s.Id)
.ToList();
return Results.Ok(songs);
});
app.MapGet("/api/answers/{mode}/{ids}", (SongsDbContext db, string mode, string ids) =>
{
// Split the ids string into a list of integers
var wordIds = ids
.Split('|', StringSplitOptions.RemoveEmptyEntries)
.Select(id => int.Parse(id))
.ToList();
List<(int Id, string? En)> wordList = new();
if (mode == "words") {
wordList = db.Words
.Where(w => wordIds.Contains(w.Id))
.Select(w => new { w.Id, w.En }).ToList()
.Select(w => (w.Id, w.En)).ToList();
} else if (mode == "sentences" || mode == "paragraph") {
wordList = db.Sentences
.Where(p => wordIds.Contains(p.Id))
.Select(p => new { p.Id, p.En }).ToList()
.Select(p => (p.Id, p.En)).ToList();
} else {
return Results.BadRequest($"Unknown mode: {mode}");
}
// Create an ordered list based on the wordIds to match the original order
var orderedWords = wordIds
.Select(id => wordList.First(w => w.Id == id).En)
.ToList();
// Return the ordered words
return Results.Ok(new { answers = orderedWords });
});
// Route for loading song words
app.MapGet("/api/words/{song_id}", (SongsDbContext db, int song_id) =>
{
var songTitle = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Song_Name)
.FirstOrDefault();
var wordIds = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Word_Id)
.Distinct()
.OrderBy(_ => Guid.NewGuid())
.Take(10)
.ToList();
var wordList = db.Words
.Where(w => wordIds.Contains(w.Id) && w.Og != null)
.OrderBy(_ => Guid.NewGuid())
.ToList();
var guid = Guid.NewGuid();
usedWords[guid] = wordList.Select(w => w.Id).ToList();
return Results.Ok(new
{
id = guid,
title = songTitle,
words = wordList.Select(w => new
{
id = w.Id,
text = w.Og
}).ToList()
});
});
// Route for loading song sentences
app.MapGet("/api/sentences/{song_id}", (SongsDbContext db, int song_id) =>
{
var songTitle = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Song_Name)
.FirstOrDefault();
var sentenceIds = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Sentence_Id)
.Distinct()
.OrderBy(_ => Guid.NewGuid())
.Take(3)
.ToList();
var sentenceList = db.Sentences
.Where(s => sentenceIds.Contains(s.Id) && s.Og != null)
.ToList();
var guid = Guid.NewGuid();
usedSentences[guid] = sentenceList.Select(s => s.Id).ToList();
return Results.Ok(new
{
id = guid,
title = songTitle,
sentences = sentenceList.Select(s => new
{
id = s.Id,
text = s.Og
}).ToList()
});
});
// Route for loading song paragraph
app.MapGet("/api/paragraph/{song_id}", (SongsDbContext db, int song_id) =>
{
var songTitle = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Song_Name)
.FirstOrDefault();
var groupId = db.SentenceWords
.Where(sw => sw.Song_Id == song_id)
.Select(sw => sw.Group_Id)
.Distinct()
.OrderBy(_ => Guid.NewGuid())
.FirstOrDefault();
var sentenceList = db.SentenceWords
.Where(sw => sw.Group_Id == groupId && sw.Song_Id == song_id && sw.Word_Pst == 1)
.OrderBy(sw => sw.Sentence_Pst)
.Select(sw => sw.Sentence_Id)
.ToList();
var paragraphSentences = sentenceList
.Select(id => new {
id = id,
text = db.Sentences
.Where(s => s.Id == id && s.Og != null)
.Select(s => s.Og)
.FirstOrDefault() ?? "[missing]"
})
.ToList();
var guid = Guid.NewGuid();
usedParagraphs[guid] = sentenceList;
return Results.Ok(new
{
id = guid,
title = songTitle,
paragraph = paragraphSentences
});
});
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCookiePolicy();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
// Log that the app has started
var logger = app.Logger;
logger.LogInformation("✅ App started logging.");
// Test DB connection with error handling
try
{
// Retrieve connection string from configuration
string? connectionString = Environment.GetEnvironmentVariable("SQL_DEFAULT_CONNECTION")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
using SqlConnection connection = new(connectionString);
connection.Open();
logger.LogInformation("✅ Database connection successful!");
}
catch (SqlException sqlEx)
{
logger.LogError(sqlEx, "❌ Database connection failed!");
}
catch (Exception ex)
{
logger.LogError(ex, "❌ Unexpected error occurred.");
}
app.Run();
// Song object
public class Song
{
public int Id { get; set; }
public string Title { get; set; } = "";
}