public static async Task <HttpResponseMessage> SetBeneficialOwnerRun( [HttpTrigger(AuthorizationLevel.Function, "POST", Route = "SetBeneficialOwner/{accountnumber}/{ownername}")] HttpRequestMessage req, string accountnumber, string ownername, [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()) { if (!string.IsNullOrEmpty(ownername)) { Account.Events.BeneficiarySet evtBeneficiary = new Account.Events.BeneficiarySet() { BeneficiaryName = ownername }; await bankAccountEvents.AppendEvent(evtBeneficiary); } return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.OK, FunctionResponse.CreateResponse(startTime, false, $"Beneficial owner of account {accountnumber} set"), FunctionResponse.MEDIA_TYPE)); } else { return(req.CreateResponse <FunctionResponse>(System.Net.HttpStatusCode.OK, FunctionResponse.CreateResponse(startTime, true, $"Account {accountnumber} does not exist"), 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)); } }