1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-22 21:51:22 +01:00

object name for meta data repo

This commit is contained in:
Kyle Spearrin 2019-09-17 10:15:59 -04:00
parent c82947a5f8
commit 2c319af95b
2 changed files with 15 additions and 15 deletions

View File

@ -5,10 +5,10 @@ namespace Bit.Core.Repositories
{ {
public interface IMetaDataRespository public interface IMetaDataRespository
{ {
Task DeleteAsync(string id); Task DeleteAsync(string objectName, string id);
Task<IDictionary<string, string>> GetAsync(string id); Task<IDictionary<string, string>> GetAsync(string objectName, string id);
Task<string> GetAsync(string id, string prop); Task<string> GetAsync(string objectName, string id, string prop);
Task UpsertAsync(string id, IDictionary<string, string> dict); Task UpsertAsync(string objectName, string id, IDictionary<string, string> dict);
Task UpsertAsync(string id, KeyValuePair<string, string> keyValuePair); Task UpsertAsync(string objectName, string id, KeyValuePair<string, string> keyValuePair);
} }
} }

View File

@ -23,17 +23,17 @@ namespace Bit.Core.Repositories.TableStorage
_table = tableClient.GetTableReference("metadata"); _table = tableClient.GetTableReference("metadata");
} }
public async Task<IDictionary<string, string>> GetAsync(string id) public async Task<IDictionary<string, string>> GetAsync(string objectName, string id)
{ {
var query = new TableQuery<DictionaryEntity>().Where( var query = new TableQuery<DictionaryEntity>().Where(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, id)); TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{objectName}_{id}"));
var queryResults = await _table.ExecuteQuerySegmentedAsync(query, null); var queryResults = await _table.ExecuteQuerySegmentedAsync(query, null);
return queryResults.Results.FirstOrDefault()?.ToDictionary(d => d.Key, d => d.Value.StringValue); return queryResults.Results.FirstOrDefault()?.ToDictionary(d => d.Key, d => d.Value.StringValue);
} }
public async Task<string> GetAsync(string id, string prop) public async Task<string> GetAsync(string objectName, string id, string prop)
{ {
var dict = await GetAsync(id); var dict = await GetAsync(objectName, id);
if(dict != null && dict.ContainsKey(prop)) if(dict != null && dict.ContainsKey(prop))
{ {
return dict[prop]; return dict[prop];
@ -41,10 +41,10 @@ namespace Bit.Core.Repositories.TableStorage
return null; return null;
} }
public async Task UpsertAsync(string id, KeyValuePair<string, string> keyValuePair) public async Task UpsertAsync(string objectName, string id, KeyValuePair<string, string> keyValuePair)
{ {
var query = new TableQuery<DictionaryEntity>().Where( var query = new TableQuery<DictionaryEntity>().Where(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, id)); TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"{objectName}_{id}"));
var queryResults = await _table.ExecuteQuerySegmentedAsync(query, null); var queryResults = await _table.ExecuteQuerySegmentedAsync(query, null);
var entity = queryResults.Results.FirstOrDefault(); var entity = queryResults.Results.FirstOrDefault();
if(entity == null) if(entity == null)
@ -62,11 +62,11 @@ namespace Bit.Core.Repositories.TableStorage
await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity)); await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
} }
public async Task UpsertAsync(string id, IDictionary<string, string> dict) public async Task UpsertAsync(string objectName, string id, IDictionary<string, string> dict)
{ {
var entity = new DictionaryEntity var entity = new DictionaryEntity
{ {
PartitionKey = id PartitionKey = $"{objectName}_{id}"
}; };
foreach(var item in dict) foreach(var item in dict)
{ {
@ -75,13 +75,13 @@ namespace Bit.Core.Repositories.TableStorage
await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity)); await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
} }
public async Task DeleteAsync(string id) public async Task DeleteAsync(string objectName, string id)
{ {
try try
{ {
await _table.ExecuteAsync(TableOperation.Delete(new DictionaryEntity await _table.ExecuteAsync(TableOperation.Delete(new DictionaryEntity
{ {
PartitionKey = id, PartitionKey = $"{objectName}_{id}",
ETag = "*" ETag = "*"
})); }));
} }