public async Task <ResourceKey> DeleteAsync(ResourceKey key, bool hardDelete, CancellationToken cancellationToken = default(CancellationToken)) { EnsureArg.IsNotNull(key, nameof(key)); if (!string.IsNullOrEmpty(key.VersionId)) { throw new MethodNotAllowedException(Core.Resources.DeleteVersionNotAllowed); } string version = null; if (hardDelete) { await _dataStore.HardDeleteAsync(key, cancellationToken); } else { ResourceWrapper existing = await _dataStore.GetAsync(key, cancellationToken); version = existing?.Version; if (existing?.IsDeleted == false) { var emptyInstance = (Resource)Activator.CreateInstance(ModelInfo.GetTypeForFhirType(existing.ResourceTypeName)); emptyInstance.Id = existing.ResourceId; ResourceWrapper deletedWrapper = CreateResourceWrapper(emptyInstance, deleted: true); bool keepHistory = await _conformanceProvider.Value.CanKeepHistory(key.ResourceType, cancellationToken); UpsertOutcome result = await _dataStore.UpsertAsync( deletedWrapper, WeakETag.FromVersionId(existing.Version), allowCreate : true, keepHistory : keepHistory, cancellationToken : cancellationToken); version = result.Wrapper.Version; } } return(new ResourceKey(key.ResourceType, key.Id, version)); }
public async Task <SaveOutcome> UpsertAsync(Resource resource, WeakETag weakETag = null, CancellationToken cancellationToken = default(CancellationToken)) { EnsureArg.IsNotNull(resource, nameof(resource)); if (await _conformanceProvider.Value.RequireETag(resource.TypeName, cancellationToken) && weakETag == null) { throw new PreconditionFailedException(string.Format(Core.Resources.IfMatchHeaderRequiredForResource, resource.TypeName)); } bool allowCreate = await _conformanceProvider.Value.CanUpdateCreate(resource.TypeName, cancellationToken); bool keepHistory = await _conformanceProvider.Value.CanKeepHistory(resource.TypeName, cancellationToken); ResourceWrapper resourceWrapper = CreateResourceWrapper(resource, deleted: false); UpsertOutcome result = await _dataStore.UpsertAsync(resourceWrapper, weakETag, allowCreate, keepHistory, cancellationToken); resource.VersionId = result.Wrapper.Version; return(new SaveOutcome(resource, result.OutcomeType)); }
public async Task <Resource> CreateAsync(Resource resource, CancellationToken cancellationToken = default(CancellationToken)) { EnsureArg.IsNotNull(resource, nameof(resource)); // If an Id is supplied on create it should be removed/ignored resource.Id = null; ResourceWrapper resourceWrapper = CreateResourceWrapper(resource, deleted: false); bool keepHistory = await _conformanceProvider.Value.CanKeepHistory(resource.TypeName, cancellationToken); UpsertOutcome result = await _dataStore.UpsertAsync( resourceWrapper, weakETag : null, allowCreate : true, keepHistory : keepHistory, cancellationToken : cancellationToken); resource.VersionId = result.Wrapper.Version; return(resource); }