public async Task UpdateItemAsync(InventoryItem item, InventoryItemChange change, InventoryItemSnapshot snapshot, CancellationToken cancellationToken = default) { Guard.Argument(item, nameof(item)).NotNull(); Guard.Argument(change, nameof(change)).NotNull(); Guard.Argument(snapshot, nameof(snapshot)).NotNull(); var lastChange = await DbContext.InventoryItemChanges .Where(someChange => someChange.ItemId == item.Id) .OrderBy(someChange => someChange.Sequence) .LastAsync(cancellationToken).ConfigureAwait(false); var lastSnapshot = await DbContext.InventoryItemSnapshots .Where(someSnapshot => someSnapshot.ItemId == item.Id) .OrderBy(someSnapshot => someSnapshot.Sequence) .LastAsync(cancellationToken).ConfigureAwait(false); change.ItemId = item.Id; change.Sequence = lastChange.Sequence + 1; change.NewSnapshotSequence = lastSnapshot.Sequence + 1; DbContext.InventoryItemChanges.Add(change); snapshot.ItemId = item.Id; snapshot.Sequence = lastSnapshot.Sequence + 1; DbContext.InventoryItemSnapshots.Add(snapshot); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); }
public async Task <CreateItemResponse> CreateItem(CreateItemRequest request, CancellationToken cancellationToken) { var change = new InventoryItemChange { UserId = AuthenticatedAppUserId, Date = DateTimeOffset.Now }; await _inventoryService.CreateItemAsync(request.Item, change, request.Snapshot, cancellationToken).ConfigureAwait(false); return(new CreateItemResponse { NewItemId = request.Item.Id }); }
public async Task <ActionResult> DeleteItem(int itemId, CancellationToken cancellationToken) { var item = await _inventoryService.FindItemByIdAsync(itemId, cancellationToken).ConfigureAwait(false); if (item is null) { return(NotFound()); } var change = new InventoryItemChange { UserId = AuthenticatedAppUserId, Date = DateTimeOffset.Now }; await _inventoryService.DeleteItemAsync(item, change, cancellationToken).ConfigureAwait(false); return(Ok()); }
public async Task CreateItemAsync(InventoryItem item, InventoryItemChange change, InventoryItemSnapshot snapshot, CancellationToken cancellationToken = default) { Guard.Argument(item, nameof(item)).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.InventoryItems.Add(item); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); change.ItemId = item.Id; change.Sequence = 1; change.NewSnapshotSequence = 1; DbContext.InventoryItemChanges.Add(change); snapshot.ItemId = item.Id; snapshot.Sequence = 1; DbContext.InventoryItemSnapshots.Add(snapshot); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); }
public async Task UpdateItemChangeAsync(InventoryItemChange change, CancellationToken cancellationToken = default) { Guard.Argument(change, nameof(change)).NotNull(); await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); }