Example #1
0
        public async Task <Unit> Handle(UpdateProfileCommand request, CancellationToken ct)
        {
            var account = await _repository.GetAsync(request.Id, ct);

            if (account is null)
            {
                throw new ElwarkException(ElwarkExceptionCodes.AccountNotFound);
            }

            var timezone = await _timezone.GetAsync(request.Timezone, ct);

            if (timezone is null)
            {
                throw new ElwarkException(ElwarkExceptionCodes.TimezoneNotFound);
            }

            account.SetName(new Name(request.Nickname, request.FirstName, request.LastName));
            account.SetAddress(new Address(new CountryCode(request.CountryCode), request.City));
            account.SetTimezone(new Timezone(timezone.Name, timezone.Offset));
            account.SetProfile(account.Profile with
            {
                Bio         = request.Bio,
                DateOfBirth = request.DateOfBirth.Date,
                Gender      = request.Gender,
                Language    = new Language(request.Language)
            });

            await _repository.UpdateAsync(account, ct);

            return(Unit.Value);
        }
        public override async Task <TimezonesReply> GetTimezones(Empty request, ServerCallContext context)
        {
            var result = await _timezone.GetAsync(context.CancellationToken);

            return(new TimezonesReply
            {
                Timezones =
                {
                    result.Select(x => new Timezone
                    {
                        Name = x.Name,
                        Offset = x.Offset.ToDuration()
                    })
                }
            });
        }
        public UpdateProfileCommandValidator(ITimezoneService timezone, ICountryService country)
        {
            RuleFor(x => x.Bio)
            .MaximumLength(260);

            RuleFor(x => x.DateOfBirth)
            .NotEmpty()
            .Must(x => x < DateTime.UtcNow);

            RuleFor(x => x.City)
            .MaximumLength(100);

            RuleFor(x => x.Gender)
            .IsInEnum();

            RuleFor(x => x.Language)
            .NotEmpty()
            .Must(x => Language.TryParse(x, out _));

            RuleFor(x => x.Nickname)
            .NotEmpty()
            .MinimumLength(3)
            .MaximumLength(100);

            RuleFor(x => x.FirstName)
            .MaximumLength(100);

            RuleFor(x => x.LastName)
            .MaximumLength(100);

            RuleFor(x => x.Timezone)
            .NotEmpty()
            .MustAsync(async(value, ct) => await timezone.GetAsync(value, ct) is not null);

            RuleFor(x => x.CountryCode)
            .NotEmpty()
            .MustAsync(async(value, ct) => await country.GetAsync(value, ct) is not null);

            RuleFor(x => x.City)
            .NotNull();
        }
        public async Task HandleAsync(AccountInfoReceivedIntegrationEvent message)
        {
            var account = await _repository.GetAsync(message.AccountId);

            if (account is null)
            {
                return;
            }

            var countryCode = await GetCountryCode(message.CountryCode);

            account.SetAddress(new Address(countryCode, message.City ?? account.Address.City));
            account.SetRegistration(IPAddress.Parse(message.Ip), countryCode, _ipAddressHasher.CreateHash);

            if (message.Timezone is not null)
            {
                var timezone = await _timezone.GetAsync(message.Timezone);

                if (timezone is not null && account.Timezone == Timezone.Default)
                    account.SetTimezone(new Timezone(timezone.Name, timezone.Offset));
            }

            account.SetName(account.Name with
            {
                FirstName = account.Name.FirstName ?? message.FirstName,
                LastName  = account.Name.LastName ?? message.FirstName
            });

            account.SetProfile(account.Profile with
            {
                Bio     = account.Profile.Bio ?? message.AboutMe,
                Picture = account.Profile.Picture == Profile.DefaultPicture
                    ? message.Image ?? account.Profile.Picture
                    : account.Profile.Picture
            });

            await _repository.UpdateAsync(account);
        }