feat(data): server queries for uploads, jobs, render nodes and sequence edits

Additive repository methods the server needs: upload state transitions,
pending bytes, per-day and per-user upload counts, render-result uploads;
job claim by id, node progress and completion, priority, queue counts and
requeue of numbered workers; sequence edits with settings_version
optimistic concurrency; audit filter by action; latest token per user.
Migration 0005 adds session_key, job_id and render_json to uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 11:31:07 +00:00
1 parent 446daeae2b
commit 9a38057169
9 files changed
+282 -6

No files matched your search

@@ -60,6 +60,28 @@ public sealed record ArchiveStats(
IReadOnlyList<FormatBucket> ByFormat,
ArchiveHealth Health);
/// <summary>Fields a client may change on a sequence; null leaves a field unchanged, an empty title or notes clears it.</summary>
public sealed record SequenceEdit
{
public string? Title { get; init; }
public IReadOnlyList<string>? Tags { get; init; }
public int? Rating { get; init; }
public string? Notes { get; init; }
public string? SettingsJson { get; init; }
}
public enum SequenceEditOutcome
{
Updated,
NotFound,
/// <summary>The settings version moved since the client read it.</summary>
Conflict,
}
/// <summary>Result of <see cref="SequenceRepository.EditAsync"/>: the updated sequence, or the stored one on conflict.</summary>
public sealed record SequenceEditResult(SequenceEditOutcome Outcome, Sequence? Sequence);
public sealed record YearBucket(int Year, int Sequences, long Photos, long Bytes);
/// <summary>One heatmap cell: sequences and photos whose first capture falls in the month.</summary>
@@ -172,6 +194,38 @@ public sealed class SequenceRepository
return updated;
}, cancellationToken);
/// <summary>
/// Applies a client edit atomically and bumps <c>settings_version</c>. With <paramref name="expectedSettingsVersion"/>
/// the edit is refused (<see cref="SequenceEditOutcome.Conflict"/>, carrying the stored sequence) when someone else
/// changed the sequence first (BRIEF §14.5).
/// </summary>
public Task<SequenceEditResult> EditAsync(Guid id, SequenceEdit edit, int? expectedSettingsVersion, CancellationToken cancellationToken = default) =>
_db.InTransactionAsync(async session =>
{
var current = await session.QuerySingleOrDefaultAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken)
.ConfigureAwait(false);
if (current is null) return new SequenceEditResult(SequenceEditOutcome.NotFound, null);
if (expectedSettingsVersion is { } expected && expected != current.SettingsVersion)
return new SequenceEditResult(SequenceEditOutcome.Conflict, current);
var updated = current with
{
Title = edit.Title is null ? current.Title : NullIfEmpty(edit.Title),
Notes = edit.Notes is null ? current.Notes : NullIfEmpty(edit.Notes),
Rating = edit.Rating ?? current.Rating,
SettingsJson = edit.SettingsJson ?? current.SettingsJson,
SettingsVersion = current.SettingsVersion + 1,
UpdatedAt = DateTimeOffset.UtcNow,
};
await UpsertRowAsync(session, updated, cancellationToken).ConfigureAwait(false);
if (edit.Tags is not null) await ReplaceTagsAsync(session, id, edit.Tags, cancellationToken).ConfigureAwait(false);
var stored = await session.QuerySingleAsync($"{_select} WHERE s.id = $1", [id], cancellationToken).ConfigureAwait(false);
return new SequenceEditResult(SequenceEditOutcome.Updated, Map(stored));
}, cancellationToken);
/// <summary>Gives every sequence owned by <paramref name="fromUserId"/> to <paramref name="toUserId"/>; returns how many.</summary>
public Task<int> ReassignOwnerAsync(Guid fromUserId, Guid toUserId, CancellationToken cancellationToken = default) =>
_db.ExecuteAsync("UPDATE sequences SET owner_user_id = $2 WHERE owner_user_id = $1", [fromUserId, toUserId], cancellationToken);
/// <summary>One page of sequences matching <paramref name="query"/>.</summary>
public Task<Page<Sequence>> ListAsync(SequenceQuery query, CancellationToken cancellationToken = default)
{
@@ -351,6 +405,8 @@ public sealed class SequenceRepository
}
}
private static string? NullIfEmpty(string value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
/// <summary>Camera wall-clock times are stored without zone, whatever kind the caller's value has.</summary>
internal static DateTime? Wall(DateTime? value) =>
value is { } v ? DateTime.SpecifyKind(v, DateTimeKind.Unspecified) : null;