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);
        }
Example #2
0
        public Task <Unit> Handle(RegisterNewCustomer message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Unit.Task);
            }

            var customer = Mapper.Map <Customer>(message);

            //if (_customersRepository.GetByEmailAsync(message.AddressEmail) != null)
            //{
            //    _bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
            //    return Unit.Task;
            //}

            _customersRepository.AddAsync(customer);

            if (Commit())
            {
                _bus.RaiseEvent(new CustomerRegisteredEvent(customer.Id, customer.Name.ToString(), customer.Email.ToString(), customer.BirthDate));
            }

            return(Unit.Task);
        }
Example #3
0
        public async Task HandleAsync(CreateCustomer command, ICorrelationContext context)
        {
            var customer = new Customer(command.Id, command.FirstName, command.SecondName,
                                        command.Surname, command.Age, command.Address);

            await _customersRepository.AddAsync(customer);

            await _busPublisher.PublishAsync(new CustomerCreated(command.Id), context);
        }
        public async Task <string> Handle(RegisterAccountCommand command, CancellationToken cancellationToken)
        {
            var userId = await _identityService.AddUserAsync(command.Username, command.Email, command.Password,
                                                             new[] { UserRoles.Customer });

            var customer = new Customer(userId);
            await _customersRepository.AddAsync(customer);

            await _unitOfWork.SaveAsync();

            return(userId);
        }
Example #5
0
        public async Task <IActionResult> Create([FromBody] Customer customer)
        {
            try
            {
                await _customersRepository.AddAsync(customer);

                return(Ok());
            }
            catch (UniqueNameException ex)
            {
                ModelState.AddModelError(ex.PropertyName, ex.Message);
                return(BadRequest(ModelState));
            }
        }
Example #6
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);
        }
Example #7
0
        public async Task <OperationResult> AddAsync(Customer customer)
        {
            // TODO: PlanId should be null already
            if (customer.PlanId == Guid.Empty)
            {
                customer.RemovePlan();
            }

            var validationResult = _validator.Validate(customer);

            if (!validationResult.IsValid)
            {
                var errors = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
                return(new OperationResult(false, customer, errors));
            }

            await _customersRepository.AddAsync(customer);

            await _unitOfWork.CommitAsync();

            return(new OperationResult(true, customer, null));
        }
Example #8
0
 public async Task HandleAsync(SignedUp @event, ICorrelationContext context)
 {
     var customer = new Customer(@event.UserId, @event.Email);
     await _customersRepository.AddAsync(customer);
 }
 public async Task HandleAsync(CustomerCreated @event, ICorrelationContext context) =>
 await _customersRepository.AddAsync(new Customer(@event.Id, @event.Email,
                                                  @event.FirstName, @event.LastName, @event.Address, @event.Country));
        public async Task HandleAsync(CustomerCreated @event, ICorrelationContext context)
        {
            await _customersRepository.AddAsync(new Customer(@event.Id, @event.Email));

            _logger.LogInformation($"Created customer with id: '{@event.Id}'.");
        }