Exemplo n.º 1
0
        public CorporationUserCreatedConfirmation CreateUser(Corporation user, string password)
        {
            if (!CheckUniqueUsername(user.Username, false, null))
            {
                //Unique violation
                throw new UniqueValueViolationException("Username should be unique");
            }
            if (!CkeckUniqueEmail(user.Email))
            {
                throw new UniqueValueViolationException("Email should be unique");
            }
            if (!CheckCity(user.CityId))
            {
                throw new ForeignKeyConstraintViolationException("Foreign key constraint violated");
            }
            CorporationUserCreatedConfirmation userCreated = _corporationUserRepository.CreateUser(user);

            _corporationUserRepository.SaveChanges();

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

            if (result.Succeeded)
            {
                _userManager.AddToRoleAsync(acc, "Regular user").Wait();
            }
            else
            {
                _corporationUserRepository.DeleteUser(userCreated.UserId);
                //throw new ExectionException("Erorr trying to create user");
            }
            return(userCreated);
        }
        public ActionResult <CorporationUserCreatedConfirmationDto> CreateUser([FromBody] CorporationUserCreationDto corporationUser)
        {
            try
            {
                Corporation userEntity = _mapper.Map <Corporation>(corporationUser);
                CorporationUserCreatedConfirmation userCreated = _corporationUsersService.CreateUser(userEntity, corporationUser.Password);


                string location = _linkGenerator.GetPathByAction("GetUserById", "CorporationUser", new { userId = userCreated.UserId });
                return(Created(location, _mapper.Map <CorporationUserCreatedConfirmationDto>(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));
            }
        }