mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 17:29:24 -05:00
Nests showed only the decimal (0.135), while the shop thinks in gauges. A nest now keeps a ThicknessName in its file, record and the server (schema v20), set from Nest Information's new box, which suggests the machine's name or the nearest standard gauge. The Open list, status bar and reports show "10 ga (0.135)"; nests saved without a name get one looked up for display only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using OpenNest.Data;
|
|
|
|
namespace OpenNest.Server;
|
|
|
|
/// <summary>
|
|
/// Reads and checks uploaded nest metadata. Text fields must be present and the status
|
|
/// must be a defined value, so invalid metadata is a 400 and never reaches storage.
|
|
/// </summary>
|
|
internal static class NestMetadata
|
|
{
|
|
// Integer statuses stay accepted for existing callers; GetError refuses undefined ones.
|
|
public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web)
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
|
|
};
|
|
|
|
public static bool TryRead(string? json, out NestRecord record, out string error)
|
|
{
|
|
record = new NestRecord();
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
error = "Missing 'metadata' part.";
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (JsonSerializer.Deserialize<NestRecord>(json, Options) is not { } parsed)
|
|
{
|
|
error = "Missing metadata.";
|
|
return false;
|
|
}
|
|
|
|
record = parsed;
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
error = "Invalid metadata JSON.";
|
|
return false;
|
|
}
|
|
|
|
error = GetError(record) ?? "";
|
|
return error.Length == 0;
|
|
}
|
|
|
|
public static string? GetError(NestRecord record)
|
|
{
|
|
if (record.Name is null) return "name must be a string.";
|
|
if (record.Customer is null) return "customer must be a string.";
|
|
if (record.Material is null) return "material must be a string.";
|
|
if (record.MaterialGrade is null) return "materialGrade must be a string.";
|
|
if (record.ThicknessName is null) return "thicknessName must be a string.";
|
|
if (record.Comments is null) return "comments must be a string.";
|
|
if (record.MadeBy is null) return "madeBy must be a string.";
|
|
if (!Enum.IsDefined(record.Status)) return "status must be draft, quote, toBeCut or hasBeenCut.";
|
|
if (!double.IsFinite(record.Thickness)) return "thickness must be a finite number.";
|
|
return null;
|
|
}
|
|
}
|