Ejemplo n.º 1
0
        public ActionResult <PersonalUserCreatedConfirmationDto> CreateAdmin([FromBody] PersonalUserCreationDto personalUser)
        {
            try
            {
                PersonalUser userEntity = _mapper.Map <PersonalUser>(personalUser);

                //Adding to userdbcontext tables
                PersonalUserCreatedConfirmation userCreated = _personalUsersService.CreateAdmin(userEntity, personalUser.Username);

                string location = _linkGenerator.GetPathByAction("GetUserById", "PersonalUser", new { userId = userCreated.UserId });
                return(Created(location, _mapper.Map <PersonalUserCreatedConfirmationDto>(userCreated)));
            }
            catch (Exception ex)
            {
                if (ex.GetType().IsAssignableFrom(typeof(ForeignKeyConstraintViolationException)))
                {
                    return(StatusCode(StatusCodes.Status409Conflict, ex.Message));
                }
                if (ex.GetType().IsAssignableFrom(typeof(UniqueValueViolationException)))
                {
                    return(StatusCode(StatusCodes.Status422UnprocessableEntity, ex.Message));
                }
                if (ex.GetBaseException().GetType() == typeof(SqlException))
                {
                    Int32 ErrorCode = ((SqlException)ex.InnerException).Number;
                    switch (ErrorCode)
                    {
                    case 2627:      // Unique constraint error
                        break;

                    case 547:       // Constraint check violation; FK violation
                        return(StatusCode(StatusCodes.Status422UnprocessableEntity, ex.Message));

                    case 2601:      // Duplicated key row error; Unique violation
                        return(StatusCode(StatusCodes.Status409Conflict, ex.Message));

                    default:
                        return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
                    }
                }
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        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);
        }