protected override async Task <IReadOnlyCollection <TReadModel> > Process(EntityIdentifiersQuery <string, TReadModel> request, CancellationToken cancellationToken) { if (request is null) { throw new System.ArgumentNullException(nameof(request)); } var keys = new HashSet <string>(); foreach (var requestId in request.Ids) { if (CosmosKey.TryDecode(requestId, out var id, out PartitionKey partitionKey)) { keys.Add(id); } } var query = await Repository .GetQueryableAsync() .ConfigureAwait(false); var results = await query .Where(p => keys.Contains(p.Id)) .ToListAsync(cancellationToken) .ConfigureAwait(false); return(Mapper.Map <IList <TEntity>, IReadOnlyCollection <TReadModel> >(results)); }
public void TryDecodeTests(string cosmosKey, bool expectedResult, string expectedId, PartitionKey expectedPartitionKey) { var result = CosmosKey.TryDecode(cosmosKey, out var id, out var partitionKey); result.Should().Be(expectedResult); id.Should().Be(expectedId); partitionKey.Should().Be(expectedPartitionKey); }
protected override async Task <TReadModel> Process(EntityUpsertCommand <string, TUpdateModel, TReadModel> request, CancellationToken cancellationToken) { if (request is null) { throw new ArgumentNullException(nameof(request)); } if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey)) { throw new InvalidOperationException("Invalid Cosmos Key format"); } var entity = await Repository .FindAsync(id, partitionKey, cancellationToken) .ConfigureAwait(false); // create entity if not found if (entity == null) { entity = new TEntity { Id = id }; // apply create metadata if (entity is ITrackCreated createdModel) { createdModel.Created = request.Activated; createdModel.CreatedBy = request.ActivatedBy; } } // copy updates from model to entity Mapper.Map(request.Model, entity); // apply update metadata if (entity is ITrackUpdated updateEntity) { updateEntity.Updated = request.Activated; updateEntity.UpdatedBy = request.ActivatedBy; } // save updates var savedEntity = await Repository .UpdateAsync(entity, cancellationToken) .ConfigureAwait(false); // return read model var model = Mapper.Map <TReadModel>(savedEntity); return(model); }
protected override async Task <TReadModel> Process(EntityDeleteCommand <string, TReadModel> request, CancellationToken cancellationToken) { if (request is null) { throw new ArgumentNullException(nameof(request)); } if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey)) { throw new InvalidOperationException("Invalid Cosmos Key format"); } var entity = await Repository .FindAsync(id, partitionKey, cancellationToken) .ConfigureAwait(false); if (entity == null) { return(default !);
protected override async Task <TReadModel> Process(EntityIdentifierQuery <string, TReadModel> request, CancellationToken cancellationToken) { if (request is null) { throw new ArgumentNullException(nameof(request)); } if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey)) { throw new InvalidOperationException("Invalid Cosmos Key format"); } var entity = await Repository .FindAsync(id, partitionKey, cancellationToken) .ConfigureAwait(false); // convert deleted entity to read model var model = Mapper.Map <TReadModel>(entity); return(model); }