Esempio n. 1
0
            public async Task <UserDto> Handle(CreateUser request, CancellationToken cancellationToken)
            {
                CreateUserValidator validator        = new CreateUserValidator();
                ValidationResult    validationResult = await validator.ValidateAsync(request, cancellationToken);

                if (!validationResult.IsValid)
                {
                    throw new EntityValidationException(nameof(CreateUser), request, validationResult.Errors);
                }

                User currentUser = await _mainDbContext.Users
                                   .AsNoTracking()
                                   .Where(u => u.Email == request.Email && !u.IsDeleted)
                                   .FirstOrDefaultAsync(cancellationToken);

                if (currentUser != null)
                {
                    throw new EntityAlreadyExists(nameof(User), request.Email);
                }

                User newUser = _mapper.Map <User>(request);

                newUser.Password = BCrypt.Net.BCrypt.HashPassword(request.Password);

                await _mainDbContext.Users.AddAsync(newUser, cancellationToken);

                await _mainDbContext.SaveChangesAsync(cancellationToken);

                _logger.LogDebug($"Created user: {request.Email}");
                return(_mapper.Map <UserDto>(newUser));
            }
Esempio n. 2
0
            public async Task <UserDto> Handle(CreateUser request, CancellationToken cancellationToken)
            {
                CreateUserValidator validator        = new CreateUserValidator();
                ValidationResult    validationResult = await validator.ValidateAsync(request, cancellationToken);

                if (!validationResult.IsValid)
                {
                    throw new EntityValidationException(nameof(User), request, validationResult.Errors);
                }

                FilterDefinition <User> filterDefinition = Builders <User> .Filter
                                                           .Or(new[]
                {
                    Builders <User> .Filter.Eq(x => x.Email, request.Email),
                    Builders <User> .Filter.Eq(x => x.Username, request.Username)
                });

                User userAlreadyExists = await _userRepository.Get(filterDefinition, cancellationToken);

                if (userAlreadyExists != null)
                {
                    throw new EntityAlreadyExistsException(nameof(User), request.Email);
                }

                User user = _mapper.Map <User>(request);

                user.Password = await _securityService.HashPassword(request.Password);

                await _userRepository.Add(user, cancellationToken);

                return(_mapper.Map <UserDto>(user));
            }
Esempio n. 3
0
        public async Task CheckRecordValidation(UserDetailSharedInputData userData)
        {
            var validate = await _createValidationRules.ValidateAsync(userData).ConfigureAwait(false);

            if (!validate.IsValid)
            {
                throw new EntityValidationException(validate.ToString("~"));
            }

            if (userData.CompanyId != null && !await _companyRepository.IsExistAsync(x => x.Id == userData.CompanyId)
                .ConfigureAwait(false))
            {
                throw new RecordNotFoundException("Company not found");
            }
        }