Exemple #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
            });
        }