Example #1
0
        public async Task <DomainValidationResult <Address> > AddAddressAsync(
            UserId userId,
            Country country,
            string line1,
            string?line2,
            string city,
            string?state,
            string?postalCode
            )
        {
            var result = new DomainValidationResult <Address>();

            var addressBookLimit = Options.Static.AddressBook.Limit;

            if (await _addressRepository.AddressCountAsync(userId) >= addressBookLimit)
            {
                result.AddFailedPreconditionError($"You can have a maximum of {addressBookLimit} addresses in your address book");
            }

            if (result.IsValid)
            {
                var address = new Address(
                    userId,
                    country,
                    line1,
                    line2,
                    city,
                    state,
                    postalCode);

                _addressRepository.Create(address);

                await _addressRepository.UnitOfWork.CommitAsync();

                await _serviceBusPublisher.PublishUserAddressChangedIntegrationEventAsync(userId, address);

                return(address);
            }

            return(result);
        }