示例#1
0
        public async Task <Optional <UserDTO> > CreateAsync(UserDTO userDTO)
        {
            Expression <Func <User, bool> > filter = user
                                                     => user.Email.ToLower() == userDTO.Email.ToLower();

            var userExists = await _userRepository.GetAsync(filter);

            if (userExists != null)
            {
                await _mediator.PublishDomainNotificationAsync(new DomainNotification(
                                                                   ErrorMessages.UserAlreadyExists,
                                                                   DomainNotificationType.UserAlreadyExists));

                return(new Optional <UserDTO>());
            }

            var user = _mapper.Map <User>(userDTO);

            user.Validate();

            if (!user.IsValid)
            {
                await _mediator.PublishDomainNotificationAsync(new DomainNotification(
                                                                   ErrorMessages.UserInvalid(user.ErrorsToString()),
                                                                   DomainNotificationType.UserInvalid));

                return(new Optional <UserDTO>());
            }

            user.SetPassword(_argon2IdHasher.Hash(user.Password));

            var userCreated = await _userRepository.CreateAsync(user);

            return(_mapper.Map <UserDTO>(userCreated));
        }