private static bool IsValidMakeAndModel(RegisterAutoCommand command,
                                         AutoInfo[] validModels)
 {
     return(validModels.Any(
                m => m.Make == command.Make &&
                m.Model == command.Model));
 }
        private Task NotifyPassedRegistration(
            RegistrationStatus status,
            RegisterAutoCommand command)
        {
            if (status.IsSuccess)
            {
                var passedEvent = new RegistrationPassedEvent(
                    status.ReferenceNumber, command.State);

                return(_messaging.PublishAsync(passedEvent));
            }
            return(Task.CompletedTask);
        }
        private Task NotifyFailedRegistration(
            RegistrationStatus status,
            RegisterAutoCommand command)
        {
            if (!status.IsSuccess)
            {
                var failedEvent = new RegistrationFailedEvent(
                    status.ReferenceNumber,
                    command.Make, command.Model,
                    command.Year);

                return(_messaging.PublishAsync(failedEvent));
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> RegisterAuto(
            [FromBody] AutoRegistrationModel model)
        {
            var command = new RegisterAutoCommand(
                model.Make,
                model.Model,
                model.Year,
                model.State);

            var status = await _messaging.SendAsync(command);

            if (!status.IsSuccess)
            {
                return(BadRequest("Registration Failed"));
            }

            return(Ok(status));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> RegisterAuto(
            [FromBody] AutoRegistrationModel model)
        {
            // Adapt the HTTP request model into command...
            var command = new RegisterAutoCommand(
                model.Make,
                model.Model,
                model.Year,
                model.State);

            // Send command to application and adapt result
            // to the HTTP response:
            var status = await _messaging.SendAsync(command);

            if (!status.IsSuccess)
            {
                return(BadRequest("Registration Failed"));
            }

            return(Ok(status));
        }
        public async Task <RegistrationStatus> RegisterAuto(RegisterAutoCommand command)
        {
            if (command.Year < 2000)
            {
                throw new InvalidOperationException(
                          "Cars must be newer than the 2000 model year.");
            }

            AutoInfo[] validModels = await _adapter.GetValidModelsAsync(command.Year);

            var status = new RegistrationStatus {
                IsSuccess         = IsValidMakeAndModel(command, validModels),
                ReferenceNumber   = Guid.NewGuid().ToString(),
                DateAccountActive = DateTime.UtcNow
            };

            await NotifyPassedRegistration(status, command);
            await NotifyFailedRegistration(status, command);

            return(status);
        }