public async Task UpdateAssigneeAsync(InventoryAssignee assignee, InventoryAssigneeChange change, InventoryAssigneeSnapshot snapshot, CancellationToken cancellationToken = default) { Guard.Argument(assignee, nameof(assignee)).NotNull(); Guard.Argument(change, nameof(change)).NotNull(); Guard.Argument(snapshot, nameof(snapshot)).NotNull(); var lastChange = await DbContext.InventoryAssigneeChanges .Where(someChange => someChange.AssigneeId == assignee.Id) .OrderBy(someChange => someChange.Sequence) .LastAsync(cancellationToken).ConfigureAwait(false); var lastSnapshot = await DbContext.InventoryAssigneeSnapshots .Where(someSnapshot => someSnapshot.AssigneeId == assignee.Id) .OrderBy(someSnapshot => someSnapshot.Sequence) .LastAsync(cancellationToken).ConfigureAwait(false); change.AssigneeId = assignee.Id; change.Sequence = lastChange.Sequence + 1; change.NewSnapshotSequence = lastSnapshot.Sequence + 1; DbContext.InventoryAssigneeChanges.Add(change); snapshot.AssigneeId = assignee.Id; snapshot.Sequence = lastSnapshot.Sequence + 1; DbContext.InventoryAssigneeSnapshots.Add(snapshot); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); }
public async Task CreateAssigneeAsync(InventoryAssignee assignee, InventoryAssigneeChange change, InventoryAssigneeSnapshot snapshot, CancellationToken cancellationToken = default) { Guard.Argument(assignee, nameof(assignee)).NotNull(); Guard.Argument(change, nameof(change)).NotNull(); Guard.Argument(snapshot, nameof(snapshot)).NotNull(); await using var transaction = await DbContext.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false); DbContext.InventoryAssignees.Add(assignee); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); change.AssigneeId = assignee.Id; change.Sequence = 1; change.NewSnapshotSequence = 1; DbContext.InventoryAssigneeChanges.Add(change); snapshot.AssigneeId = assignee.Id; snapshot.Sequence = 1; DbContext.InventoryAssigneeSnapshots.Add(snapshot); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); }