Ejemplo n.º 1
0
        public async Task <UserLogin> Register(UserRegistration registration)
        {
            // If registration provided an email, check to see if it's available.
            if (!String.IsNullOrWhiteSpace(registration.Email))
            {
                User?emailInUse = await userRepo.FindByEmail(registration.Email !);

                if (emailInUse != null)
                {
                    throw new EmailAlreadyInUseException();
                }
            }

            // Check to see if the username is unique.
            User?usernameInUse = await userRepo.FindByUsername(registration.Username);

            if (usernameInUse != null)
            {
                throw new UsernameAlreadyInUseException();
            }

            User user = factory.CreateFromRegistration(registration);
            await userRepo.Add(user);

            UserLogin login = new UserLogin(user.Id);
            await loginRepo.Add(login);

            await bus.Dispatch(new UserRegisterEvent(user, login));

            return(login);
        }