Пример #1
0
        public static async Task <IActionResult> UpdateItem(
            [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "UpdateItem/{id}")] FoodfestItemPatchBindingModel bindingModel,
            [Table("FoodfestItems", "Foods", Connection = "AzureWebJobsStorage")] CloudTable foodfestItemsTable,
            ILogger log,
            string id)
        {
            try
            {
                var findOperation = TableOperation.Retrieve <FoodfestItem>("Foods", id);
                var findResult    = await foodfestItemsTable.ExecuteAsync(findOperation);

                if (findResult.Result == null)
                {
                    return(new NotFoundResult());
                }

                var existingRow = (FoodfestItem)findResult.Result;
                existingRow.Food   = bindingModel.Food ?? existingRow.Food;
                existingRow.Amount = bindingModel.Amount ?? existingRow.Amount;

                var replaceOperation = TableOperation.Replace(existingRow);
                await foodfestItemsTable.ExecuteAsync(replaceOperation);

                return(new OkObjectResult(FoodfestFactory.GetBindingModelFor(existingRow)));
            }
            catch
            {
                return(new BadRequestResult());
            }
        }
Пример #2
0
        public static async Task <IEnumerable <FoodfestItemBindingModel> > GetItems(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            [Table("FoodfestItems", "Foods", Connection = "AzureWebJobsStorage")] CloudTable foodfestItemsTable,
            ILogger log)
        {
            var query   = new TableQuery <FoodfestItem>();
            var segment = await foodfestItemsTable.ExecuteQuerySegmentedAsync(query, null);

            return(segment.Results.Select(tableModel => FoodfestFactory.GetBindingModelFor(tableModel)));
        }
Пример #3
0
        public static async Task <IActionResult> AddItem(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] FoodfestItemPostBindingModel bindingModel,
            [Table("FoodfestItems", "Foods", Connection = "AzureWebJobsStorage")] IAsyncCollector <FoodfestItem> foodfestItemsTable,
            ILogger log)
        {
            try
            {
                var tableModel = FoodfestFactory.GetTableModelFor(bindingModel);
                await foodfestItemsTable.AddAsync(tableModel);

                log.LogInformation($"Added {tableModel.RowKey}: {tableModel.Food} by {tableModel.FullName}");

                return(new OkObjectResult(FoodfestFactory.GetBindingModelFor(tableModel)));
            }
            catch
            {
                return(new BadRequestResult());
            }
        }