public async Task InitAsync()
        {
            Console.WriteLine("Initializing inventory database...");
            var random     = new Random();
            var dataAccess = new StorageAccess();
            await dataAccess.InitTableAsync();

            await dataAccess.InitBlobAsync();

            var table     = dataAccess.table;
            var container = dataAccess.container;

            Console.WriteLine("Deleting old table...");
            await table.DeleteIfExistsAsync();

            var tableCreated = false;

            while (!tableCreated)
            {
                try
                {
                    tableCreated = await table.CreateIfNotExistsAsync();
                }
                catch (StorageException se)
                {
                    Console.WriteLine(se.Message);
                    Console.WriteLine("Waiting for table to be deleted. Trying again in 10 seconds...");
                    Thread.Sleep(10000);
                }
                catch
                {
                    throw;
                }
            }
            for (var idx = 0; idx < skus.Length; idx += 1)
            {
                var item = new TableInventoryItem
                {
                    Sku              = skus[idx],
                    Price            = random.Next(99, 100000) / 100.0m,
                    ImageUrl         = await LoadImageAsync(container, skus[idx]),
                    HumanDescription = descriptions[idx],
                    PriceSet         = true,
                    DescriptionSet   = true,
                    ImageSet         = true,
                    IsActive         = true
                };
                var operation = TableOperation.Insert(item);
                await table.ExecuteAsync(operation);

                Console.WriteLine($"Inserted sku {item.Sku}: {item.Description}");
            }
        }
Beispiel #2
0
        public async Task <IInventoryItem> GetAsync(string sku)
        {
            if (string.IsNullOrWhiteSpace(sku))
            {
                throw new ArgumentException("sku");
            }

            await InitTableAsync();

            var temp      = TableInventoryItem.GenerateTemplateFromSku(sku);
            var operation = TableOperation.Retrieve <TableInventoryItem>(temp.PartitionKey, temp.RowKey);
            var result    = await table.ExecuteAsync(operation);

            return(result?.Result as IInventoryItem);
        }
Beispiel #3
0
        public async Task <IInventoryItem> NewSkuAsync(string sku)
        {
            if (string.IsNullOrWhiteSpace(sku))
            {
                throw new ArgumentException("sku");
            }
            await InitTableAsync();

            var newSku = new TableInventoryItem
            {
                Sku = sku
            };
            var operation = TableOperation.Insert(newSku);
            var result    = await table.ExecuteAsync(operation);

            return(newSku);
        }
Beispiel #4
0
 private async Task ReplaceAsync(TableInventoryItem item)
 {
     var operation = TableOperation.Replace(item);
     var result    = await table.ExecuteAsync(operation);
 }