public async Task <AddCustomerAddressCommandResponse> Handle(AddCustomerAddressCommand command)
        {
            var customer = await _repository.AsQuery().SingleOrDefaultAsync(p => p.UserId == command.UserId);

            if (customer == null)
            {
                throw new DomainException("مشتری یافت نشد");
            }

            var city = _cityRepository.Find(command.CityId);

            if (city == null)
            {
                throw new DomainException("شهر یافت نشد");
            }

            var address = new CustomerAddress(Guid.NewGuid(), command.Title, command.AddressText, command.PhoneNumber,
                                              command.CityId, city.Code, city.CityName, command.Position.ToDbGeography(), city.Province.Id,
                                              city.Province.Name, city.Province.Code, false);

            if (!customer.CustomerAddresses.Any())
            {
                customer.DefultCustomerAddress = new DefultCustomerAddress(address.Id, command.Title, command.AddressText,
                                                                           command.PhoneNumber, command.CityId, city.Code, city.CityName, command.Position.ToDbGeography(),
                                                                           city.Province.Id, city.Province.Name, city.Province.Code);
            }
            customer.CustomerAddresses.Add(address);
            return(new AddCustomerAddressCommandResponse());
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> Post(AddCustomerAddressCommand command)
        {
            command.UserId = UserId;
            var commandResponse = await Bus.Send <AddCustomerAddressCommand, AddCustomerAddressCommandResponse>(command);

            var response = new ResponseModel
            {
                Message      = "اضافه کردن آدرس با موفقیت انجام شد",
                Success      = true,
                ResponseData = commandResponse
            };

            return(Ok(response));
        }
        public async Task <ActionResult <bool> > AddCustomerAddress(int identity, AddCustomerAddressCommand addCustomerAddressCommand)
        {
            try
            {
                addCustomerAddressCommand.SetCustomerIdentity(identity);
                bool result = await this.mediator.Send(addCustomerAddressCommand);

                return(this.Ok(result));
            }
            catch (Exception ex)
            {
                return(this.BadRequest(ex));
            }
        }
Esempio n. 4
0
        public async Task Handle_CustomerExist_AddCustomerAddress(
            [Frozen] Mock <IRepository <Entities.Customer> > customerRepoMock,
            AddCustomerAddressCommandHandler sut,
            AddCustomerAddressCommand command
            )
        {
            // Arrange

            //Act
            var result = await sut.Handle(command, CancellationToken.None);

            //Assert
            result.Should().NotBeNull();
            customerRepoMock.Verify(x => x.UpdateAsync(
                                        It.IsAny <Entities.Customer>(),
                                        It.IsAny <CancellationToken>()
                                        ));
        }
Esempio n. 5
0
        public void Handle_CustomerDoesNotExist_ThrowArgumentNullException(
            [Frozen] Mock <IRepository <Entities.Customer> > customerRepoMock,
            AddCustomerAddressCommandHandler sut,
            AddCustomerAddressCommand command
            )
        {
            // Arrange
            customerRepoMock.Setup(x => x.GetBySpecAsync(
                                       It.IsAny <GetCustomerSpecification>(),
                                       It.IsAny <CancellationToken>()
                                       ))
            .ReturnsAsync((Entities.Customer)null);

            //Act
            Func <Task> func = async() => await sut.Handle(command, CancellationToken.None);

            //Assert
            func.Should().Throw <ArgumentNullException>()
            .WithMessage("Value cannot be null. (Parameter 'customer')");
        }