示例#1
0
        public async Task <UserDto> HandleAsync(UpdateUser command)
        {
            var user = await _usersRepository.GetById(command.Id);

            if (user == null)
            {
                throw new UserNotFoundException($"The user with id: '{command.Id}' was not found.");
            }

            if ((command.Email != user.Email.Value()) && !(await _checkEmailAvailability.IsAvailable(command.Email)))
            {
                throw new EmailAlreadyInUseException($"The email '{command.Email}' is already in use.");
            }

            user.SetName(command.Name);
            user.SetSurname(command.Surname);
            user.SetEmail(Email.FromString(command.Email));
            if (user.Password.VerifyPassword(command.Password))
            {
                user.SetPassword(Password.FromString(command.NewPassword));
            }
            else
            {
                throw new WrongPasswordException("The password provided is wrong.");
            }
            user.SetCountry(command.Country);
            user.SetPhone(Phone.FromString(command.Phone));
            user.SetPostalCode(PostalCode.FromString(command.PostalCode));

            await _usersRepository.UpdateAsync(user);

            return(UserMapper.From(user));
        }
示例#2
0
        public async Task <UserDto> HandleAsync(CreateUser command)
        {
            if (!(await _checkEmailAvailability.IsAvailable(command.Email)))
            {
                throw new EmailAlreadyInUseException($"The email '{command.Email}' is already in use.");
            }

            User user = new User(new EntityId(command.Id), command.Name, command.Surname, Email.FromString(command.Email),
                                 Password.FromString(command.Password), command.Country, string.IsNullOrEmpty(command.Phone) ? null : Phone.FromString(command.Phone),
                                 string.IsNullOrEmpty(command.PostalCode) ? null : PostalCode.FromString(command.PostalCode));

            await _usersRepository.AddAsync(user);

            return(UserMapper.From(user));
        }
示例#3
0
 public static Core.Domain.Entities.User MapTo(User user)
 {
     return(new Core.Domain.Entities.User(new EntityId(user.Id), user.Name, user.Surname, Email.FromString(user.Email),
                                          Password.FromHash(user.Password), user.Country, string.IsNullOrEmpty(user.Phone) ? null : Phone.FromString(user.Phone),
                                          string.IsNullOrEmpty(user.PostalCode) ? null : PostalCode.FromString(user.PostalCode)));
 }