Exemplo n.º 1
0
        /// <summary>
        /// Implementation of <see cref="ICustomerCommands.SetCustomerAccount(Guid, string, string)"/>
        /// </summary>
        /// <param name="customerId">The customer id</param>
        /// <param name="userName">The username</param>
        /// <param name="password">The password</param>
        /// <returns></returns>
        public async Task SetCustomerAccount(Guid customerId, string userName, string password)
        {
            if (customerId == Guid.Empty)
            {
                throw new ArgumentException("value cannot be empty", nameof(customerId));
            }

            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("value cannot be empty", nameof(userName));
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException("value cannot be empty", nameof(password));
            }

            var customer = await Repository.GetByKeyAsync <Customer>(customerId);

            if (customer == null)
            {
                throw new ArgumentOutOfRangeException(nameof(customerId));
            }

            try
            {
                var userId = await AuthClient.FindOrRegisterAccount(userName, password);

                customer.SetAccount(userId, userName);

                await Repository.SaveChangesAsync();

                var @event = new CustomerAccountSetEvent(customerId, userName);
                EventBus.RaiseEvent(@event);
            }
            catch
            {
                throw;
            }
        }