示例#1
0
        public PersonalUserCreatingConfirmation CreateUser(PersonalUser user, string password)
        {
            if (!CheckUniqueUsername(user.Username, false, null))
            {
                throw new UniqueValueViolationException("Username should be unique");
            }
            if (!CkeckUniqueEmail(user.Email))
            {
                throw new UniqueValueViolationException("Email should be unique");
            }
            if (!CheckCountry(user.CountryId))
            {
                throw new ForeignKeyConstraintViolationException("Foreign key constraint violated");
            }

            ////Dodavanje u userDbContext
            PersonalUserCreatingConfirmation persUserCreated = _personalUserRepository.CreateUser(user);

            _personalUserRepository.SaveChanges();

            //Dodavanje u IdentityUserDbContext
            string         username = string.Join("", user.Username.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
            var            persuser = new AccountInfo(username, user.Email, persUserCreated.UserId);
            IdentityResult result   = _userManager.CreateAsync(persuser, password).Result;

            if (result.Succeeded)
            {
                _userManager.AddToRoleAsync(persuser, "Regular user").Wait();
            }
            else
            {
                _personalUserRepository.DeleteUser(persUserCreated.UserId);
            }
            return(persUserCreated);
        }
        public PersonalUserCreatedConfirmation CreateUser(PersonalUser personalUser, string password)
        {
            if (!CheckUniqueUsername(personalUser.Username, false, null))
            {
                //Unique violation
                throw new UniqueValueViolationException("Username should be unique");
            }
            if (!CkeckUniqueEmail(personalUser.Email))
            {
                throw new UniqueValueViolationException("Email should be unique");
            }
            if (!CheckCity(personalUser.CityId))
            {
                throw new ForeignKeyConstraintViolationException("Foreign key constraint violated");
            }

            ////Adding to userdbcontext tables
            PersonalUserCreatedConfirmation userCreated = _personalUserRepository.CreateUser(personalUser);

            _personalUserRepository.SaveChanges();

            //Adding to identityuserdbcontext tables
            string         username = string.Join("", personalUser.Username.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
            var            acc      = new AccountInfo(username, personalUser.Email, userCreated.UserId);
            IdentityResult result   = _userManager.CreateAsync(acc, password).Result;

            if (result.Succeeded)
            {
                _userManager.AddToRoleAsync(acc, "Regular user").Wait();
            }
            else
            {
                _personalUserRepository.DeleteUser(userCreated.UserId);
                throw new ExectionException("Erorr trying to create user");
            }
            return(userCreated);
        }