// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            InventoryData inventoryData = new InventoryData();

            try
            {
                using (StreamReader file = File.OpenText("InventoryItem.json"))
                {
                    inventoryData.InventoryItems = new ObservableCollection <InventoryItem>((List <InventoryItem>)JsonSerializer.Deserialize(file.ReadToEnd(),
                                                                                                                                             returnType: typeof(List <InventoryItem>)) ?? throw new InvalidOperationException("Empty"));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            services.AddControllers();
            services.AddSingleton <IDatabase, InventoryData>();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "InventoryApp", Version = "v1"
                });
            });
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string connectionsJson = File.ReadAllText(Path.Combine(context.FunctionAppDirectory, "Connections.json"));

            JObject ConnectionsObject = JObject.Parse(connectionsJson);

            string connectionString = ConnectionsObject["AZURE_STORAGE_URL"].ToString();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            CloudTable table = tableClient.GetTableReference("InventoryData");

            await table.CreateIfNotExistsAsync();

            var room = req.Headers["room"];

            if (string.IsNullOrEmpty(room))
            {
                room = req.Query["room"];
            }

            if (string.IsNullOrEmpty(room))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Please pass a room name on the query string or in the header")
                });
            }

            var partitionKey = "Demo";
            var rowKey       = room;

            try
            {
                // get the room from the table
                var getRoom = TableOperation.Retrieve <InventoryData>(partitionKey, rowKey);

                var query = await table.ExecuteAsync(getRoom);

                var currInventoryData = (InventoryData)query.Result;

                // if room not exist, create a record using default data
                if (currInventoryData == null)
                {
                    var defaultRoom = new InventoryData(partitionKey, rowKey);
                    var createRoom  = TableOperation.Insert(defaultRoom);
                    await table.ExecuteAsync(createRoom);

                    currInventoryData = (InventoryData)(await table.ExecuteAsync(getRoom)).Result;
                }

                var operation = req.Query["operation"].ToString().ToLower();
                var product   = req.Query["product"].ToString().ToLower();
                var quantity  = req.Query["quantity"].ToString().ToLower();
                var updated   = false;

                log.LogInformation($"Executing {operation} on {(string.IsNullOrEmpty(product) ? "no product" : product)} for {(string.IsNullOrEmpty(quantity) ? "0" : quantity)}");

                if (!string.IsNullOrEmpty(operation))
                {
                    if (operation.Equals("reset"))
                    {
                        currInventoryData.LoadDefaultData();
                        updated = true;
                    }
                    else if (operation.Equals("help"))
                    {
                        currInventoryData.Help = true;
                        updated = true;
                    }
                    else if (operation.Equals("query"))
                    {
                        //This is to ensure the json data object is returned to Custom Commands.
                        updated = true;
                    }
                    else if (operation.Equals("remove"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            currInventoryData.FirstItem -= int.Parse(quantity);
                            currInventoryData.Message    = "Shipped " + quantity + " small items";
                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            currInventoryData.SecondItem -= int.Parse(quantity);
                            currInventoryData.Message     = "Shipped " + quantity + " medium items";
                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            currInventoryData.ThirdItem -= int.Parse(quantity);
                            currInventoryData.Message    = "Shipped " + quantity + " large items";
                            updated = true;
                        }
                        else
                        {
                            currInventoryData.Help = true;
                            updated = true;
                        }
                    }
                    else if (operation.Equals("deplete"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            currInventoryData.Message   = "Shipped all " + currInventoryData.FirstItem + " small items";
                            currInventoryData.FirstItem = 0;

                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            currInventoryData.Message    = "Shipped all " + currInventoryData.SecondItem + " medium items";
                            currInventoryData.SecondItem = 0;

                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            currInventoryData.Message   = "Shipped all " + currInventoryData.ThirdItem + " large items";
                            currInventoryData.ThirdItem = 0;

                            updated = true;
                        }
                        else
                        {
                            currInventoryData.FirstItem  = 0;
                            currInventoryData.SecondItem = 0;
                            currInventoryData.ThirdItem  = 0;
                            currInventoryData.Message    = "Shipped all items";
                            updated = true;
                        }
                    }
                    else if (operation.Equals("add"))
                    {
                        currInventoryData.Help = false;
                        if (product.Equals("blue"))
                        {
                            currInventoryData.FirstItem += int.Parse(quantity);
                            currInventoryData.Message    = "Received " + quantity + " small items";
                            updated = true;
                        }
                        else if (product.Equals("yellow"))
                        {
                            currInventoryData.SecondItem += int.Parse(quantity);
                            currInventoryData.Message     = "Received " + quantity + " medium items";
                            updated = true;
                        }
                        else if (product.Equals("green"))
                        {
                            currInventoryData.ThirdItem += int.Parse(quantity);
                            currInventoryData.Message    = "Received " + quantity + " large items";
                            updated = true;
                        }
                        else
                        {
                            currInventoryData.Help = true;
                            updated = true;
                        }
                    }
                }

                if (updated)
                {
                    var updateRoom = TableOperation.Replace(currInventoryData as InventoryData);
                    await table.ExecuteAsync(updateRoom);

                    log.LogInformation("successfully updated the record");
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(currInventoryData, Formatting.Indented), Encoding.UTF8, "application/json")
                });
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("Failed to process request")
                });
            }
        }