|
1 | 1 | using EventSourcingOnAzureFunctions.Common.Binding;
|
2 | 2 | using EventSourcingOnAzureFunctions.Common.CQRS;
|
3 | 3 | using EventSourcingOnAzureFunctions.Common.EventSourcing;
|
| 4 | +using Microsoft.Azure.WebJobs; |
| 5 | +using Microsoft.Azure.WebJobs.Extensions.Http; |
| 6 | +using System; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
4 | 10 |
|
5 | 11 | namespace RetailBank.AzureFunctionApp.Transfer.Functions
|
6 | 12 | {
|
7 | 13 | public class TransferCommands
|
8 | 14 | {
|
9 | 15 |
|
10 | 16 | //Transfer money
|
| 17 | + /// <summary> |
| 18 | + /// Initiate a transfer of money between bank accounts |
| 19 | + /// </summary> |
| 20 | + /// <param name="transferidentifier"> |
| 21 | + /// (Optional) A unique identifier to use to identify the transfer - if not set a GUID will be supplied |
| 22 | + /// </param> |
| 23 | + [FunctionName(nameof(TransferMoneyCommand))] |
| 24 | + public static async Task<HttpResponseMessage> TransferMoneyCommand( |
| 25 | + [HttpTrigger(AuthorizationLevel.Function, "POST", Route = @"TransferMoney/{transferidentifier?}")] HttpRequestMessage req, |
| 26 | + string transferidentifier) |
| 27 | + { |
| 28 | + |
| 29 | + #region Tracing telemetry |
| 30 | + Activity.Current.AddTag("Transfer Unique Identifier", transferidentifier); |
| 31 | + #endregion |
| 32 | + |
| 33 | + // Set the start time for how long it took to process the message |
| 34 | + DateTime startTime = DateTime.UtcNow; |
| 35 | + |
| 36 | + if (string.IsNullOrWhiteSpace(transferidentifier)) |
| 37 | + { |
| 38 | + transferidentifier = Guid.NewGuid().ToString("N"); |
| 39 | + } |
| 40 | + |
| 41 | + // Get request body |
| 42 | + MoneyTransferData data = await req.Content.ReadAsAsync<MoneyTransferData>(); |
| 43 | + |
| 44 | + if (data == null) |
| 45 | + { |
| 46 | + return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.BadRequest, |
| 47 | + FunctionResponse.CreateResponse(startTime, |
| 48 | + false, |
| 49 | + $"No transfer request body/data supplied"), |
| 50 | + FunctionResponse.MEDIA_TYPE); |
| 51 | + } |
| 52 | + |
| 53 | + if (string.IsNullOrWhiteSpace(data.SourceAccountNumber)) |
| 54 | + { |
| 55 | + return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.BadRequest, |
| 56 | + FunctionResponse.CreateResponse(startTime, |
| 57 | + false, |
| 58 | + $"No source account for money transfer"), |
| 59 | + FunctionResponse.MEDIA_TYPE); |
| 60 | + } |
| 61 | + |
| 62 | + if (string.IsNullOrWhiteSpace(data.TargetAccountNumber)) |
| 63 | + { |
| 64 | + return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.BadRequest, |
| 65 | + FunctionResponse.CreateResponse(startTime, |
| 66 | + false, |
| 67 | + $"No target account for money transfer"), |
| 68 | + FunctionResponse.MEDIA_TYPE); |
| 69 | + } |
| 70 | + |
| 71 | + // Create a transfer event stream and add the Transfer Initiated event to it |
| 72 | + Command cmdTransfer = new Command(new CommandAttribute("Bank", "Transfer Money", transferidentifier)); |
| 73 | + |
| 74 | + // set the parameters |
| 75 | + await cmdTransfer.SetParameter(nameof(data.SourceAccountNumber), data.SourceAccountNumber); |
| 76 | + await cmdTransfer.SetParameter(nameof(data.TargetAccountNumber), data.TargetAccountNumber); |
| 77 | + await cmdTransfer.SetParameter(nameof(data.Amount), data.Amount); |
| 78 | + |
| 79 | + // Start the first step of the command |
| 80 | + await cmdTransfer.InitiateStep("Initiate Transfer"); |
| 81 | + |
| 82 | + // Return that the command has been initiated... |
| 83 | + return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.OK, |
| 84 | + FunctionResponse.CreateResponse(startTime, |
| 85 | + false, |
| 86 | + $"Money transfer initiated - transfer id is {transferidentifier}"), |
| 87 | + FunctionResponse.MEDIA_TYPE); |
| 88 | + } |
11 | 89 |
|
12 | 90 | //Cancel in-progress transfer
|
| 91 | + [FunctionName(nameof(CancelMoneyTransferCommand))] |
| 92 | + public static async Task<HttpResponseMessage> CancelMoneyTransferCommand( |
| 93 | + [HttpTrigger(AuthorizationLevel.Function, "POST", Route = @"CancelMoneyTransfer/{transferidentifier}")] HttpRequestMessage req, |
| 94 | + string transferidentifier, |
| 95 | + [Command("Bank", "Transfer Money", "{transferidentifier}")] Command cmdTransfer |
| 96 | + ) |
| 97 | + { |
| 98 | + |
| 99 | + #region Tracing telemetry |
| 100 | + Activity.Current.AddTag("Transfer Unique Identifier", transferidentifier); |
| 101 | + #endregion |
| 102 | + |
| 103 | + // Set the start time for how long it took to process the message |
| 104 | + DateTime startTime = DateTime.UtcNow; |
| 105 | + |
| 106 | + await cmdTransfer.InitiateStep("Cancel Transfer"); |
13 | 107 |
|
| 108 | + return req.CreateResponse<FunctionResponse>(System.Net.HttpStatusCode.OK, |
| 109 | + FunctionResponse.CreateResponse(startTime, |
| 110 | + false, |
| 111 | + $"Money transfer cancelation requested - transfer id is {transferidentifier}"), |
| 112 | + FunctionResponse.MEDIA_TYPE); |
| 113 | + } |
14 | 114 | }
|
15 | 115 | }
|
0 commit comments