Rename Public -> App
This commit is contained in:
14
api/App/App.csproj
Normal file
14
api/App/App.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
21
api/App/Endpoints/LokEndpoints.cs
Normal file
21
api/App/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");
|
||||
}
|
||||
}
|
||||
31
api/App/Endpoints/SystemEndpoints.cs
Normal file
31
api/App/Endpoints/SystemEndpoints.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
public static class SystemEndpoints
|
||||
{
|
||||
public static void MapSystemEndpoints(WebApplication app)
|
||||
{
|
||||
app.MapGet("/", () =>
|
||||
{
|
||||
return new
|
||||
{
|
||||
Version = "1.0.0"
|
||||
};
|
||||
})
|
||||
.WithName("GetVersion");
|
||||
|
||||
app.MapGet("/health/db", async (SqliteConnection connection) =>
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT 1";
|
||||
var result = await command.ExecuteScalarAsync();
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
Database = "ok",
|
||||
Result = result
|
||||
});
|
||||
})
|
||||
.WithName("GetDatabaseHealth");
|
||||
}
|
||||
}
|
||||
12
api/App/Models/LokOpenHours.cs
Normal file
12
api/App/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;
|
||||
}
|
||||
61
api/App/Program.cs
Normal file
61
api/App/Program.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var configuredConnectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
||||
?? throw new InvalidOperationException("Connection string 'DefaultConnection' was not found.");
|
||||
|
||||
var sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder(configuredConnectionString);
|
||||
var databasePath = Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, sqliteConnectionStringBuilder.DataSource));
|
||||
var databaseDirectory = Path.GetDirectoryName(databasePath)
|
||||
?? throw new InvalidOperationException("Could not determine database directory.");
|
||||
|
||||
Directory.CreateDirectory(databaseDirectory);
|
||||
|
||||
sqliteConnectionStringBuilder.DataSource = databasePath;
|
||||
var resolvedConnectionString = sqliteConnectionStringBuilder.ToString();
|
||||
|
||||
builder.Services.AddScoped(_ => new SqliteConnection(resolvedConnectionString));
|
||||
builder.Services.AddScoped<LokService>();
|
||||
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
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())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
SystemEndpoints.MapSystemEndpoints(app);
|
||||
LokEndpoints.MapLokEndpoints(app);
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
23
api/App/Properties/launchSettings.json
Normal file
23
api/App/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5013",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7064;http://localhost:5013",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
api/App/Services/LokService.cs
Normal file
42
api/App/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
|
||||
};
|
||||
}
|
||||
}
|
||||
11
api/App/appsettings.Development.json
Normal file
11
api/App/appsettings.Development.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
12
api/App/appsettings.json
Normal file
12
api/App/appsettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=../Database/klapi.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user