mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 05:21:42 -05:00
refactor: share duplicated snapshot, history and fetch helpers
- CuttingPlanSnapshot: one private Copy carries every value and init property for WithSearchLimits, WithTabLeadOuts, WithPreservedOrder and WithTabbedPartsAvoided, keeping the defensive CrossableTabbedParts copy. - NestPhaseExtensions: DisplayName and ShortName share one attribute lookup and keep their own caches. - NestDatabase: History, DrawingHistory and DrawingSetHistory share ReadRevisionPage (count, page query, read rows) under the same lock. - NestServerClient.GetOrNullAsync: the GET, 404 check-then-null, ensure success and read flow used by the Remote* clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
409d30d29e
commit
1f7fd248fe
11 files changed
+126
-184
No files matched your search
@@ -66,15 +66,9 @@ public sealed class RemoteDrawingLibrary : NestServerClient, IDrawingLibraryStor
|
||||
public async Task<DrawingLibraryRecord?> GetAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ValidateId(id);
|
||||
using var response = await _httpClient.GetAsync(Url($"api/drawings/{id}"), cancellationToken);
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
await ThrowIfServerTooOld(cancellationToken);
|
||||
return null;
|
||||
}
|
||||
await NestServerHttp.EnsureSuccessAsync(response, $"read library drawing {id}", cancellationToken);
|
||||
var record = await ReadRecord(response, cancellationToken);
|
||||
return record.Id == id ? record : throw new IOException("Nest server returned a different library drawing.");
|
||||
var record = await GetOrNullAsync($"api/drawings/{id}", $"read library drawing {id}", ReadRecord,
|
||||
ThrowIfServerTooOld, cancellationToken);
|
||||
return record is null || record.Id == id ? record : throw new IOException("Nest server returned a different library drawing.");
|
||||
}
|
||||
|
||||
/// <summary>The current package bytes, or null for an unknown id.</summary>
|
||||
@@ -90,16 +84,13 @@ public sealed class RemoteDrawingLibrary : NestServerClient, IDrawingLibraryStor
|
||||
{
|
||||
ValidateId(id);
|
||||
NestServerHttp.ValidatePaging(offset, limit, DrawingLibraryRules.MaxRevisionLimit);
|
||||
using var response = await _httpClient.GetAsync(
|
||||
Url($"api/drawings/{id}/revisions?{NestServerHttp.Paging(offset, limit)}"), cancellationToken);
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
await ThrowIfServerTooOld(cancellationToken);
|
||||
var page = await GetOrNullAsync($"api/drawings/{id}/revisions?{NestServerHttp.Paging(offset, limit)}",
|
||||
$"read library drawing history {id}",
|
||||
async (response, token) => await response.Content.ReadFromJsonAsync<DrawingLibraryPage>(NestServerHttp.JsonOptions, token)
|
||||
?? throw new IOException("Nest server returned an empty drawing history page."),
|
||||
ThrowIfServerTooOld, cancellationToken);
|
||||
if (page is null)
|
||||
return null;
|
||||
}
|
||||
await NestServerHttp.EnsureSuccessAsync(response, $"read library drawing history {id}", cancellationToken);
|
||||
var page = await response.Content.ReadFromJsonAsync<DrawingLibraryPage>(NestServerHttp.JsonOptions, cancellationToken)
|
||||
?? throw new IOException("Nest server returned an empty drawing history page.");
|
||||
if (page.Items is null || !NestServerHttp.IsBoundedPage(page.Items.Count, page.Total, page.Offset, page.Limit, offset, limit)
|
||||
|| !NestServerHttp.IsRevisionHistory(page.Items, id, item => item.Id, item => item.Revision))
|
||||
throw new IOException("Nest server returned an invalid or unbounded drawing history page.");
|
||||
@@ -172,17 +163,8 @@ public sealed class RemoteDrawingLibrary : NestServerClient, IDrawingLibraryStor
|
||||
ReadRecord, current => new DrawingLibraryConflictException(current), saved => (saved.Id, saved.Revision),
|
||||
ThrowIfServerTooOld, cancellationToken);
|
||||
|
||||
private async Task<byte[]?> GetBytesAsync(string relative, string action, CancellationToken cancellationToken)
|
||||
{
|
||||
using var response = await _httpClient.GetAsync(Url(relative), cancellationToken);
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
await ThrowIfServerTooOld(cancellationToken);
|
||||
return null;
|
||||
}
|
||||
await NestServerHttp.EnsureSuccessAsync(response, action, cancellationToken);
|
||||
return await response.Content.ReadAsByteArrayAsync(cancellationToken);
|
||||
}
|
||||
private Task<byte[]?> GetBytesAsync(string relative, string action, CancellationToken cancellationToken) =>
|
||||
GetOrNullAsync(relative, action, ReadBytesAsync, ThrowIfServerTooOld, cancellationToken);
|
||||
|
||||
// A 404 for one drawing is ambiguous; the query route exists on every library-aware server.
|
||||
private Task ThrowIfServerTooOld(CancellationToken cancellationToken) =>
|
||||
|
||||
Reference in new issue
Block a user