public async Task <IActionResult> EndRental([FromBody] RentalEnd end) { #region Check prerequisites if (string.IsNullOrEmpty(end.CustomerID)) { return(BadRequest(new ErrorResult { Description = "Customer ID missing" })); } if (string.IsNullOrEmpty(end.BikeID)) { return(BadRequest(new ErrorResult { Description = "Bike ID missing" })); } #endregion // Remember end of rental var endOfRental = DateTime.UtcNow; // Simulate Data Access (e.g. read rental data from DB) await Task.Delay(250); // Simulate rental that started a while ago var beginOfRental = endOfRental.AddHours(Math.Round(new Random().NextDouble() * (-3), 1)); // Use rating service to calculate costs var totalCosts = rating.CalculateTotalCosts(endOfRental - beginOfRental); // Build result object var result = new RentalEndResult { CustomerID = end.CustomerID, BikeID = end.BikeID, EndOfRental = endOfRental, BeginOfRental = beginOfRental, TotalCosts = totalCosts }; // If service bus sender is available, send notification to service bus if (serviceBusSender != null) { await serviceBusSender.SendRentalEndResult(result); } return(CreatedAtRoute("getRental", new { id = "BR999" }, result)); }
public async Task SendRentalEndResult(RentalEndResult result) { var topicClient = new TopicClient(await keyVaultReader.GetSecretAsync("ServiceBus"), "bike-rental-end"); var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result))); await topicClient.SendAsync(message); }