public async Task <Guid> HandleAsync(CreateUserCommand cmd, CancellationToken ct)
        {
            var email = cmd.Email.Trim().ToLowerInvariant();

            using (_logger.BeginScope("Email:'{email}'", email))
            {
                _logger.LogDebug("Creating user");

                var user = new User
                {
                    Id    = Guid.NewGuid(),
                    Email = email,
                    Name  = cmd.Name
                };

                if (_store.TryAdd(email, user))
                {
                    await _mediator.BroadcastAsync(new UserCreatedEvent(user.Id), ct);

                    return(user.Id);
                }

                throw new InvalidOperationException($"Duplicated email '{email}'");
            }
        }
        public async Task <CreateProductResult> HandleAsync(CreateProductCommand cmd, CancellationToken ct)
        {
            var products = _context.Set <ProductEntity>();

            if (await products.AnyAsync(p => p.Code == cmd.Code, ct))
            {
                throw new InvalidOperationException($"Product code '{cmd.Code}' already exists");
            }

            var externalId = Guid.NewGuid();
            await products.AddAsync(new ProductEntity
            {
                ExternalId = externalId,
                Code       = cmd.Code,
                Name       = cmd.Name,
                Price      = cmd.Price
            }, ct);

            await _mediator.BroadcastAsync(new CreatedProductEvent
            {
                ExternalId = externalId,
                Code       = cmd.Code,
                Name       = cmd.Name,
                Price      = cmd.Price,
                CreatedBy  = cmd.CreatedBy
            }, ct);

            //await _context.SaveChangesAsync(ct);

            return(new CreateProductResult
            {
                Id = externalId
            });
        }
        public async Task HandleAsync(DeleteUserCommand cmd, CancellationToken ct)
        {
            var email = cmd.Email.Trim().ToLowerInvariant();

            using (_logger.BeginScope("Email:'{email}'", email))
            {
                _logger.LogDebug("Deleting user");

                if (_store.TryRemove(email, out var user))
                {
                    await _mediator.BroadcastAsync(new UserDeletedEvent(user.Id), ct);
                }
                else
                {
                    throw new InvalidOperationException($"User with email '{email}' not found");
                }
            }
        }
Beispiel #4
0
        public async Task HandleAsync(DeleteProductCommand cmd, CancellationToken ct)
        {
            var products = _context.Set <ProductEntity>();

            var product = await products.SingleOrDefaultAsync(p => p.ExternalId == cmd.ProductId, ct);

            if (product == null)
            {
                throw new InvalidOperationException($"Product '{cmd.ProductId}' not found");
            }

            products.Remove(product);

            await _context.SaveChangesAsync(ct);

            await _mediator.BroadcastAsync(new DeletedProductEvent
            {
                ProductId = cmd.ProductId
            }, ct);
        }
Beispiel #5
0
        public async Task HandleAsync(UpdateProductCommand cmd, CancellationToken ct)
        {
            var products = _context.Set <ProductEntity>();

            var product = await products.SingleOrDefaultAsync(p => p.ExternalId == cmd.ProductId, ct);

            if (product == null)
            {
                throw new InvalidOperationException($"Product '{cmd.ProductId}' not found");
            }

            if (product.Code == cmd.Code &&
                product.Name == cmd.Name &&
                product.Price == cmd.Price)
            {
                return;
            }

            var previousCode  = product.Code;
            var previousName  = product.Name;
            var previousPrice = product.Price;

            product.Code  = cmd.Code;
            product.Name  = cmd.Name;
            product.Price = cmd.Price;

            await _mediator.BroadcastAsync(new UpdatedProductEvent
            {
                ProductId     = cmd.ProductId,
                PreviousCode  = previousCode,
                PreviousName  = previousName,
                PreviousPrice = previousPrice,
                CurrentCode   = product.Code,
                CurrentName   = product.Name,
                CurrentPrice  = product.Price,
                CreatedBy     = cmd.CreatedBy
            }, ct);

            //await _context.SaveChangesAsync(ct);
        }
Beispiel #6
0
 public async Task EventAsync <TEvent> (TEvent query, CancellationToken cancellationToken = default(CancellationToken)) where TEvent : IEvent
 {
     await _mediator.BroadcastAsync(query, cancellationToken);
 }
Beispiel #7
0
 async Task IParticipant.SendAsync(string message)
 {
     Console.WriteLine($" Participant 1 Send Message : {message}");
     await mediator?.BroadcastAsync(message, this);
 }
 public async Task PublishAsync <TEvent>(TEvent @event, CancellationToken ct = default(CancellationToken)) where TEvent : IEvent
 {
     await _mediator.BroadcastAsync(@event, ct);
 }