Get endpoint for LOK open hours
This commit is contained in:
21
api/Public/Endpoints/LokEndpoints.cs
Normal file
21
api/Public/Endpoints/LokEndpoints.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
12
api/Public/Models/LokOpenHours.cs
Normal file
12
api/Public/Models/LokOpenHours.cs
Normal 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;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ public class Program
|
||||
var resolvedConnectionString = sqliteConnectionStringBuilder.ToString();
|
||||
|
||||
builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString));
|
||||
builder.Services.AddScoped<LokService>();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
42
api/Public/Services/LokService.cs
Normal file
42
api/Public/Services/LokService.cs
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user