Exemple #1
0
        public async Task <IActionResult> CreateNewUser([FromBody] NewUserPlayerRequest newUserPlayerRequest)
        {
            try
            {
                var newUserId = await _userManagementService.CreateNewUserPlayerAsync(newUserPlayerRequest);

                return(Ok(newUserId));
            }
            catch (UserExistsException)
            {
                return(StatusCode((int)HttpStatusCode.Conflict, $"User with email '{newUserPlayerRequest.Email}' already exists"));
            }
        }
        public async Task <Guid> CreateNewUserPlayerAsync(NewUserPlayerRequest newUserPlayerRequest)
        {
            var userWithEmailExists = await
                                      _isolaniDbContext.Users
                                      .AnyAsync(user =>
                                                user.Email.Equals(newUserPlayerRequest.Email, StringComparison.OrdinalIgnoreCase));

            if (userWithEmailExists)
            {
                throw new UserExistsException();
            }

            var now = DateTime.UtcNow;

            var savedPasswordHash = AuthenticationService.CreateSaltyPasswordHash(newUserPlayerRequest.Password);

            var userId = Guid.NewGuid();

            var newUser = _objectMapper.Map <User>(newUserPlayerRequest);

            newUser.Id             = userId;
            newUser.CreatedDateUtc = newUser.LastLoginDateUtc = now;
            newUser.Password       = savedPasswordHash;

            await _isolaniDbContext.AddAsync(newUser);

            var newPlayer = _objectMapper.Map <Player>(newUserPlayerRequest);

            newPlayer.Id = userId;

            await _isolaniDbContext.AddAsync(newPlayer);

            await _isolaniDbContext.SaveChangesAsync();

            return(newUser.Id);
        }