oqtane.framework/Oqtane.Server/Repository/TenantRepository.cs
Pavel Vesely 940cdcb349 Database Manager
done:
+ master.sql as resource
+ implemented incremental database changes also for Master
+ dbUp sql script variables implemented
+ improved database handling and creation code
+ simpified database creation
+ almost all Database and Tenant creation moved to DatabaseManager.cs (rest code marked with TODO)
+ Unattended install of master can be performed by settings in appsettings.json
+ Improved IsInstalled checking
+ Removed DBSchema field from Tenant
+ Default database and site creation moved to Program.Main
2020-03-29 14:45:02 +02:00

64 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Oqtane.Models;
namespace Oqtane.Repository
{
public class TenantRepository : ITenantRepository
{
private MasterDBContext _db;
private readonly IMemoryCache _cache;
public TenantRepository(MasterDBContext context, IMemoryCache cache)
{
_db = context;
_cache = cache;
}
public IEnumerable<Tenant> GetTenants()
{
return _cache.GetOrCreate("tenants", entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return _db.Tenant.ToList();
});
}
public Tenant AddTenant(Tenant tenant)
{
_db.Tenant.Add(tenant);
_db.SaveChanges();
_cache.Remove("tenants");
return tenant;
}
public Tenant UpdateTenant(Tenant tenant)
{
_db.Entry(tenant).State = EntityState.Modified;
_db.SaveChanges();
_cache.Remove("tenants");
return tenant;
}
public Tenant GetTenant(int tenantId)
{
return _db.Tenant.Find(tenantId);
}
public void DeleteTenant(int tenantId)
{
Tenant tenant = _db.Tenant.Find(tenantId);
if (tenant != null)
{
_db.Tenant.Remove(tenant);
_db.SaveChanges();
}
_cache.Remove("tenants");
}
}
}