public async Task AddProfile(UserProfile profile) { ValidateArgument(profile); var entity = new ProfileTableEntity { PartitionKey = profile.Username, RowKey = profile.Username, FirstName = profile.FirstName, LastName = profile.LastName, }; var insertOperation = TableOperation.Insert(entity); try { await table.ExecuteAsync(insertOperation); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 409) // conflict { throw new DuplicateProfileException($"Profile for user {profile.Username} already exists"); } throw new StorageErrorException("Could not write to Azure Table", e); } }
public async Task UpdateProfile(UserProfile profile) { ValidateArgument(profile); string username = profile.Username; ProfileTableEntity entity = await RetrieveEntity(username); entity.FirstName = profile.FirstName; entity.LastName = profile.LastName; TableOperation updateOperation = TableOperation.Replace(entity); try { await table.ExecuteAsync(updateOperation); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 412) // precondition failed { throw new StorageConflictException("Optimistic concurrency failed"); } throw new StorageErrorException($"Could not update profile in storage, username = {username}", e); } }