Author : MD TAREQ HASSAN | Updated : 2020/10/27

The app we are gonna create

Prerequisites

Dependency

Install-Package Microsoft.Azure.WebJobs.Extensions.Storage

Table storage

Visual Studio project

Creating Azure Function project in Visual Studio Step 1

Creating Azure Function project in Visual Studio Step 2

Creating Azure Function project in Visual Studio Step 3

Creating Azure Function project in Visual Studio Step 4

Creating Azure Function project in Visual Studio Step 5

Creating Azure Function project in Visual Studio Step 6

Implement logic

Models

// OrderItem.cs
public class OrderItem
{
	public string Id { get; set; }

	public string Name { get; set; }

	public int Quantity { get; set; }

	public string Size { get; set; }
}

// Order.cs
public class Order
{
	public List<OrderItem> Items { get; set; }
}

// TableOrderItem.cs
public class TableOrderItem : OrderItem
{
	public TableOrderItem()
	{}
	public TableOrderItem(OrderItem item)
	{
		base.Id = item.Id;
		base.Name = item.Name;
		base.Size = item.Size;
		base.Quantity = item.Quantity;
	}
	public string PartitionKey { get; set; }

	public string RowKey { get; set; }
}

Rename file name to ‘OrderFunction’: OrderFunction.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;

namespace HoverFunctionApp
{
    public static class OrderFunction
    {
        [FunctionName("OrderFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", 
            Route = null)] HttpRequestMessage req,
            [Table("orderhistory")]ICollector<TableOrderItem> items,
            ILogger log)
        {
            log.LogInformation("Hover -> (HTTP trigger) function processed a request.");
            string orderId = System.Guid.NewGuid().ToString();

            Order order = await req.Content.ReadAsAsync<Order>();

            foreach (var item in order.Items)
            {
                TableOrderItem toi = new TableOrderItem(item);
                toi.PartitionKey = "history";
                toi.RowKey = $"{orderId} - {item.Id}";
                items.Add(toi);
            }

            return new OkResult();
        }
    }
}

Run locally and test

Azure storage explorer

Azure storage explorer

Running function app

Running function app locally

Send request to function app using postman

Send request to function app using postman

Request data:

{"Items": [
    {"Id":"xyz-9b80-12b956b90de4", "Name":"ABC", "Quantity": 1, "Size":"M"},
    {"Id":"xyz-4e1d-eb4f9fc8f907", "Name":"PQR", "Quantity": 3, "Size":"L"},
    {"Id":"xyz-4e1d-e9ffc8bebb1e", "Name":"UVW", "Quantity": 6, "Size":"S"}
]}

Check storage explorer to see if order is created

Checking order explorer to see if order is created

Publish to Azure cloud and test

Create function app in Azure portal:

Publish function app from visual studio:

Check published function app in Azure portal:

Text using Postman:

Request data:

{"Items": [
    {"Id":"hovermind-12b956b90de4", "Name":"ABC", "Quantity": 1, "Size":"M"},
    {"Id":"hovermind-eb4f9fc8f907", "Name":"PQR", "Quantity": 3, "Size":"L"},
    {"Id":"hovermind-e9ffc8bebb1e", "Name":"UVW", "Quantity": 6, "Size":"S"}
]}

Azure Function app - publish to Azure cloud and test Step 1

Azure Function app - publish to Azure cloud and test Step 2

Azure Function app - publish to Azure cloud and test Step 3

Azure Function app - publish to Azure cloud and test Step 4

Azure Function app - publish to Azure cloud and test Step 5

Azure Function app - publish to Azure cloud and test Step 6

Azure Function app - publish to Azure cloud and test Step 7

Azure Function app - publish to Azure cloud and test Step 8

Azure Function app - publish to Azure cloud and test Step 9

Azure Function app - publish to Azure cloud and test Step 10

Azure Function app - publish to Azure cloud and test Step 11

Azure Function app - publish to Azure cloud and test Step 12

Azure Function app - publish to Azure cloud and test Step 13

Azure Function app - publish to Azure cloud and test Step 14

Azure Function app - publish to Azure cloud and test Step 15

Azure Function app - publish to Azure cloud and test Step 16

Azure Function app - publish to Azure cloud and test Step 17

Azure Function app - publish to Azure cloud and test Step 18

Azure Function app - publish to Azure cloud and test Step 19

Azure Function app - publish to Azure cloud and test Step 20

Azure Function app - publish to Azure cloud and test Step 21

Azure Function app - publish to Azure cloud and test Step 22