public static async Task <HttpResponseMessage> DepositMoneyRun(
            [HttpTrigger(AuthorizationLevel.Function, "POST", Route = @"DepositMoney/{accountnumber}")] HttpRequestMessage req,
            string accountnumber,
            [EventStream("Bank", "Account", "{accountnumber}")]  EventStream bankAccountEvents)
        {
            // Set the start time for how long it took to process the message
            DateTime startTime = DateTime.UtcNow;

            if (!await bankAccountEvents.Exists())
            {
                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.NotFound,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             true,
                                                                                             $"Account {accountnumber} does not exist"),
                                                             FunctionResponse.MEDIA_TYPE));
            }
            else
            {
                // get the request body...
                MoneyDepositData data = await req.Content.ReadAsAsync <MoneyDepositData>();

                // create a deposited event
                DateTime dateDeposited = DateTime.UtcNow;
                Account.Events.MoneyDeposited evDeposited = new Account.Events.MoneyDeposited()
                {
                    LoggedDepositDate = dateDeposited,
                    AmountDeposited   = data.DepositAmount,
                    Commentary        = data.Commentary,
                    Source            = data.Source
                };

                await bankAccountEvents.AppendEvent(evDeposited);

                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.OK,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             false,
                                                                                             $"{data.DepositAmount} deposited to account {accountnumber} "),
                                                             FunctionResponse.MEDIA_TYPE));
            }
        }
        public static async Task <HttpResponseMessage> OpenAccountRun(
            [HttpTrigger(AuthorizationLevel.Function, "POST", Route = @"OpenAccount/{accountnumber}")] HttpRequestMessage req,
            string accountnumber,
            [EventStream("Bank", "Account", "{accountnumber}")]  EventStream bankAccountEvents)
        {
            // Set the start time for how long it took to process the message
            DateTime startTime = DateTime.UtcNow;

            if (await bankAccountEvents.Exists())
            {
                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Forbidden,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             true,
                                                                                             $"Account {accountnumber} already exists"),
                                                             FunctionResponse.MEDIA_TYPE));
            }
            else
            {
                // Get request body
                AccountOpeningData data = await req.Content.ReadAsAsync <AccountOpeningData>();

                // Append a "created" event
                DateTime dateCreated            = DateTime.UtcNow;
                Account.Events.Opened evtOpened = new Account.Events.Opened()
                {
                    LoggedOpeningDate = dateCreated
                };
                if (!string.IsNullOrWhiteSpace(data.Commentary))
                {
                    evtOpened.Commentary = data.Commentary;
                }
                try
                {
                    await bankAccountEvents.AppendEvent(evtOpened,
                                                        streamConstraint : EventStreamExistenceConstraint.MustBeNew
                                                        );
                }
                catch (EventStreamWriteException exWrite)
                {
                    return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Conflict,
                                                                 FunctionResponse.CreateResponse(startTime,
                                                                                                 true,
                                                                                                 $"Account {accountnumber} had a conflict error on creation {exWrite.Message }"),
                                                                 FunctionResponse.MEDIA_TYPE));
                }

                // If there is an initial deposit in the account opening data, append a "deposit" event
                if (data.OpeningBalance.HasValue)
                {
                    Account.Events.MoneyDeposited evtInitialDeposit = new Account.Events.MoneyDeposited()
                    {
                        AmountDeposited   = data.OpeningBalance.Value,
                        LoggedDepositDate = dateCreated,
                        Commentary        = "Opening deposit"
                    };
                    await bankAccountEvents.AppendEvent(evtInitialDeposit);
                }

                // If there is a beneficiary in the account opening data append a "beneficiary set" event
                if (!string.IsNullOrEmpty(data.ClientName))
                {
                    Account.Events.BeneficiarySet evtBeneficiary = new Account.Events.BeneficiarySet()
                    {
                        BeneficiaryName = data.ClientName
                    };
                    await bankAccountEvents.AppendEvent(evtBeneficiary);
                }

                return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.Created,
                                                             FunctionResponse.CreateResponse(startTime,
                                                                                             false,
                                                                                             $"Account { accountnumber} created"),
                                                             FunctionResponse.MEDIA_TYPE));
            }
        }