/// <summary> /// Should be ran at startup /// </summary> /// <param name="tenantsDbContext"></param> public static void AddAnyNewCustomDomainsOrIps(TenantsDbContext tenantsDbContext) { if (tenantsDbContext == null) { return; } try { if (File.Exists(s_customTenantsFilePath)) { var tenants = tenantsDbContext.Tenants.ToList(); var customTenants = JsonConvert.DeserializeObject <List <Tenant> >(File.ReadAllText(s_customTenantsFilePath)); foreach (var customTenant in customTenants) { if (tenants.Any(tenant => tenant.Name == customTenant.Name && tenant.DomainNames != customTenant.DomainNames)) { var updatedTenant = tenants.SingleOrDefault(tenant => tenant.Name == customTenant.Name); updatedTenant.DomainNames = customTenant.DomainNames; tenantsDbContext.Update(updatedTenant); } if (tenants.Any(tenant => tenant.Name == customTenant.Name && tenant.IpAddresses != customTenant.IpAddresses)) { var updatedTenant = tenants.SingleOrDefault(tenant => tenant.Name == customTenant.Name); updatedTenant.IpAddresses = customTenant.IpAddresses; tenantsDbContext.Update(updatedTenant); } } tenantsDbContext.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> UpdateDomainNames(Tenant tenant) { try { if (tenant == null || string.IsNullOrWhiteSpace(tenant.Name)) { return(StatusCode((int)HttpStatusCode.UnprocessableEntity, "The tenant name must not be null")); } var tenantName = tenant.Name; if (_tenantsDbContext.Tenants.Any(tenant => tenant.Name == tenantName) == false) { return(StatusCode((int)HttpStatusCode.InternalServerError, $"The tenant: {tenantName}. Is not in the database so the domain names could not be updated.")); } var updatedTenant = _tenantsDbContext.Tenants.SingleOrDefault(tenant => tenant.Name == tenantName); if (updatedTenant.DomainNames != tenant.DomainNames) { var oldDomainNames = updatedTenant.DomainNames; updatedTenant.DomainNames = tenant.DomainNames; _tenantsDbContext.Update(updatedTenant); await _tenantsDbContext.SaveChangesAsync(); CustomTenantsFileManager.UpdateCustomTenant(updatedTenant); return(Ok($"Updated {tenantName}'s Domain Names from: {oldDomainNames} to: {tenant.DomainNames}")); } else { return(StatusCode((int)HttpStatusCode.InternalServerError, $"The domain names for {tenantName} are the same.")); } } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }