almost ready
This commit is contained in:
64
BlueLaminate/BlueLaminate.EFCore/Data/SweepCheckpoints.cs
Normal file
64
BlueLaminate/BlueLaminate.EFCore/Data/SweepCheckpoints.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using BlueLaminate.EFCore.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BlueLaminate.EFCore.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Write helpers for the per-site sweep checkpoints (<see cref="SkinSweep"/> /
|
||||
/// <see cref="SkinConditionSweep"/>). Each marketplace sweeper stamps its own row
|
||||
/// keyed by <c>(entity, source)</c>, so a band swept on one site is still "never
|
||||
/// swept" on another. Adding a new site means a new <see cref="SweepSource"/>
|
||||
/// constant — no schema changes.
|
||||
/// <para>
|
||||
/// Reads stay inline in the sweep queries (a correlated subquery over the navigation
|
||||
/// for the relevant <c>Source</c>) so EF can translate and order by them server-side.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class SweepCheckpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// Record that <paramref name="source"/> just swept this wear band. Upserts the
|
||||
/// single (condition, source) row via the change tracker; the caller persists with
|
||||
/// <see cref="DbContext.SaveChangesAsync"/>.
|
||||
/// </summary>
|
||||
public static async Task StampConditionAsync(
|
||||
SkinTrackerDbContext db, int conditionId, string source, DateTimeOffset sweptAt, CancellationToken ct)
|
||||
{
|
||||
var existing = await db.SkinConditionSweeps
|
||||
.FirstOrDefaultAsync(s => s.SkinConditionId == conditionId && s.Source == source, ct);
|
||||
if (existing is null)
|
||||
{
|
||||
db.SkinConditionSweeps.Add(new SkinConditionSweep
|
||||
{
|
||||
SkinConditionId = conditionId,
|
||||
Source = source,
|
||||
SweptAt = sweptAt,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.SweptAt = sweptAt;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>As <see cref="StampConditionAsync"/>, for a whole-skin unit (no wear bands).</summary>
|
||||
public static async Task StampSkinAsync(
|
||||
SkinTrackerDbContext db, int skinId, string source, DateTimeOffset sweptAt, CancellationToken ct)
|
||||
{
|
||||
var existing = await db.SkinSweeps
|
||||
.FirstOrDefaultAsync(s => s.SkinId == skinId && s.Source == source, ct);
|
||||
if (existing is null)
|
||||
{
|
||||
db.SkinSweeps.Add(new SkinSweep
|
||||
{
|
||||
SkinId = skinId,
|
||||
Source = source,
|
||||
SweptAt = sweptAt,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.SweptAt = sweptAt;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user