Ejemplo n.º 1
0
        public async Task <bool> Handle(CreateIdentityResourceCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                NotifyValidationErrors(request);
                return(false);
            }

            var existed = await _identityResourceRepository.FirstOrDefaultAsync(c => c.Name == request.IdentityResource.Name) != null;

            if (existed)
            {
                await _bus.RaiseEvent(new DomainNotification("key_already_existed", $"IdentityResource named {request.IdentityResource.Name} already existed"));

                return(false);
            }

            var resource = request.IdentityResource.ToEntity();
            await _identityResourceRepository.AddAsync(resource);

            if (Commit())
            {
                await _bus.RaiseEvent(new IdentityResourceCreatedEvent(resource.Id, resource.Name));

                return(true);
            }
            return(false);
        }