/// <inheritdoc /> public async Task <TElement> UpdateAsync( TElement entity, TableStorageStrategy strategy = TableStorageStrategy.InsertOrReplace) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (entity.PartitionKey == null) { throw new PropNullException(nameof(entity.PartitionKey)); } if (entity.RowKey == null) { throw new PropNullException(nameof(entity.RowKey)); } try { await CreateTableIfNotExistsAsync(); if (strategy == TableStorageStrategy.Merge || strategy == TableStorageStrategy.Replace) { var any = await AnyAsync(entity.PartitionKey, entity.RowKey); if (!any) { throw new InvalidOperationException( UtilResources.Get("TableStorageEntityNotExists", entity.PartitionKey, entity.RowKey)); } entity.ETag ??= "*"; } var operation = strategy switch { TableStorageStrategy.InsertOrMerge => TableOperation.InsertOrMerge(entity), TableStorageStrategy.Merge => TableOperation.Merge(entity), TableStorageStrategy.Replace => TableOperation.Replace(entity), _ => TableOperation.InsertOrReplace(entity), }; var result = await ExecuteAsync(operation); ExceptionChecker.AssertHttp200Code(result.HttpStatusCode); entity = (TElement)(dynamic)result.Result; return(entity); } catch (Exception ex) { throw new TableStorageException(nameof(UpdateAsync), ex); } }
/// <inheritdoc /> public async Task <TElement> InsertAsync(TElement entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (entity.PartitionKey == null) { throw new PropNullException(nameof(entity.PartitionKey)); } if (entity.RowKey == null) { throw new PropNullException(nameof(entity.RowKey)); } try { await CreateTableIfNotExistsAsync(); var entityExists = await AnyAsync(entity.PartitionKey, entity.RowKey); if (entityExists) { throw new InvalidOperationException( UtilResources.Get("TableStorageEntityExists", entity.PartitionKey, entity.RowKey)); } var operation = TableOperation.Insert(entity); var result = await ExecuteAsync(operation); ExceptionChecker.AssertHttp200Code(result.HttpStatusCode); entity = (TElement)(dynamic)result.Result; return(entity); } catch (Exception ex) { throw new TableStorageException(nameof(InsertAsync), ex); } }
/// <inheritdoc /> public async Task <TElement> DeleteAsync(string partitionKey, string rowKey) { if (partitionKey == null) { throw new ArgumentNullException(nameof(partitionKey)); } if (rowKey == null) { throw new ArgumentNullException(nameof(rowKey)); } try { await CreateTableIfNotExistsAsync(); var entity = await GetAsync(partitionKey, rowKey); if (entity == null) { throw new InvalidOperationException( UtilResources.Get("TableStorageEntityNotExists", partitionKey, rowKey)); } var operation = TableOperation.Delete(entity); var result = await ExecuteAsync(operation); ExceptionChecker.AssertHttp200Code(result.HttpStatusCode); entity = (TElement)(dynamic)result.Result; return(entity); } catch (Exception ex) { throw new TableStorageException(nameof(DeleteAsync), ex); } }