Пример #1
0
        public async Task <IActionResult> RegisterUser([FromBody] UserRegistration userRegistration)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userDefaultLeagueCode = null;

            if (!userRegistration.DoNotSetDefaultLeague)
            {
                if (String.IsNullOrEmpty(userRegistration.DefaultLeagueCode))
                {
                    return(new BadRequestObjectResult($"Setting default league, but none passed in"));
                }
                else
                {
                    userDefaultLeagueCode = userRegistration.DefaultLeagueCode;
                    var league = await this._leagueService.ReadLeague(userDefaultLeagueCode);

                    // this verifies the exact league casing
                    userDefaultLeagueCode = league.LeagueCode;
                }
            }

            var newPickEmUser = new PickEmUser
            {
                Email             = userRegistration.Email,
                UserName          = userRegistration.UserName,
                DefaultLeagueCode = userDefaultLeagueCode
            };

            var result = await _userManager.CreateAsync(newPickEmUser, userRegistration.Password);

            if (!result.Succeeded)
            {
                return(new BadRequestObjectResult(AuthHelpers.AddErrorsToModelState(result, ModelState)));
            }

            if (!userRegistration.DoNotSetDefaultLeague)
            {
                await _leagueService.AddLeaguePlayer(userDefaultLeagueCode, new LeaguePlayerAdd { PlayerTag = userRegistration.UserName, UserName = userRegistration.UserName });
            }

            string resultMessage = string.Format("User ({0}) created", userRegistration.UserName);

            _logger.LogInformation(resultMessage);

            User newUser = new User
            {
                DefaultLeagueCode = userDefaultLeagueCode,
                Email             = userRegistration.Email,
                UserName          = userRegistration.UserName
            };

            return(new OkObjectResult(newUser));
        }