Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CreateTeamModel model)
        {
            //TODO: Implement Detailed Error Checking
            if (ModelState.IsValid)
            {
                try
                {
                    //TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig
                    var databaseConfig = new DatabaseConfig
                    {
                        DatabasePassword   = _configuration["DatabaseOptions:DatabasePassword"],
                        DatabaseUser       = _configuration["DatabaseOptions:DatabaseUser"],
                        DatabaseServerPort = Int32.Parse(_configuration["DatabaseOptions:DatabaseServerPort"]),
                        SqlProtocol        = SqlProtocol.Tcp,
                        ConnectionTimeOut  = Int32.Parse(_configuration["DatabaseOptions:ConnectionTimeOut"]),
                    };

                    var catalogConfig = new CatalogConfig
                    {
                        ServicePlan     = _configuration["DatabaseOptions:ServicePlan"],
                        CatalogDatabase = _configuration["DatabaseOptions:CatalogDatabase"],
                        CatalogServer   = _configuration["DatabaseOptions:CatalogServer"], // + ".database.windows.net"
                    };

                    var tenantServerConfig = new TenantServerConfig
                    {
                        TenantServer   = _configuration["DatabaseOptions:CatalogServer"],// + ".database.windows.net",
                        TenantDatabase = _configuration["DatabaseOptions:TenantDatabase"],
                    };

                    var team = new Team
                    {
                        Id       = _utilities.GetTenantKey(model.TenantName),
                        Name     = model.TenantName,
                        LogoLink = model.TenantLogoLink,
                    };



                    //Create Shard, Add Team and Register Tenant against shard
                    var shard = Sharding.CreateNewShard(tenantServerConfig.TenantDatabase, tenantServerConfig.TenantServer, databaseConfig.DatabaseServerPort, null);
                    await _tenantRepository.AddTeam(team);

                    var x = await Sharding.RegisterNewShard(team.Id, "", shard);

                    //Add first user to team. Team Owner!
                    var applicationUser = await _userService.GetApplicationUserAsync();

                    var user = new User {
                        ApplicationUserId = applicationUser.Id, Email = applicationUser.Email, UserRole = Role.SuperAdministrator
                    };
                    await _tenantRepository.AddUserToTeam(user, team.Id);


                    return(Ok(new { team_id = team.Id, team_name = team.Name }));
                }
                catch (Exception ex)
                {
                    //TODO: Log Error
                    return(BadRequest(new { Error = ex.Message }));
                }
            }

            return(BadRequest(ModelState));
        }