Esempio n. 1
0
        public async Task <UserDataDto> AddAsync(UserDto user)
        {
            var isValidEmail = EmailTool.IsValidEmail(user.Email);

            if (!isValidEmail)
            {
                throw new Exception("Email is not valid");
            }

            var userAge = DateTool.GetAge(user.BirthDate);

            if (userAge < 18)
            {
                throw new Exception("You must be of legal age to register");
            }

            var emailExist = await this._unitOfWork.Repositories.Users
                             .QueryableBy(x =>
                                          x.Email == user.Email)
                             .AnyAsync();

            if (emailExist)
            {
                throw new Exception("You email already exists");
            }

            var phoneNumberExist = await this._unitOfWork.Repositories.Users
                                   .QueryableBy(x =>
                                                x.PhoneNumber == user.PhoneNumber)
                                   .AnyAsync();

            if (phoneNumberExist)
            {
                throw new Exception("You phone number already exists");
            }

            var password          = StringTool.GetRandomString(20);
            var encryptedPassword = EncryptedTool.EncryptToMD5(password);
            var entity            = new User
            {
                Name           = user.Name,
                LastName       = user.LastName,
                SecondLastName = user.SecondLastName,
                Email          = user.Email,
                BirthDate      = user.BirthDate,
                PhoneNumber    = user.PhoneNumber,
                Genre          = user.Genre,
                Password       = encryptedPassword
            };

            this._unitOfWork.Repositories.Users.Add(entity);
            await this._unitOfWork.SaveAsync();

            return(new UserDataDto
            {
                Id = entity.Id,
                Email = entity.Email,
                Password = password
            });
        }
Esempio n. 2
0
        public async Task <UserDataDto> UpdatePasswordAsync(string email)
        {
            var entity = await this._unitOfWork.Repositories.Users
                         .GetByAsync(x =>
                                     x.Email == email);

            if (entity == null)
            {
                return(null);
            }

            var password          = StringTool.GetRandomString(20);
            var encryptedPassword = EncryptedTool.EncryptToMD5(password);

            entity.Password = encryptedPassword;
            this._unitOfWork.Repositories.Users.Update(entity);
            await this._unitOfWork.SaveAsync();

            return(new UserDataDto
            {
                Id = entity.Id,
                FullName = $"{entity.Name} {entity.LastName}{(!String.IsNullOrWhiteSpace(entity.SecondLastName) ? $" {entity.SecondLastName}" : String.Empty)}",
                Email = entity.Email,
                Password = password
            });
        }
Esempio n. 3
0
        public async Task <UserDto> GetByAsync(string email, string password)
        {
            var encryptedPassword = EncryptedTool.EncryptToMD5(password);
            var user = await _unitOfWork.Repositories.Users
                       .GetByAsync(x =>
                                   x.Email == email &&
                                   x.Password == encryptedPassword);

            if (user == null)
            {
                return(null);
            }

            return(new UserDto
            {
                Id = user.Id,
                Name = user.Name,
                LastName = user.LastName,
                SecondLastName = user.SecondLastName,
                Email = user.Email,
                BirthDate = user.BirthDate,
                PhoneNumber = user.PhoneNumber,
                Genre = user.Genre
            });
        }