public virtual async Task <TEntity> GetAsync(Guid id) { if (Table == null) { throw new InvalidOperationException($"Could not find a valid {nameof(Table)}."); } if (!_initialized && !_isReadOnly) { await Table.CreateIfNotExistsAsync(); _initialized = true; } try { TableOperation operation = TableOperation.Retrieve <TEntity>(_defaultPartitionKey, id.ToString()); TableResult result = await Table.ExecuteAsync(operation); if (!result.IsSuccessStatusCode()) { throw new InvalidOperationException($"Could not add the entry ({result.HttpStatusCode}) : {result.Etag} - {result.Result}"); } return((TEntity)result.Result); } catch (Exception e) { throw new InvalidOperationException("Error while retrieving the entity.", e); } }
public void StatusCode300_ReturnsFalse() { var result = new TableResult() { HttpStatusCode = 300 }; Assert.False(result.IsSuccessStatusCode()); }
public void StatusCode299_ReturnsTrue() { var result = new TableResult() { HttpStatusCode = 299 }; Assert.True(result.IsSuccessStatusCode()); }
public virtual async Task <TEntity> CreateOrUpdateAsync(TEntity entity) { if (_isReadOnly) { throw new InvalidOperationException($"This Azure Storage Service is in readonly mode."); } if (Table == null) { throw new InvalidOperationException($"Could not find a valid {nameof(Table)}."); } if (string.IsNullOrEmpty(entity.PartitionKey)) { entity.PartitionKey = _defaultPartitionKey; } if (string.IsNullOrEmpty(entity.RowKey)) { entity.RowKey = Guid.NewGuid().ToString(); } if (!_initialized) { await Table.CreateIfNotExistsAsync(); _initialized = true; } try { TableOperation operation = TableOperation.InsertOrReplace(entity); TableResult result = await Table.ExecuteAsync(operation); if (!result.IsSuccessStatusCode()) { throw new InvalidOperationException($"Could not add the entry ({result.HttpStatusCode}) : {result.Etag} - {result.Result}"); } return((TEntity)result.Result); } catch (Exception e) { throw new InvalidOperationException("Error while creating the entity.", e); } }
public virtual async Task <int> DeleteAsync(TEntity entity) { if (_isReadOnly) { throw new InvalidOperationException($"This Azure Storage Service is in readonly mode."); } if (Table == null) { throw new InvalidOperationException($"Could not find a valid {nameof(Table)}."); } if (!_initialized) { throw new InvalidOperationException($"The {nameof(Table)} is not initialized."); } if (string.IsNullOrEmpty(entity.RowKey)) { throw new ArgumentException($"The entity does not have a valid {nameof(ITableEntity.RowKey)}."); } try { TableOperation operation = TableOperation.Delete(entity); TableResult result = await Table.ExecuteAsync(operation); if (!result.IsSuccessStatusCode()) { throw new InvalidOperationException($"Could not delete the entry ({result.HttpStatusCode}) : {result.Etag} - {result.Result}"); } return(1); } catch (Exception e) { throw new InvalidOperationException("Error while deleting the entity.", e); } }