Exemplo n.º 1
0
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <Unit> Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Unit.Task);
            }

            var customer         = new Entitys.Customer(message.Id, message.Name, message.Email, message.BirthDate);
            var existingCustomer = _customerRepository.GetByEmail(customer.Email);

            ////如果按邮箱查出来的customer的id和传过来的不相等
            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                //和查出来的对象不相等
                if (!existingCustomer.Equals(customer))
                {
                    //发布通知,这个邮箱被占用了
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                    return(Unit.Task);
                }
            }
            //否则更新
            _customerRepository.Update(customer);


            if (Commit())
            {
                //提交成功,发布领域事件
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
            }

            return(Unit.Task);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Handle(UpdateCustomerCommand message)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return;
            }

            var customer         = new Models.Customer(message.Id, message.Name, message.Email, message.BirthDate);
            var existingCustomer = _customerRepository.GetByEmail(customer.Email);

            if (existingCustomer != null)
            {
                if (!existingCustomer.Equals(customer))
                {
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                    return;
                }
            }

            _customerRepository.Update(customer);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
            }
        }
        public Task Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.CompletedTask);
            }

            var customer         = new Customer(message.Id, message.Name, message.Email, message.BirthDate);
            var existingCustomer = _customerRepository.GetByEmail(customer.Email);

            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                if (!existingCustomer.Equals(customer))
                {
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                    return(Task.CompletedTask);
                }
            }

            _customerRepository.Update(customer);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
            }

            return(Task.CompletedTask);
        }
        public Task Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.CompletedTask);
            }

            var customer         = new Customer(message.Id, message.Name, message.IsActive);
            var existingCustomer = _customerRepository.GetByName(customer.Name);

            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                if (!existingCustomer.Equals(customer))
                {
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer name has already been exists."));
                    return(Task.CompletedTask);
                }
            }

            _customerRepository.Update(customer);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.IsActive));
            }

            return(Task.CompletedTask);
        }
        public void Handle(UpdateCustomerCommand message)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return;
            }

            var customer         = new Customer(message.Id, message.FirstName, message.LastName, message.Birthday, message.Cpf, message.Phone);
            var existingCustomer = _customerRepository.GetByEmail(customer.User.Email);

            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                if (!existingCustomer.Equals(customer))
                {
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                    return;
                }
            }

            _customerRepository.Update(customer);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, message.FirstName, message.LastName, message.Phone, message.Cpf, message.Birthday, message.User, message.Address));
            }
        }
Exemplo n.º 6
0
        public async Task <ValidationResult> Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                return(message.ValidationResult);
            }

            var customer         = new Customer(message.Id, message.Name, message.Email, message.BirthDate);
            var existingCustomer = await _customerRepository.GetByEmail(customer.Email);

            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                if (!existingCustomer.Equals(customer))
                {
                    AddError("The customer e-mail has already been taken.");
                    return(ValidationResult);
                }
            }

            customer.AddDomainEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));

            _customerRepository.Update(customer);

            return(await Commit(_customerRepository.UnitOfWork));
        }
Exemplo n.º 7
0
 public Task <object> Handle(UpdateCustomerCommand command, CancellationToken cancellationToken)
 {
     if (!command.IsValid())
     {
         NotifyValidationErrors(command);
     }
     else
     {
         AddressModel addressModel = _AddressRepository.GetByLatLng(command.Lat, command.Lng);
         if (addressModel == null)
         {
             Entities.Address address = new Entities.Address(
                 null,
                 new City(command.City),
                 new Country(command.Country),
                 new District(command.District),
                 new Lat(command.Lat),
                 new Lng(command.Lng),
                 new Street(command.Street),
                 new StreetNumber(command.StreetNumber)
                 );
             addressModel = _AddressRepository.Add(address);
         }
         Entities.Customer u = new Entities.Customer(
             new Identity((uint)command.Id),
             new Name(command.Name),
             new Note(command.Note),
             new CartCode(command.CartCode),
             new PhoneNumber(command.PhoneNumber),
             new Email(command.Email),
             new Code(command.Code),
             new Status(command.Status),
             new Entities.Address(
                 new Identity((uint)addressModel.ID),
                 new City(command.City),
                 new Country(command.Country),
                 new District(command.District),
                 new Lat(command.Lat),
                 new Lng(command.Lng),
                 new Street(command.Street),
                 new StreetNumber(command.StreetNumber)
                 )
             );
         bool resullt = _CustomerRepository.Update(u);
         if (!resullt)
         {
             _bus.RaiseEvent(new DomainNotification("Customer", "Server error", NotificationCode.Error));
         }
         return(Task.FromResult(resullt as object));
     }
     return(Task.FromResult(false as object));
 }
        public Task <bool> Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            var customer = new Customer(message.Id, message.Name, message.Address);

            _customerRepository.Update(customer);

            if (Commit())
            {
                _bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Address));
            }

            return(Task.FromResult(true));
        }
Exemplo n.º 9
0
        public async Task <ICommandQuery> Handle(UpdateCustomerCommand command, CancellationToken cancellationToken)
        {
            // VALIDATE COMMAND
            if (!command.IsValid())
            {
                return(command);
            }

            // VALIDATE CUSTOMER ON REPOSITORY
            var customer = _mapper.Map <Customer>(command);

            if (!command.IsRepositoryValid(customer, _customerRepo))
            {
                return(command);
            }

            // GET CUSTOMER
            var oldCustomer = await _customerRepo.GetCustomer(command.IdCustomer);

            customer.SetIdUser(oldCustomer.IdUser);

            // GET USER ID FROM CUSTOMER
            var user = _mapper.Map <User>(command);

            user.SetId(customer.IdUser);

            // VALIDATE USER ON REPOSITORY
            if (!command.IsRepositoryValid(user, _userRepo))
            {
                return(command);
            }

            // UPDATE USER
            await _userRepo.UpdateUser(user);

            // UPDATE CUSTOMER
            await _customerRepo.UpdateCustomer(customer);

            return(command);
        }
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public Task Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                foreach (var error in request.ValidationResult.Errors)
                {
                    _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, error.ErrorMessage));
                }
                return(Task.CompletedTask);
            }

            var country = _countryRepository.FindById(request.CountryId);

            if (country == null)
            {
                _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, @"Country id does not exit in system"));
                return(Task.CompletedTask);
            }

            var existingCustomer = _customerRepository.FindOne(request.Id);

            if (existingCustomer == null)
            {
                _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, @"The customer does not exit in system"));
                return(Task.CompletedTask);
            }

            //edit customer
            var customer = Customer.UpdateWithoutEmailAndPassword(request.Id, request.FirstName, request.LastName, country);

            _customerRepository.UpdateWithoutEmailAndPassword(customer);
            _unitOfWork.Commit();

            _eventDispatcher.RaiseEvent(new CustomerUpdatedEvent {
                Customer = customer
            });

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task <Unit> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                foreach (var error in request.ValidationResult.Errors)
                {
                    await _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, error.ErrorMessage));
                }
                return(Unit.Value);
            }

            var existingCustomer = _customerRepository.FindOne(request.Id);

            if (existingCustomer == null)
            {
                await _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, @"The customer does not exit in system"));

                return(Unit.Value);
            }

            var customer = new Customer();

            if (string.IsNullOrEmpty(request.Email) && string.IsNullOrEmpty(request.Password))
            {
                customer = Customer.UpdateWithoutEmailAndPassword(request.Id, request.FirstName, request.LastName);
                _customerRepository.UpdateWithoutEmailAndPassword(customer);
            }
            else
            {
                customer = Customer.Update(request.Id, request.FirstName, request.LastName, request.Email, request.Password);
                _customerRepository.Update(customer);
            }

            _unitOfWork.Commit();

            await _eventDispatcher.RaiseEvent(new CustomerUpdatedEvent { Customer = customer });

            return(Unit.Value);
        }