Further improvements on the open hours endpoints

This commit is contained in:
2026-02-24 21:52:16 +02:00
parent 082eb2575e
commit bc4c849590
11 changed files with 425 additions and 18 deletions

View File

@@ -2,27 +2,74 @@ public static class LokEndpoints
{
public static void MapLokEndpoints(WebApplication app)
{
app.MapPost("/lok/open-hours", async (LokOpenHours openHours, LokService lokService) =>
app.MapPost("/lok/open-hours", async (HttpContext httpContext) =>
{
var lokService = httpContext.RequestServices.GetRequiredService<LokService>();
var openHours = await httpContext.Request.ReadFromJsonAsync<LokOpenHours>();
if (openHours is null)
{
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
await httpContext.Response.WriteAsJsonAsync(new
{
Message = "Request body is required."
});
return;
}
if (string.IsNullOrWhiteSpace(openHours.Name))
{
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
await httpContext.Response.WriteAsJsonAsync(new
{
Message = "Open hours version name is required."
});
return;
}
var createdOpenHours = await lokService.InsertOpenHours(openHours);
return Results.Created("/lok/open-hours", createdOpenHours);
httpContext.Response.StatusCode = StatusCodes.Status201Created;
httpContext.Response.Headers.Location = "/lok/open-hours";
await httpContext.Response.WriteAsJsonAsync(createdOpenHours);
})
.WithName("CreateLokOpenHours");
app.MapGet("/lok/open-hours", async (LokService lokService) =>
app.MapGet("/lok/open-hours", async (HttpContext httpContext) =>
{
var lokService = httpContext.RequestServices.GetRequiredService<LokService>();
var openHours = await lokService.GetOpenHours();
if (openHours.Count == 0)
{
return Results.NotFound(new
httpContext.Response.StatusCode = StatusCodes.Status404NotFound;
await httpContext.Response.WriteAsJsonAsync(new
{
Message = "Open hours not found."
});
return;
}
return Results.Ok(openHours);
await httpContext.Response.WriteAsJsonAsync(openHours);
})
.WithName("GetLokOpenHours");
app.MapDelete("/lok/open-hours/{id:long}", async (HttpContext httpContext, long id) =>
{
var lokService = httpContext.RequestServices.GetRequiredService<LokService>();
var deleted = await lokService.DeleteOpenHours(id);
if (!deleted)
{
httpContext.Response.StatusCode = StatusCodes.Status404NotFound;
await httpContext.Response.WriteAsJsonAsync(new
{
Message = "Open hours version not found."
});
return;
}
httpContext.Response.StatusCode = StatusCodes.Status204NoContent;
})
.WithName("DeleteLokOpenHours");
}
}

View File

@@ -11,18 +11,18 @@ public static class SystemEndpoints
})
.WithName("GetVersion");
app.MapGet("/health/db", async (SqliteConnection connection) =>
app.MapGet("/health/db", async (Microsoft.Data.Sqlite.SqliteConnection connection) =>
{
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT 1";
var result = await command.ExecuteScalarAsync();
return Results.Ok(new
return new
{
Database = "ok",
Result = result
});
};
})
.WithName("GetDatabaseHealth");
}

View File

@@ -1,5 +1,9 @@
public class LokOpenHours
{
public long Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime Version { get; set; }
public string Paragraph1 { get; set; } = string.Empty;

View File

@@ -55,6 +55,15 @@ public class Program
command.CommandText = "ALTER TABLE LokOpenHours ADD COLUMN version TEXT NOT NULL DEFAULT '';";
command.ExecuteNonQuery();
}
command.CommandText = "SELECT COUNT(*) FROM pragma_table_info('LokOpenHours') WHERE name = 'name';";
var hasNameColumn = Convert.ToInt32(command.ExecuteScalar()) > 0;
if (!hasNameColumn)
{
command.CommandText = "ALTER TABLE LokOpenHours ADD COLUMN name TEXT NOT NULL DEFAULT '';";
command.ExecuteNonQuery();
}
}
}

View File

@@ -19,7 +19,7 @@ public class LokService
await using var command = _connection.CreateCommand();
command.CommandText = @"
SELECT version, paragraph1, paragraph2, paragraph3, paragraph4, kitchenNotice
SELECT id, name, version, paragraph1, paragraph2, paragraph3, paragraph4, kitchenNotice
FROM LokOpenHours
ORDER BY datetime(version) DESC, id DESC
LIMIT 5";
@@ -32,6 +32,8 @@ public class LokService
{
openHoursList.Add(new LokOpenHours
{
Id = reader["id"] is long id ? id : Convert.ToInt64(reader["id"]),
Name = reader["name"]?.ToString() ?? string.Empty,
Version = ParseVersion(reader["version"]?.ToString()),
Paragraph1 = reader["paragraph1"]?.ToString() ?? string.Empty,
Paragraph2 = reader["paragraph2"]?.ToString() ?? string.Empty,
@@ -55,9 +57,11 @@ public class LokService
await using var command = _connection.CreateCommand();
command.CommandText = @"
INSERT INTO LokOpenHours (version, paragraph1, paragraph2, paragraph3, paragraph4, kitchenNotice)
VALUES (@version, @paragraph1, @paragraph2, @paragraph3, @paragraph4, @kitchenNotice);";
INSERT INTO LokOpenHours (name, version, paragraph1, paragraph2, paragraph3, paragraph4, kitchenNotice)
VALUES (@name, @version, @paragraph1, @paragraph2, @paragraph3, @paragraph4, @kitchenNotice);
SELECT last_insert_rowid();";
command.Parameters.AddWithValue("@name", openHours.Name ?? string.Empty);
command.Parameters.AddWithValue("@version", version.ToString("O"));
command.Parameters.AddWithValue("@paragraph1", openHours.Paragraph1 ?? string.Empty);
command.Parameters.AddWithValue("@paragraph2", openHours.Paragraph2 ?? string.Empty);
@@ -65,10 +69,12 @@ public class LokService
command.Parameters.AddWithValue("@paragraph4", openHours.Paragraph4 ?? string.Empty);
command.Parameters.AddWithValue("@kitchenNotice", openHours.KitchenNotice ?? string.Empty);
await command.ExecuteNonQueryAsync();
var insertedId = await command.ExecuteScalarAsync();
return new LokOpenHours
{
Id = Convert.ToInt64(insertedId),
Name = openHours.Name ?? string.Empty,
Version = version,
Paragraph1 = openHours.Paragraph1 ?? string.Empty,
Paragraph2 = openHours.Paragraph2 ?? string.Empty,
@@ -78,6 +84,24 @@ public class LokService
};
}
public async Task<bool> DeleteOpenHours(long id)
{
if (_connection.State != ConnectionState.Open)
{
await _connection.OpenAsync();
}
await using var command = _connection.CreateCommand();
command.CommandText = @"
DELETE FROM LokOpenHours
WHERE id = @id;";
command.Parameters.AddWithValue("@id", id);
var affectedRows = await command.ExecuteNonQueryAsync();
return affectedRows > 0;
}
private static DateTime ParseVersion(string? value)
{
if (!string.IsNullOrWhiteSpace(value) && DateTime.TryParse(value, out var parsed))