Get endpoint for LOK open hours

This commit is contained in:
2026-02-17 21:38:52 +02:00
parent 5e0ed10976
commit 13ca568244
6 changed files with 104 additions and 4 deletions

8
api/Database/init.sql Normal file
View File

@@ -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 ''
);

View File

@@ -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");
}
}

View File

@@ -1,8 +1,8 @@
using Microsoft.Data.Sqlite; 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("/", () => app.MapGet("/", () =>
{ {
@@ -28,4 +28,4 @@ public static class PublicEndpoints
}) })
.WithName("GetDatabaseHealth"); .WithName("GetDatabaseHealth");
} }
} }

View File

@@ -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;
}

View File

@@ -20,6 +20,7 @@ public class Program
var resolvedConnectionString = sqliteConnectionStringBuilder.ToString(); var resolvedConnectionString = sqliteConnectionStringBuilder.ToString();
builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString)); builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString));
builder.Services.AddScoped<LokService>();
builder.Services.AddOpenApi(); builder.Services.AddOpenApi();
@@ -28,6 +29,21 @@ public class Program
using (var connection = new SqliteConnection(resolvedConnectionString)) using (var connection = new SqliteConnection(resolvedConnectionString))
{ {
connection.Open(); 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()) if (app.Environment.IsDevelopment())
@@ -37,7 +53,8 @@ public class Program
app.UseHttpsRedirection(); app.UseHttpsRedirection();
PublicEndpoints.MapPublicEndpoints(app); SystemEndpoints.MapSystemEndpoints(app);
LokEndpoints.MapLokEndpoints(app);
app.Run(); app.Run();
} }

View File

@@ -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<LokOpenHours?> 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
};
}
}