Performance improvements, refactoring of multi-tenant support, split Alias and Tenant entities for cleaner separation of concerns, create an additional site during installation for demonstratng multitenancy

This commit is contained in:
Shaun Walker
2019-05-24 13:33:19 -04:00
parent 0067521cd5
commit 8deb119f36
57 changed files with 880 additions and 309 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using Oqtane.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.EntityFrameworkCore;
namespace Oqtane.Repository
{
@ -11,23 +12,14 @@ namespace Oqtane.Repository
{
private HostContext db;
private readonly IMemoryCache _cache;
private readonly string alias;
public TenantRepository(HostContext context, IMemoryCache cache, IHttpContextAccessor accessor)
public TenantRepository(HostContext context, IMemoryCache cache)
{
db = context;
_cache = cache;
// get site alias based on request context
alias = accessor.HttpContext.Request.Host.Value;
string path = accessor.HttpContext.Request.Path.Value;
if (path.StartsWith("/~") && !path.StartsWith("/~/"))
{
alias += path.Substring(0, path.IndexOf("/", 1));
}
}
public Tenant GetTenant()
public IEnumerable<Tenant> GetTenants()
{
try
{
@ -36,15 +28,45 @@ namespace Oqtane.Repository
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return db.Tenant.ToList();
});
Tenant tenant;
if (tenants.Count() == 1)
{
tenant = tenants.FirstOrDefault();
}
else
{
tenant = tenants.Where(item => item.Alias == alias).FirstOrDefault();
}
return tenants;
}
catch
{
throw;
}
}
public void AddTenant(Tenant tenant)
{
try
{
db.Tenant.Add(tenant);
db.SaveChanges();
}
catch
{
throw;
}
}
public void UpdateTenant(Tenant tenant)
{
try
{
db.Entry(tenant).State = EntityState.Modified;
db.SaveChanges();
}
catch
{
throw;
}
}
public Tenant GetTenant(int tenantId)
{
try
{
Tenant tenant = db.Tenant.Find(tenantId);
return tenant;
}
catch
@ -52,5 +74,19 @@ namespace Oqtane.Repository
throw;
}
}
public void DeleteTenant(int tenantId)
{
try
{
Tenant tenant = db.Tenant.Find(tenantId);
db.Tenant.Remove(tenant);
db.SaveChanges();
}
catch
{
throw;
}
}
}
}