mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-10-27 02:41:06 -04:00
Nests, drawings and releases store material and grade as free text, so there was no canonical list for clients to suggest from or to fix spelling against. Schema v10 adds materials and material_grades keyed by SharedListNames.Key, with GET/POST /api/materials. Adding is idempotent by key and keeps the first spelling. The tables start empty: the materials already stored did not import cleanly, so the list is filled through the API instead. Existing free-text columns are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using OpenNest.Data;
|
|
|
|
namespace OpenNest.Server;
|
|
|
|
/// <summary>
|
|
/// <c>/api/materials</c>: the company material list. Adding is idempotent by
|
|
/// <see cref="SharedListNames.Key"/>: 201 when a material or grade was added, 200 when both
|
|
/// were already listed. Either way the reply is the material as listed.
|
|
/// </summary>
|
|
internal static class MaterialRoutes
|
|
{
|
|
private static readonly JsonSerializerOptions AdditionOptions = new(JsonSerializerDefaults.Web)
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
|
|
};
|
|
|
|
public static void MapMaterials(this IEndpointRouteBuilder app)
|
|
{
|
|
app.MapGet("/api/materials", (NestDatabase db) => Results.Ok(db.ListMaterials()));
|
|
|
|
app.MapPost("/api/materials", async (HttpRequest request, NestDatabase db) =>
|
|
{
|
|
var (json, error) = await DrawingSetRoutes.ReadJson(request, "material");
|
|
if (error is not null) return error;
|
|
|
|
MaterialListAddition? addition;
|
|
try { addition = JsonSerializer.Deserialize<MaterialListAddition>(json!, AdditionOptions); }
|
|
catch (JsonException) { return Results.BadRequest("Invalid material JSON."); }
|
|
if (MaterialListRules.GetError(addition) is { } invalid)
|
|
return Results.BadRequest(invalid);
|
|
|
|
var (entry, added) = db.AddMaterial(addition!);
|
|
return added ? Results.Created("/api/materials", entry) : Results.Ok(entry);
|
|
});
|
|
}
|
|
}
|