diff --git a/api/Database/init.sql b/api/Database/init.sql new file mode 100644 index 0000000..f153f5e --- /dev/null +++ b/api/Database/init.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS LokOpenHours ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + paragraph1 TEXT NOT NULL DEFAULT '', + paragraph2 TEXT NOT NULL DEFAULT '', + paragraph3 TEXT NOT NULL DEFAULT '', + paragraph4 TEXT NOT NULL DEFAULT '', + kitchenNotice TEXT NOT NULL DEFAULT '' +); diff --git a/api/Public/Endpoints/LokEndpoints.cs b/api/Public/Endpoints/LokEndpoints.cs new file mode 100644 index 0000000..5baf549 --- /dev/null +++ b/api/Public/Endpoints/LokEndpoints.cs @@ -0,0 +1,21 @@ +public static class LokEndpoints +{ + public static void MapLokEndpoints(WebApplication app) + { + app.MapGet("/lok/open-hours", async (LokService lokService) => + { + var openHours = await lokService.GetOpenHoursAsync(); + + if (openHours is null) + { + return Results.NotFound(new + { + Message = "Open hours not found." + }); + } + + return Results.Ok(openHours); + }) + .WithName("GetLokOpenHours"); + } +} \ No newline at end of file diff --git a/api/Public/Endpoints/PublicEndpoints.cs b/api/Public/Endpoints/SystemEndpoints.cs similarity index 85% rename from api/Public/Endpoints/PublicEndpoints.cs rename to api/Public/Endpoints/SystemEndpoints.cs index 5a32984..986e629 100644 --- a/api/Public/Endpoints/PublicEndpoints.cs +++ b/api/Public/Endpoints/SystemEndpoints.cs @@ -1,8 +1,8 @@ using Microsoft.Data.Sqlite; -public static class PublicEndpoints +public static class SystemEndpoints { - public static void MapPublicEndpoints(WebApplication app) + public static void MapSystemEndpoints(WebApplication app) { app.MapGet("/", () => { @@ -28,4 +28,4 @@ public static class PublicEndpoints }) .WithName("GetDatabaseHealth"); } -} +} \ No newline at end of file diff --git a/api/Public/Models/LokOpenHours.cs b/api/Public/Models/LokOpenHours.cs new file mode 100644 index 0000000..6ce109a --- /dev/null +++ b/api/Public/Models/LokOpenHours.cs @@ -0,0 +1,12 @@ +public class LokOpenHours +{ + public string Paragraph1 { get; set; } = string.Empty; + + public string Paragraph2 { get; set; } = string.Empty; + + public string Paragraph3 { get; set; } = string.Empty; + + public string Paragraph4 { get; set; } = string.Empty; + + public string KitchenNotice { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/api/Public/Program.cs b/api/Public/Program.cs index 7223a11..9697f19 100644 --- a/api/Public/Program.cs +++ b/api/Public/Program.cs @@ -20,6 +20,7 @@ public class Program var resolvedConnectionString = sqliteConnectionStringBuilder.ToString(); builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString)); + builder.Services.AddScoped(); builder.Services.AddOpenApi(); @@ -28,6 +29,21 @@ public class Program using (var connection = new SqliteConnection(resolvedConnectionString)) { connection.Open(); + + var initScriptPath = Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, "../Database/init.sql")); + + if (File.Exists(initScriptPath)) + { + var initSql = File.ReadAllText(initScriptPath); + if (!string.IsNullOrWhiteSpace(initSql)) + { + using (var command = connection.CreateCommand()) + { + command.CommandText = initSql; + command.ExecuteNonQuery(); + } + } + } } if (app.Environment.IsDevelopment()) @@ -37,7 +53,8 @@ public class Program app.UseHttpsRedirection(); - PublicEndpoints.MapPublicEndpoints(app); + SystemEndpoints.MapSystemEndpoints(app); + LokEndpoints.MapLokEndpoints(app); app.Run(); } diff --git a/api/Public/Services/LokService.cs b/api/Public/Services/LokService.cs new file mode 100644 index 0000000..cfbf152 --- /dev/null +++ b/api/Public/Services/LokService.cs @@ -0,0 +1,42 @@ +using System.Data; +using Microsoft.Data.Sqlite; + +public class LokService +{ + private readonly SqliteConnection _connection; + + public LokService(SqliteConnection connection) + { + _connection = connection; + } + + public async Task GetOpenHoursAsync() + { + if (_connection.State != ConnectionState.Open) + { + await _connection.OpenAsync(); + } + + await using var command = _connection.CreateCommand(); + command.CommandText = @" + SELECT paragraph1, paragraph2, paragraph3, paragraph4, kitchenNotice + FROM LokOpenHours + LIMIT 1"; + + await using var reader = await command.ExecuteReaderAsync(); + + if (!await reader.ReadAsync()) + { + return null; + } + + return new LokOpenHours + { + Paragraph1 = reader["paragraph1"]?.ToString() ?? string.Empty, + Paragraph2 = reader["paragraph2"]?.ToString() ?? string.Empty, + Paragraph3 = reader["paragraph3"]?.ToString() ?? string.Empty, + Paragraph4 = reader["paragraph4"]?.ToString() ?? string.Empty, + KitchenNotice = reader["kitchenNotice"]?.ToString() ?? string.Empty + }; + } +} \ No newline at end of file