using Microsoft.Azure.Cosmos.Table; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Habitica.Todoist.Integration.Data { public class TableStorageClient { private CloudStorageAccount storageAccount { get; set; } private CloudTableClient tableClient { get; set; } /* TODO: change this so it reads from classes available instead of hardcoding */ private static List tableNames = new List() { "habittodolink", "todohabitlink", "todochange", "todoistsync" }; private Dictionary tables = new Dictionary(); public TableStorageClient(string connectionString) { storageAccount = CloudStorageAccount.Parse(connectionString); tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration()); // initialize all tables to use foreach (var tableName in tableNames) { var table = tableClient.GetTableReference(tableName); table.CreateIfNotExists(); tables[tableName] = table; } } public async Task RetrieveRecord(string partitionKey, string rowKey) where T : TableEntity, new() { var tableName = typeof(T).Name.ToLower(); var table = tables[tableName]; var operation = TableOperation.Retrieve(partitionKey, rowKey); var result = await table.ExecuteAsync(operation); return result.Result as T; } public async Task InsertOrUpdate(T entity) where T : TableEntity, new() { var tableName = typeof(T).Name.ToLower(); var table = tables[tableName]; var operation = TableOperation.InsertOrReplace(entity); // TODO: InsertOrReplace vs InsertOrMerge var result = await table.ExecuteAsync(operation); return result.Result as T; // TODO: might not work } public async Task ExistsAsync(string partitionKey, string rowKey) where T : TableEntity, new() { var tableName = typeof(T).Name.ToLower(); var table = tables[tableName]; var operation = TableOperation.Retrieve(partitionKey, rowKey); var result = await table.ExecuteAsync(operation); return result.Result != null; } public bool Exists(string partitionKey, string rowKey) where T : TableEntity, new() { return Query().Where(x => x.PartitionKey == partitionKey && x.RowKey == rowKey).ToList().Any(); } public TableQuery Query() where T : TableEntity, new() { var tableName = typeof(T).Name.ToLower(); var table = tables[tableName]; return table.CreateQuery(); } } }