Exemplo n.º 1
0
        /// <summary>
        /// Create
        /// </summary>
        /// <param name="userResponseModel"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <UserResponseModel> CreateAsync(UserRequestModel userRequestModel, int agencyId, CancellationToken ct = default)
        {
            Agency agency = await _agencyRepository.FindAsync(x => x.Id == agencyId, ct);

            if (agency is null)
            {
                throw new IdentityException("Invalid Agency.");
            }
            User user = await _userRepository.FindAsync(x => x.UserName == userRequestModel.Email, ct);

            if (user != null)
            {
                throw new IdentityException("This email already registered.");
            }
            user                 = _mapper.Map <User>(userRequestModel);
            user.Password        = _passHasher.HashPassword(user, userRequestModel.Password);
            user.Employee.Agency = agency;
            user.Status          = Status.Active;

            _userRepository.Add(user);

            await _unitOfWork.SaveChangesAsync(ct);

            var data = _mapper.Map <UserResponseModel>(user);

            return(data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create
        /// </summary>
        /// <param name="userResponseModel"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <UserResponseModel> Create(UserRequestModel userRequestModel, CancellationToken ct = default)
        {
            int    agencyId = 1;// this value cames by parameter, from token claims
            Agency agency   = await _agencyRepository.FindAsync(x => x.Id == agencyId);

            if (agency is null)
            {
                // report agency doesn't exist
            }
            User user = await _userRepository.FindAsync(x => x.UserName == userRequestModel.Email);

            if (user != null)
            {
                // report user with this email exists
            }
            user                 = _mapper.Map <User>(userRequestModel);
            user.Password        = Guid.NewGuid().ToString();
            user.Agency          = agency;
            user.Employee.Agency = user.Agency;

            _userRepository.Add(user);

            await _unitOfWork.SaveChangesAsync(ct);

            var data = _mapper.Map <UserResponseModel>(user);

            return(data);
        }