Files
OpenNest/OpenNest.Data/DrawingLibraryEditSession.cs
T
ajandClaude Opus 5.5 5a374aa3f4 feat(library): content-addressed packages and editable nesting settings
Schema v5 stores each distinct drawing package once in drawing_packages,
keyed by SHA-256; revisions point at it, so details, settings, archive and
restore revisions add no blob. The v4 drawing tables are replaced outright
(no production library existed); nest tables are untouched.

Nesting settings (cut first, priority, rotation constraints, color) become
structured record fields (DrawingLibraryRecord.Settings), returned on every
read, filterable by cutFirst, editable through the metadata endpoint and
applied by LibraryDrawingInsert. Canonical packages now leave every
record-owned field at its default, so renaming or changing settings never
changes the package hash. Saving a nest drawing to the library carries its
settings into the record.

The desktop library browser gains a Cut first column and an Edit Settings
dialog that saves notes and settings as a new revision with the same package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 22:47:18 -04:00

68 lines
3.2 KiB
C#

namespace OpenNest.Data;
/// <summary>
/// Edits one listed library drawing's notes and nesting settings in place: the save is a
/// metadata-only revision guarded by the revision it was listed with, so the package (and
/// its hash) stays the same. A stale revision raises <see cref="DrawingLibraryConflictException"/>
/// and changes nothing.
/// </summary>
public sealed class DrawingLibraryEditSession
{
private readonly IDrawingLibraryStore _library;
public DrawingLibraryEditSession(IDrawingLibraryStore library, DrawingLibraryRecord listed, string savedBy)
{
_library = library ?? throw new ArgumentNullException(nameof(library));
Listed = listed ?? throw new ArgumentNullException(nameof(listed));
if (listed.Id == Guid.Empty || listed.Revision < 1)
throw new ArgumentException("Choose a listed library drawing to edit.", nameof(listed));
if (listed.Archived)
throw new ArgumentException("An archived library drawing cannot be edited. Restore it first.", nameof(listed));
SavedBy = savedBy ?? "";
}
/// <summary>The record as it was listed; its revision guards the save.</summary>
public DrawingLibraryRecord Listed { get; }
public string SavedBy { get; }
/// <summary>The record to save: the listed details with new notes and settings.</summary>
public DrawingLibraryRecord Build(string notes, DrawingNestingSettings settings) => new()
{
Id = Listed.Id,
Name = Listed.Name,
Customer = Listed.Customer,
MaterialName = Listed.MaterialName,
MaterialGrade = Listed.MaterialGrade,
Thickness = Listed.Thickness,
Units = Listed.Units,
CutType = Listed.CutType,
Notes = notes ?? "",
Settings = settings ?? throw new ArgumentNullException(nameof(settings)),
SavedBy = SavedBy,
};
/// <summary>The first rule the edit breaks, or null when it may be saved.</summary>
public string? GetError(string notes, DrawingNestingSettings settings) =>
DrawingLibraryRules.GetMetadataError(Build(notes, settings));
/// <summary>True when saving would change nothing.</summary>
public bool IsUnchanged(string notes, DrawingNestingSettings settings) =>
(notes ?? "") == Listed.Notes && Equals(settings, Listed.Settings);
/// <summary>Saves the edit as the next revision; returns the stored record.</summary>
public async Task<DrawingLibraryRecord> SaveAsync(string notes, DrawingNestingSettings settings,
CancellationToken cancellationToken = default)
{
var record = Build(notes, settings);
var error = DrawingLibraryRules.GetMetadataError(record);
if (error != null)
throw new ArgumentException(error, nameof(settings));
var saved = await _library.UpdateMetadataAsync(Listed.Id, record, Listed.Revision, cancellationToken);
if (!string.Equals(saved.PackageSha256, Listed.PackageSha256, StringComparison.OrdinalIgnoreCase)
|| !Equals(saved.Settings, record.Settings) || saved.Notes != record.Notes)
throw new IOException("The nest server stored a different library drawing than was edited. Reload the library.");
return saved;
}
}