Пример #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
            [StorageAccount("StorageConnectionString")] CloudStorageAccount storageAccount,
            TraceWriter log
            )
        {
            var    client       = storageAccount.CreateCloudTableClient();
            string partitionKey = await Authenticator.Authenticate(client, req);

            if (string.IsNullOrEmpty(partitionKey))
            {
                return(new UnauthorizedResult());
            }

            string json = await req.ReadAsStringAsync();

            var items = JsonConvert.DeserializeObject <List <ItemJson> >(json);

            if (items == null)
            {
                return(new BadRequestObjectResult("Could not correctly parse the request parameters"));
            }
            else if (items?.Count > 100)
            {
                return(new BadRequestObjectResult("Too many items to store, maximum 100 per call"));
            }
            else if (items?.Count <= 0)
            {
                return(new BadRequestObjectResult("No items to store"));
            }

            var partition = await PartionUtility.GetUploadPartition(log, client, partitionKey);

            log.Info($"Received a request from {partitionKey} to upload {items.Count} items to {partition.RowKey}");

            var itemTable = client.GetTableReference(ItemV1.TableName);
            await itemTable.CreateIfNotExistsAsync();

            var itemMapping = await Insert(partitionKey, partition.RowKey, itemTable, items);

            log.Info($"Inserted {items} items over {itemMapping} batches");

            return(new OkObjectResult(itemMapping));
        }
Пример #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
            [StorageAccount("StorageConnectionString")] CloudStorageAccount storageAccount,
            TraceWriter log
            )
        {
            var    client       = storageAccount.CreateCloudTableClient();
            string partitionKey = await Authenticator.Authenticate(client, req);

            if (string.IsNullOrEmpty(partitionKey))
            {
                return(new UnauthorizedResult());
            }


            string json = await req.ReadAsStringAsync();

            var data = JsonConvert.DeserializeObject <List <ItemToRemove> >(json);

            if (data == null)
            {
                return(new BadRequestObjectResult("Could not correctly parse the request parameters"));
            }
            else if (data?.Count <= 0)
            {
                return(new BadRequestObjectResult("No items to delete"));
            }

            log.Info($"Received a request from {partitionKey} to remove {data.Count} items");
            var itemTable = client.GetTableReference(ItemV1.TableName);

            DeleteByPartition(partitionKey, itemTable, data, log);


            var partition = await PartionUtility.GetUploadPartition(log, client, partitionKey);

            var deletedItemsTable = client.GetTableReference(DeletedItemV1.TableName);

            DeleteInActivePartition(partition.RowKey, deletedItemsTable, data);
            log.Info($"Marked {data.Count} items as deleted in the active partition {partition.RowKey}");

            return(new OkResult());
        }