public async Task HandleAsync(CreateCustomerCommand command)
        {
            // TODO: zastnowić się jeszcze nad logiką

            var customer = await _customersRepository.GetAsync(command.Id);

            if (customer is null)
            {
                if (string.IsNullOrWhiteSpace(command.Email))
                {
                    throw new MyShopException("email_invalid",
                                              $"Email can not be empty.");
                }
                customer = new Customer(command.Id, command.Email);
                await _customersRepository.AddAsync(customer);

                var newCart = new Cart(command.Id);
                await _cartsRepository.AddAsync(newCart);
            }

            if (customer.Completed)
            {
                throw new MyShopException("customer_already_completed",
                                          $"Customer accoutn was already created for user with id: '{command.Id}.'");
            }

            customer.Complete(command.FirstName, command.LastName, command.Address);
            await _customersRepository.UpdateAsync(customer);
        }
        public async Task HandleAsync(CreateCustomer command, ICorrelationContext context)
        {
            var customer = await _customersRepository.GetAsync(command.Id);

            if (customer.Completed)
            {
                throw new DShopException(Codes.CustomerAlreadyCompleted,
                                         $"Customer account was already created for user with id: '{command.Id}'.");
            }

            customer.Complete(command.FirstName, command.LastName, command.Address, command.Country);
            await _customersRepository.UpdateAsync(customer);

            var cart = new Cart(command.Id);
            await _cartsRepository.AddAsync(cart);

            await _busPublisher.PublishAsync(new CustomerCreated(command.Id, customer.Email,
                                                                 command.FirstName, command.LastName, command.Address, command.Country), context);
        }
Beispiel #3
0
        public async Task HandleAsync(SignUpCommand command)
        {
            var user = await _usersRepository.GetAsync(command.Email);

            if (user != null)
            {
                throw new MyShopException("email_in_use",
                                          $"Email: '{command.Email}' as already in use.");
            }

            user = new User(command.Id, command.Email, Role.User);
            user.SetPassword(command.Password, _passwordHasher);

            await _usersRepository.AddAsync(user);

            var newCustomer = new Customer(command.Id, command.Email);
            await _customersRepository.AddAsync(newCustomer);

            var newCart = new Cart(command.Id);
            await _cartsRepository.AddAsync(newCart);
        }