Exemple #1
0
        public void Should_Convert_Nickname_To_String()
        {
            var    nick = "nick";
            string sut  = Nickname.Create(nick).Value;

            sut.Should().Be(nick);
        }
        private static void Build(EntityTypeBuilder <User> builder)
        {
            builder
            .HasKey(x => x.Id)
            .HasName("PK_Users");
            builder.Property(x => x.Id)
            .ValueGeneratedNever()
            .HasConversion(x => x.Value, x => new UserId(x))
            .HasColumnName("UserId");

            builder.OwnsOne(x => x.Email, e =>
            {
                e.Property(x => x.Value).HasColumnName("Email").IsRequired();
            });
            builder
            .Property(x => x.PasswordHash).IsRequired();
            builder.Property(x => x.IsVerified).IsRequired();
            builder.Property(x => x.Nickname)
            .HasConversion(x => x.Value, x => Nickname.Create(x))
            .IsRequired(false);
            builder.OwnsOne(
                u => u.Avatar,
                b =>
            {
                b.Property(x => x.Value).IsRequired().HasColumnName("Avatar");
            }
                );
        }
        public async Task HandleAsync(ActivateCommand command)
        {
            var verificationLink = await _activationLinkRepository.GetAsync(command.UserId) ?? throw new InvalidVerificationTokenException();

            var user = await _userRepository.GetAsync((UserId)verificationLink.UserId) ?? throw new InvalidVerificationTokenException();

            await user.Activate(verificationLink, command.Token, Nickname.Create(command.Nickname), _uniqueNicknameRule);
        }
Exemple #4
0
        public void Different_Strings_Should_Create_Different_Nicknames()
        {
            var nick1 = Nickname.Create("nick1").Value;
            var nick2 = Nickname.Create("nick2").Value;

            nick1.Should().NotBe(nick2);
            nick1.GetHashCode().Equals(nick2.GetHashCode()).Should().BeFalse();
        }
Exemple #5
0
        public void Same_String_Should_Create_The_Same_Nickname()
        {
            var nick1 = Nickname.Create("nick").Value;
            var nick2 = Nickname.Create("nick").Value;

            nick1.Should().Be(nick2);
            nick1.GetHashCode().Equals(nick2.GetHashCode()).Should().BeTrue();
        }
Exemple #6
0
        public void User_Should_Change_Nickname()
        {
            var sut         = CreateTestUser();
            var newNickname = Nickname.Create("newnickname");

            sut.ChangeNickname(newNickname.Value);

            sut.Nickname.Value.Should().Be(newNickname.Value);
        }
Exemple #7
0
        public async Task <Result <int> > Handle(RegisterUserCommand request, CancellationToken cancellationToken)
        {
            var nicknameValidationResult = Nickname.Create(request.Nickname);
            var eMailValidationResult    = Email.Create(request.Email);
            var passwordValidationResult = Password.Create(request.Password);
            var fullNameValidationResult = FullName.Create(request.Name, request.Surname);

            var validationResult = Result.Combine(nicknameValidationResult, eMailValidationResult, passwordValidationResult, fullNameValidationResult);

            if (validationResult.IsFailure)
            {
                _logger.Warning("Error during registration of user {Nickname}: Error message: {ErrorMessage}",
                                request.Nickname,
                                validationResult.Error);
                return(Result.Failure <int>(validationResult.Error));
            }

            var userToRegister = new User(
                nicknameValidationResult.Value,
                fullNameValidationResult.Value,
                passwordValidationResult.Value,
                eMailValidationResult.Value,
                request.Biography);

            if (request.ProfilePicture != null)
            {
                var profilePicture = Picture.Create(request.ProfilePicture, _idGenerator.GetNextId()).Value;
                userToRegister.SetProfilePicture(profilePicture);
            }

            using (var tx = _session.BeginTransaction())
            {
                try
                {
                    await _session.SaveAsync(userToRegister, cancellationToken);

                    await tx.CommitAsync(cancellationToken);

                    _logger.Information("User [{Nickname}({UserID})] has just registered.",
                                        request.Nickname,
                                        userToRegister.ID);

                    return(Result.Success(userToRegister.ID));
                }
                catch (ADOException ex)
                {
                    await tx.RollbackAsync(cancellationToken);

                    _logger.Error("Error during registration of user {Nickname}. Error message: {ErrorMessage}",
                                  request.Nickname,
                                  ex.Message);

                    return(Result.Failure <int>(ex.Message));
                }
            }
        }
Exemple #8
0
 public async Task <UserDto> HandleAsync(GetUserQuery query)
 => await _context.Users
 .AsNoTracking()
 .Where(x => x.Nickname == Nickname.Create(query.Nickname))
 .Select(x => new UserDto
 {
     Id       = x.Id,
     Email    = x.Email,
     Nickname = x.Nickname
 }
         )
 .FirstOrDefaultAsync();
        public async Task <Result> Handle(EditUserDetailsCommand request, CancellationToken cancellationToken)
        {
            var nicknameValidationResult = Nickname.Create(request.Nickname);
            var eMailValidationResult    = Email.Create(request.Email);
            var fullNameValidationResult = FullName.Create(request.Name, request.Surname);

            var validationResult = Result.Combine(nicknameValidationResult, eMailValidationResult, fullNameValidationResult);

            if (validationResult.IsFailure)
            {
                _logger.Warning("Tried to update user profile for user [{Nickname}({UserID})] but some data were not valid: {WarningMessage}",
                                request.Nickname,
                                request.UserID,
                                validationResult.Error);
                return(Result.Fail(validationResult.Error));
            }

            using (var tx = _session.BeginTransaction())
            {
                try
                {
                    var userToUpdate = await _session.GetAsync <User>(request.UserID);

                    userToUpdate.ChangeNickname(nicknameValidationResult.Value);
                    userToUpdate.ChangeEmailAddress(eMailValidationResult.Value);
                    userToUpdate.ChangeFullName(fullNameValidationResult.Value);
                    userToUpdate.UpdateBiography(request.Bio);
                    userToUpdate.SetProfilePicture((Picture)request.ProfilePicture);

                    await tx.CommitAsync();

                    _logger.Information("Successfully updated user profile for user [{Nickname}({UserID})]",
                                        request.Nickname,
                                        request.UserID);

                    return(Result.Ok(userToUpdate.ID));
                }
                catch (ADOException ex)
                {
                    await tx.RollbackAsync();

                    _logger.Error("Error updating user profile for user [{Nickname}({UserID})]. Error message: {ErrorMessage}",
                                  request.Nickname,
                                  request.UserID,
                                  ex.Message);

                    return(Result.Fail(ex.Message));
                }
            }
        }
        public async Task HandleAsync(ChangeNicknameCommand command)
        {
            var user = await _repository.GetAsync(new UserId(command.UserId)) ?? throw new UserNotFoundException();

            await user.Change(Nickname.Create(command.Nickname), _rule);
        }
Exemple #11
0
 public void Should_Create_A_Valid_Nickname()
 {
     Nickname.Create("nick").IsSuccess.Should().BeTrue();
 }
Exemple #12
0
        public void Nickname_From_String_Too_Long_Is_Not_Valid()
        {
            var sut = new string('x', 42);

            Nickname.Create(sut).IsFailure.Should().BeTrue();
        }
Exemple #13
0
 public void Nickname_From_Empty_String_Is_Not_Valid()
 {
     Nickname.Create(string.Empty).IsFailure.Should().BeTrue();
 }