Beispiel #1
0
        public async Task Handle(RegisterSuperheroCommand notification, CancellationToken cancellationToken)
        {
            if (!notification.IsValid())
            {
                NotifyValidationErrors(notification);
                return;
            }

            List <string> validationErrors = new List <string>();

            if (await _superheroRepository.GetByNameAsync(notification.Name) != null)
            {
                validationErrors.Add("There's already a superhero with this name");
            }

            if (await _protectionAreaRepository.GetAsync(notification.ProtectionAreaId) == null)
            {
                validationErrors.Add("Invalid protection area id");
            }

            if (validationErrors.Count > 0)
            {
                foreach (var error in validationErrors)
                {
                    await _bus.RaiseEvent(new DomainNotification(notification.MessageType, error));
                }
                return;
            }

            var superhero = new Superhero(Guid.NewGuid(),
                                          notification.Name,
                                          notification.Alias,
                                          notification.ProtectionAreaId,
                                          DateTime.Now);

            _superheroRepository.Add(superhero);

            if (await CommitAsync())
            {
                await _auditEventService.Subscribe(new AuditEvent(
                                                       Guid.NewGuid(),
                                                       nameof(Superhero),
                                                       superhero.Id,
                                                       notification.Username,
                                                       AuditEventAction.Create
                                                       ));
            }
        }