Exemplo n.º 1
0
        public IBaseCommandResult Handle(CreateUserCommand command)
        {
            _repository.MockDataCreator(); //note: comment THIS LINE to avoid the creation of FAKE MockData

            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new BaseCommandResult(false, "Need to fix the errors on User", command.Notifications));
            }

            var userByUsername = _repository.GetByLogin(command.UserName);

            if (userByUsername != null)
            {
                return(new BaseCommandResult(true, "Username Already exists.", null));
            }

            // if i pass the validation, i should create a new user
            var user = new User(command.AditionalInfo,
                                new Name(command.FirstName, command.LastName),
                                new Document(command.CountryRegistryNumber, command.StateRegistryNumber),
                                new Phone(command.PhoneNumber1, command.PhoneNumber2, command.MobilePhoneNumber1, command.MobilePhoneNumber2),
                                new Email(command.EmailAddress),
                                new Address(command.Street, command.StreetNumber, command.NeighborHood, command.City, command.ZipCode),
                                new UserAccount(command.UserName, command.Password));

            // Save on Database
            _repository.Create(user);

            // Note: Here I`ll simply add all CLAIMS to this New User, so basically the user will access everything. As described in README.MD
            // you should adapt this rule to your business (like creating claims based on a "Profile" or a Screen to select claims to the user)
            // because this sample is not a sample of "real business" but development, we will simply give user all claims.
            var claims        = _claimRepository.Get();
            var claimsForUser = new List <UserClaim>();

            foreach (Claim cla in claims)
            {
                claimsForUser.Add(new UserClaim(user, cla));
            }

            _userClaimRepository.AddUserClaims(claimsForUser);
            // Return the Value
            return(new BaseCommandResult(true, "User Saved with Success! You can use it for login now.", null));
        }