public async Task <ActionResult> CollectFine(SpeedingViolation speedingViolation, [FromServices] DaprClient daprClient)
        {
            decimal fine = _fineCalculator.CalculateFine(_fineCalculatorLicenseKey, speedingViolation.ViolationInKmh);

            // get owner info
            var vehicleInfo = await _vehicleRegistrationService.GetVehicleInfo(speedingViolation.VehicleId);

            // log fine
            string fineString = fine == 0 ? "tbd by the prosecutor" : $"{fine} Euro";

            _logger.LogInformation($"Sent speeding ticket to {vehicleInfo.OwnerName}. " +
                                   $"Road: {speedingViolation.RoadId}, Licensenumber: {speedingViolation.VehicleId}, " +
                                   $"Vehicle: {vehicleInfo.Brand} {vehicleInfo.Model}, " +
                                   $"Violation: {speedingViolation.ViolationInKmh} Km/h, Fine: {fineString}, " +
                                   $"On: {speedingViolation.Timestamp.ToString("dd-MM-yyyy")} " +
                                   $"at {speedingViolation.Timestamp.ToString("hh:mm:ss")}.");

            // send fine by email
            var body     = EmailUtils.CreateEmailBody(speedingViolation, vehicleInfo, fineString);
            var metadata = new Dictionary <string, string>
            {
                ["emailFrom"] = "*****@*****.**",
                ["emailTo"]   = vehicleInfo.OwnerEmail,
                ["subject"]   = $"Speeding violation on the {speedingViolation.RoadId}"
            };
            await daprClient.InvokeBindingAsync("sendmail", "create", body, metadata);

            return(Ok());
        }
예제 #2
0
        public async Task <ActionResult> CollectFine(SpeedingViolation speedingViolation, [FromServices] DaprClient daprClient)
        // Replace the SpeedingViolation with a JsonDocument parameter. Doing so will enable Dapr pub/sub messaging.
        //public async Task<ActionResult> CollectFine([FromBody] System.Text.Json.JsonDocument cloudevent)
        {
            // Remove CloudEvent parsing code when using ASP.NET Core Dapr client
            // // Extract SpeedingViolation data from the cloudevent parameter and assign to SpeedingViolation type.
            // var data = cloudevent.RootElement.GetProperty("data");

            // // Transform raw data into a SpeedingViolation object
            // var speedingViolation = new SpeedingViolation
            // {
            //     VehicleId = data.GetProperty("vehicleId").GetString(),
            //     RoadId = data.GetProperty("roadId").GetString(),
            //     Timestamp = data.GetProperty("timestamp").GetDateTime(),
            //     ViolationInKmh = data.GetProperty("violationInKmh").GetInt32()
            // };

            decimal fine = _fineCalculator.CalculateFine(_fineCalculatorLicenseKey, speedingViolation.ViolationInKmh);

            // get owner info
            var vehicleInfo = await _vehicleRegistrationService.GetVehicleInfo(speedingViolation.VehicleId);

            // log fine
            string fineString = fine == 0 ? "tbd by the prosecutor" : $"{fine} Euro";

            _logger.LogInformation($"Sent speeding ticket to {vehicleInfo.OwnerName}. " +
                                   $"Road: {speedingViolation.RoadId}, Licensenumber: {speedingViolation.VehicleId}, " +
                                   $"Vehicle: {vehicleInfo.Brand} {vehicleInfo.Model}, " +
                                   $"Violation: {speedingViolation.ViolationInKmh} Km/h, Fine: {fineString}, " +
                                   $"On: {speedingViolation.Timestamp.ToString("dd-MM-yyyy")} " +
                                   $"at {speedingViolation.Timestamp.ToString("hh:mm:ss")}.");

            // send fine by email
            // Create email body with custom EmailUtility class
            var body = EmailUtils.CreateEmailBody(speedingViolation, vehicleInfo, fineString);

            // Specify email metadata
            var metadata = new Dictionary <string, string>
            {
                ["emailFrom"] = "*****@*****.**",
                ["emailTo"]   = vehicleInfo.OwnerEmail,
                ["subject"]   = $"Speeding violation on the {speedingViolation.RoadId}"
            };

            // Call email server with Dapr output binding
            await daprClient.InvokeBindingAsync("sendmail", "create", body, metadata);

            return(Ok());
        }