Fix #4010: update repositories to using db context factory.
This commit is contained in:
parent
ce710134ac
commit
c58254f951
|
@ -12,14 +12,16 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class FileRepository : IFileRepository
|
public class FileRepository : IFileRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IPermissionRepository _permissions;
|
private readonly IPermissionRepository _permissions;
|
||||||
private readonly IFolderRepository _folderRepository;
|
private readonly IFolderRepository _folderRepository;
|
||||||
private readonly ITenantManager _tenants;
|
private readonly ITenantManager _tenants;
|
||||||
|
|
||||||
public FileRepository(TenantDBContext context, IPermissionRepository permissions, IFolderRepository folderRepository, ITenantManager tenants)
|
public FileRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IPermissionRepository permissions, IFolderRepository folderRepository, ITenantManager tenants)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = dbContextFactory.CreateDbContext();
|
||||||
_permissions = permissions;
|
_permissions = permissions;
|
||||||
_folderRepository = folderRepository;
|
_folderRepository = folderRepository;
|
||||||
_tenants = tenants;
|
_tenants = tenants;
|
||||||
|
@ -38,12 +40,13 @@ namespace Oqtane.Repository
|
||||||
IEnumerable<File> files;
|
IEnumerable<File> files;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
files = _db.File.Where(item => item.FolderId == folderId).Include(item => item.Folder);
|
files = _queryContext.File.Where(item => item.FolderId == folderId).Include(item => item.Folder);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
files = _db.File.AsNoTracking().Where(item => item.FolderId == folderId).Include(item => item.Folder);
|
files = _queryContext.File.AsNoTracking().Where(item => item.FolderId == folderId).Include(item => item.Folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (File file in files)
|
foreach (File file in files)
|
||||||
{
|
{
|
||||||
file.Folder.PermissionList = permissions.ToList();
|
file.Folder.PermissionList = permissions.ToList();
|
||||||
|
@ -55,9 +58,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public File AddFile(File file)
|
public File AddFile(File file)
|
||||||
{
|
{
|
||||||
file.IsDeleted = false;
|
using var db = _dbContextFactory.CreateDbContext(); file.IsDeleted = false;
|
||||||
_db.File.Add(file);
|
db.File.Add(file);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
file.Folder = _folderRepository.GetFolder(file.FolderId);
|
file.Folder = _folderRepository.GetFolder(file.FolderId);
|
||||||
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
||||||
return file;
|
return file;
|
||||||
|
@ -65,8 +68,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public File UpdateFile(File file)
|
public File UpdateFile(File file)
|
||||||
{
|
{
|
||||||
_db.Entry(file).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(file).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
file.Folder = _folderRepository.GetFolder(file.FolderId);
|
file.Folder = _folderRepository.GetFolder(file.FolderId);
|
||||||
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
||||||
return file;
|
return file;
|
||||||
|
@ -80,14 +84,14 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public File GetFile(int fileId, bool tracking)
|
public File GetFile(int fileId, bool tracking)
|
||||||
{
|
{
|
||||||
File file;
|
using var db = _dbContextFactory.CreateDbContext(); File file;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
file = _db.File.Include(item => item.Folder).FirstOrDefault(item => item.FileId == fileId);
|
file = db.File.Include(item => item.Folder).FirstOrDefault(item => item.FileId == fileId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
file = _db.File.AsNoTracking().Include(item => item.Folder).FirstOrDefault(item => item.FileId == fileId);
|
file = db.File.AsNoTracking().Include(item => item.Folder).FirstOrDefault(item => item.FileId == fileId);
|
||||||
}
|
}
|
||||||
if (file != null)
|
if (file != null)
|
||||||
{
|
{
|
||||||
|
@ -99,7 +103,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public File GetFile(int folderId, string fileName)
|
public File GetFile(int folderId, string fileName)
|
||||||
{
|
{
|
||||||
var file = _db.File.AsNoTracking()
|
using var db = _dbContextFactory.CreateDbContext(); var file = db.File.AsNoTracking()
|
||||||
.Include(item => item.Folder)
|
.Include(item => item.Folder)
|
||||||
.FirstOrDefault(item => item.FolderId == folderId &&
|
.FirstOrDefault(item => item.FolderId == folderId &&
|
||||||
item.Name.ToLower() == fileName);
|
item.Name.ToLower() == fileName);
|
||||||
|
@ -115,7 +119,8 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public File GetFile(int siteId, string folderPath, string fileName)
|
public File GetFile(int siteId, string folderPath, string fileName)
|
||||||
{
|
{
|
||||||
var file = _db.File.AsNoTracking()
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var file = db.File.AsNoTracking()
|
||||||
.Include(item => item.Folder)
|
.Include(item => item.Folder)
|
||||||
.FirstOrDefault(item => item.Folder.SiteId == siteId &&
|
.FirstOrDefault(item => item.Folder.SiteId == siteId &&
|
||||||
item.Folder.Path.ToLower() == folderPath &&
|
item.Folder.Path.ToLower() == folderPath &&
|
||||||
|
@ -123,7 +128,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
if (file != null)
|
if (file != null)
|
||||||
{
|
{
|
||||||
IEnumerable<Permission> permissions = _permissions.GetPermissions(file.Folder.SiteId, EntityNames.Folder, file.FolderId).ToList();
|
var permissions = _permissions.GetPermissions(file.Folder.SiteId, EntityNames.Folder, file.FolderId).ToList();
|
||||||
file.Folder.PermissionList = permissions.ToList();
|
file.Folder.PermissionList = permissions.ToList();
|
||||||
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
file.Url = GetFileUrl(file, _tenants.GetAlias());
|
||||||
}
|
}
|
||||||
|
@ -133,21 +138,24 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeleteFile(int fileId)
|
public void DeleteFile(int fileId)
|
||||||
{
|
{
|
||||||
File file = _db.File.Find(fileId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.File.Remove(file);
|
var file = db.File.Find(fileId);
|
||||||
_db.SaveChanges();
|
db.File.Remove(file);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFilePath(int fileId)
|
public string GetFilePath(int fileId)
|
||||||
{
|
{
|
||||||
var file = _db.File.Find(fileId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var file = db.File.Find(fileId);
|
||||||
return GetFilePath(file);
|
return GetFilePath(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFilePath(File file)
|
public string GetFilePath(File file)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (file == null) return null;
|
if (file == null) return null;
|
||||||
var folder = file.Folder ?? _db.Folder.AsNoTracking().FirstOrDefault(item => item.FolderId == file.FolderId);
|
var folder = file.Folder ?? db.Folder.AsNoTracking().FirstOrDefault(item => item.FolderId == file.FolderId);
|
||||||
var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name);
|
var filepath = Path.Combine(_folderRepository.GetFolderPath(folder), file.Name);
|
||||||
return filepath;
|
return filepath;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,14 +11,16 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class FolderRepository : IFolderRepository
|
public class FolderRepository : IFolderRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IPermissionRepository _permissions;
|
private readonly IPermissionRepository _permissions;
|
||||||
private readonly IWebHostEnvironment _environment;
|
private readonly IWebHostEnvironment _environment;
|
||||||
private readonly ITenantManager _tenants;
|
private readonly ITenantManager _tenants;
|
||||||
|
|
||||||
public FolderRepository(TenantDBContext context, IPermissionRepository permissions,IWebHostEnvironment environment, ITenantManager tenants)
|
public FolderRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IPermissionRepository permissions,IWebHostEnvironment environment, ITenantManager tenants)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_permissions = permissions;
|
_permissions = permissions;
|
||||||
_environment = environment;
|
_environment = environment;
|
||||||
_tenants = tenants;
|
_tenants = tenants;
|
||||||
|
@ -26,9 +28,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<Folder> GetFolders(int siteId)
|
public IEnumerable<Folder> GetFolders(int siteId)
|
||||||
{
|
{
|
||||||
IEnumerable<Permission> permissions = _permissions.GetPermissions(siteId, EntityNames.Folder).ToList();
|
var permissions = _permissions.GetPermissions(siteId, EntityNames.Folder).ToList();
|
||||||
IEnumerable<Folder> folders = _db.Folder.Where(item => item.SiteId == siteId);
|
var folders = _queryContext.Folder.Where(item => item.SiteId == siteId);
|
||||||
foreach(Folder folder in folders)
|
foreach (var folder in folders)
|
||||||
{
|
{
|
||||||
folder.PermissionList = permissions.Where(item => item.EntityId == folder.FolderId).ToList();
|
folder.PermissionList = permissions.Where(item => item.EntityId == folder.FolderId).ToList();
|
||||||
}
|
}
|
||||||
|
@ -37,17 +39,19 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Folder AddFolder(Folder folder)
|
public Folder AddFolder(Folder folder)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
folder.IsDeleted = false;
|
folder.IsDeleted = false;
|
||||||
_db.Folder.Add(folder);
|
db.Folder.Add(folder);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
|
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Folder UpdateFolder(Folder folder)
|
public Folder UpdateFolder(Folder folder)
|
||||||
{
|
{
|
||||||
_db.Entry(folder).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(folder).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
|
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
@ -59,14 +63,15 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Folder GetFolder(int folderId, bool tracking)
|
public Folder GetFolder(int folderId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
Folder folder;
|
Folder folder;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
folder = _db.Folder.Find(folderId);
|
folder = db.Folder.Find(folderId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
folder = _db.Folder.AsNoTracking().Where(item => item.FolderId == folderId).FirstOrDefault();
|
folder = db.Folder.AsNoTracking().Where(item => item.FolderId == folderId).FirstOrDefault();
|
||||||
}
|
}
|
||||||
if (folder != null)
|
if (folder != null)
|
||||||
{
|
{
|
||||||
|
@ -77,7 +82,8 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Folder GetFolder(int siteId, string path)
|
public Folder GetFolder(int siteId, string path)
|
||||||
{
|
{
|
||||||
Folder folder = _db.Folder.Where(item => item.SiteId == siteId && item.Path == path).FirstOrDefault();
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var folder = db.Folder.Where(item => item.SiteId == siteId && item.Path == path).FirstOrDefault();
|
||||||
if (folder != null)
|
if (folder != null)
|
||||||
{
|
{
|
||||||
folder.PermissionList = _permissions.GetPermissions(folder.SiteId, EntityNames.Folder, folder.FolderId)?.ToList();
|
folder.PermissionList = _permissions.GetPermissions(folder.SiteId, EntityNames.Folder, folder.FolderId)?.ToList();
|
||||||
|
@ -87,15 +93,17 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeleteFolder(int folderId)
|
public void DeleteFolder(int folderId)
|
||||||
{
|
{
|
||||||
Folder folder = _db.Folder.Find(folderId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var folder = db.Folder.Find(folderId);
|
||||||
_permissions.DeletePermissions(folder.SiteId, EntityNames.Folder, folderId);
|
_permissions.DeletePermissions(folder.SiteId, EntityNames.Folder, folderId);
|
||||||
_db.Folder.Remove(folder);
|
db.Folder.Remove(folder);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFolderPath(int folderId)
|
public string GetFolderPath(int folderId)
|
||||||
{
|
{
|
||||||
Folder folder = _db.Folder.Find(folderId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var folder = db.Folder.Find(folderId);
|
||||||
return GetFolderPath(folder);
|
return GetFolderPath(folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,64 +7,70 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class LanguageRepository : ILanguageRepository
|
public class LanguageRepository : ILanguageRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
|
|
||||||
public LanguageRepository(TenantDBContext context)
|
public LanguageRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Language> GetLanguages(int siteId)
|
public IEnumerable<Language> GetLanguages(int siteId)
|
||||||
{
|
{
|
||||||
return _db.Language.Where(l => l.SiteId == siteId);
|
return _queryContext.Language.Where(l => l.SiteId == siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Language AddLanguage(Language language)
|
public Language AddLanguage(Language language)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (language.IsDefault)
|
if (language.IsDefault)
|
||||||
{
|
{
|
||||||
// Ensure all other languages are not set to default
|
// Ensure all other languages are not set to default
|
||||||
_db.Language
|
db.Language
|
||||||
.Where(l => l.SiteId == language.SiteId)
|
.Where(l => l.SiteId == language.SiteId)
|
||||||
.ToList()
|
.ToList()
|
||||||
.ForEach(l => l.IsDefault = false);
|
.ForEach(l => l.IsDefault = false);
|
||||||
}
|
}
|
||||||
|
|
||||||
_db.Language.Add(language);
|
db.Language.Add(language);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateLanguage(Language language)
|
public void UpdateLanguage(Language language)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (language.LanguageId != 0)
|
if (language.LanguageId != 0)
|
||||||
{
|
{
|
||||||
_db.Entry(language).State = EntityState.Modified;
|
db.Entry(language).State = EntityState.Modified;
|
||||||
}
|
}
|
||||||
if (language.IsDefault)
|
if (language.IsDefault)
|
||||||
{
|
{
|
||||||
// Ensure all other languages are not set to default
|
// Ensure all other languages are not set to default
|
||||||
_db.Language
|
db.Language
|
||||||
.Where(l => l.SiteId == language.SiteId &&
|
.Where(l => l.SiteId == language.SiteId &&
|
||||||
l.LanguageId != language.LanguageId)
|
l.LanguageId != language.LanguageId)
|
||||||
.ToList()
|
.ToList()
|
||||||
.ForEach(l => l.IsDefault = false);
|
.ForEach(l => l.IsDefault = false);
|
||||||
}
|
}
|
||||||
|
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Language GetLanguage(int languageId)
|
public Language GetLanguage(int languageId)
|
||||||
{
|
{
|
||||||
return _db.Language.Find(languageId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
return db.Language.Find(languageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteLanguage(int languageId)
|
public void DeleteLanguage(int languageId)
|
||||||
{
|
{
|
||||||
var language = _db.Language.Find(languageId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Language.Remove(language);
|
var language = db.Language.Find(languageId);
|
||||||
_db.SaveChanges();
|
db.Language.Remove(language);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
|
|
||||||
namespace Oqtane.Repository
|
namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class LogRepository : ILogRepository
|
public class LogRepository : ILogRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
|
|
||||||
public LogRepository(TenantDBContext context)
|
public LogRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows)
|
public IEnumerable<Log> GetLogs(int siteId, string level, string function, int rows)
|
||||||
|
@ -20,48 +23,51 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
if (function == null)
|
if (function == null)
|
||||||
{
|
{
|
||||||
return _db.Log.Where(item => item.SiteId == siteId).
|
return _queryContext.Log.Where(item => item.SiteId == siteId).
|
||||||
OrderByDescending(item => item.LogDate).Take(rows);
|
OrderByDescending(item => item.LogDate).Take(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _db.Log.Where(item => item.SiteId == siteId && item.Function == function).
|
return _queryContext.Log.Where(item => item.SiteId == siteId && item.Function == function).
|
||||||
OrderByDescending(item => item.LogDate).Take(rows);
|
OrderByDescending(item => item.LogDate).Take(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (function == null)
|
if (function == null)
|
||||||
{
|
{
|
||||||
return _db.Log.Where(item => item.SiteId == siteId && item.Level == level)
|
return _queryContext.Log.Where(item => item.SiteId == siteId && item.Level == level)
|
||||||
.OrderByDescending(item => item.LogDate).Take(rows);
|
.OrderByDescending(item => item.LogDate).Take(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _db.Log.Where(item => item.SiteId == siteId && item.Level == level && item.Function == function)
|
return _queryContext.Log.Where(item => item.SiteId == siteId && item.Level == level && item.Function == function)
|
||||||
.OrderByDescending(item => item.LogDate).Take(rows);
|
.OrderByDescending(item => item.LogDate).Take(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Log GetLog(int logId)
|
public Log GetLog(int logId)
|
||||||
{
|
{
|
||||||
return _db.Log.Find(logId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
return db.Log.Find(logId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddLog(Log log)
|
public void AddLog(Log log)
|
||||||
{
|
{
|
||||||
_db.Log.Add(log);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Log.Add(log);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int DeleteLogs(int siteId, int age)
|
public int DeleteLogs(int siteId, int age)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
// delete logs in batches of 100 records
|
// delete logs in batches of 100 records
|
||||||
int count = 0;
|
var count = 0;
|
||||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||||
var logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
var logs = db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
||||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||||
while (logs.Count > 0)
|
while (logs.Count > 0)
|
||||||
{
|
{
|
||||||
count += logs.Count;
|
count += logs.Count;
|
||||||
_db.Log.RemoveRange(logs);
|
db.Log.RemoveRange(logs);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
logs = _db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
logs = db.Log.Where(item => item.SiteId == siteId && item.Level != "Error" && item.LogDate < purgedate)
|
||||||
.OrderBy(item => item.LogDate).Take(100).ToList();
|
.OrderBy(item => item.LogDate).Take(100).ToList();
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -14,15 +14,17 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class ModuleRepository : IModuleRepository
|
public class ModuleRepository : IModuleRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IPermissionRepository _permissions;
|
private readonly IPermissionRepository _permissions;
|
||||||
private readonly ISettingRepository _settings;
|
private readonly ISettingRepository _settings;
|
||||||
private readonly IModuleDefinitionRepository _moduleDefinitions;
|
private readonly IModuleDefinitionRepository _moduleDefinitions;
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public ModuleRepository(TenantDBContext context, IPermissionRepository permissions, ISettingRepository settings, IModuleDefinitionRepository moduleDefinitions, IServiceProvider serviceProvider)
|
public ModuleRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IPermissionRepository permissions, ISettingRepository settings, IModuleDefinitionRepository moduleDefinitions, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_permissions = permissions;
|
_permissions = permissions;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
_moduleDefinitions = moduleDefinitions;
|
_moduleDefinitions = moduleDefinitions;
|
||||||
|
@ -31,21 +33,23 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<Module> GetModules(int siteId)
|
public IEnumerable<Module> GetModules(int siteId)
|
||||||
{
|
{
|
||||||
return _db.Module.Where(item => item.SiteId == siteId).ToList();
|
return _queryContext.Module.Where(item => item.SiteId == siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Module AddModule(Module module)
|
public Module AddModule(Module module)
|
||||||
{
|
{
|
||||||
_db.Module.Add(module);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Module.Add(module);
|
||||||
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(module.SiteId, EntityNames.Module, module.ModuleId, module.PermissionList);
|
_permissions.UpdatePermissions(module.SiteId, EntityNames.Module, module.ModuleId, module.PermissionList);
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Module UpdateModule(Module module)
|
public Module UpdateModule(Module module)
|
||||||
{
|
{
|
||||||
_db.Entry(module).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(module).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(module.SiteId, EntityNames.Module, module.ModuleId, module.PermissionList);
|
_permissions.UpdatePermissions(module.SiteId, EntityNames.Module, module.ModuleId, module.PermissionList);
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
@ -57,14 +61,15 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Module GetModule(int moduleId, bool tracking)
|
public Module GetModule(int moduleId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
Module module;
|
Module module;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
module = _db.Module.Find(moduleId);
|
module = db.Module.Find(moduleId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
module = _db.Module.AsNoTracking().FirstOrDefault(item => item.ModuleId == moduleId);
|
module = db.Module.AsNoTracking().FirstOrDefault(item => item.ModuleId == moduleId);
|
||||||
}
|
}
|
||||||
if (module != null)
|
if (module != null)
|
||||||
{
|
{
|
||||||
|
@ -75,11 +80,12 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeleteModule(int moduleId)
|
public void DeleteModule(int moduleId)
|
||||||
{
|
{
|
||||||
Module module = _db.Module.Find(moduleId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var module = db.Module.Find(moduleId);
|
||||||
_permissions.DeletePermissions(module.SiteId, EntityNames.Module, moduleId);
|
_permissions.DeletePermissions(module.SiteId, EntityNames.Module, moduleId);
|
||||||
_settings.DeleteSettings(EntityNames.Module, moduleId);
|
_settings.DeleteSettings(EntityNames.Module, moduleId);
|
||||||
_db.Module.Remove(module);
|
db.Module.Remove(module);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ExportModule(int moduleId)
|
public string ExportModule(int moduleId)
|
||||||
|
|
|
@ -8,25 +8,26 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class NotificationRepository : INotificationRepository
|
public class NotificationRepository : INotificationRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
|
||||||
public NotificationRepository(TenantDBContext context)
|
public NotificationRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId)
|
public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (toUserId == -1 && fromUserId == -1)
|
if (toUserId == -1 && fromUserId == -1)
|
||||||
{
|
{
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
||||||
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
||||||
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
||||||
|
@ -35,9 +36,10 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId, int count, bool isRead)
|
public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId, int count, bool isRead)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (toUserId == -1 && fromUserId == -1)
|
if (toUserId == -1 && fromUserId == -1)
|
||||||
{
|
{
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
||||||
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
||||||
|
@ -47,7 +49,7 @@ namespace Oqtane.Repository
|
||||||
.Take(count);
|
.Take(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
||||||
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
||||||
|
@ -59,9 +61,10 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public int GetNotificationCount(int siteId, int fromUserId, int toUserId, bool isRead)
|
public int GetNotificationCount(int siteId, int fromUserId, int toUserId, bool isRead)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (toUserId == -1 && fromUserId == -1)
|
if (toUserId == -1 && fromUserId == -1)
|
||||||
{
|
{
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
||||||
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
||||||
|
@ -71,7 +74,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _db.Notification
|
return db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
.Where(item => item.ToUserId == toUserId || toUserId == -1)
|
||||||
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
|
||||||
|
@ -83,15 +86,17 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Notification AddNotification(Notification notification)
|
public Notification AddNotification(Notification notification)
|
||||||
{
|
{
|
||||||
_db.Notification.Add(notification);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Notification.Add(notification);
|
||||||
|
db.SaveChanges();
|
||||||
return notification;
|
return notification;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Notification UpdateNotification(Notification notification)
|
public Notification UpdateNotification(Notification notification)
|
||||||
{
|
{
|
||||||
_db.Entry(notification).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(notification).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return notification;
|
return notification;
|
||||||
}
|
}
|
||||||
public Notification GetNotification(int notificationId)
|
public Notification GetNotification(int notificationId)
|
||||||
|
@ -101,36 +106,39 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Notification GetNotification(int notificationId, bool tracking)
|
public Notification GetNotification(int notificationId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.Notification.Find(notificationId);
|
return db.Notification.Find(notificationId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.Notification.AsNoTracking().FirstOrDefault(item => item.NotificationId == notificationId);
|
return db.Notification.AsNoTracking().FirstOrDefault(item => item.NotificationId == notificationId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteNotification(int notificationId)
|
public void DeleteNotification(int notificationId)
|
||||||
{
|
{
|
||||||
Notification notification = _db.Notification.Find(notificationId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Notification.Remove(notification);
|
var notification = db.Notification.Find(notificationId);
|
||||||
_db.SaveChanges();
|
db.Notification.Remove(notification);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int DeleteNotifications(int siteId, int age)
|
public int DeleteNotifications(int siteId, int age)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
// delete notifications in batches of 100 records
|
// delete notifications in batches of 100 records
|
||||||
int count = 0;
|
var count = 0;
|
||||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||||
var notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
var notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
||||||
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
||||||
while (notifications.Count > 0)
|
while (notifications.Count > 0)
|
||||||
{
|
{
|
||||||
count += notifications.Count;
|
count += notifications.Count;
|
||||||
_db.Notification.RemoveRange(notifications);
|
db.Notification.RemoveRange(notifications);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
notifications = _db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate)
|
||||||
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
.OrderBy(item => item.DeliveredOn).Take(100).ToList();
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -27,8 +27,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<PageModule> GetPageModules(int siteId)
|
public IEnumerable<PageModule> GetPageModules(int siteId)
|
||||||
{
|
{
|
||||||
using(var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
|
||||||
var pagemodules = db.PageModule
|
var pagemodules = db.PageModule
|
||||||
.Include(item => item.Module) // eager load modules
|
.Include(item => item.Module) // eager load modules
|
||||||
.Where(item => item.Module.SiteId == siteId).ToList();
|
.Where(item => item.Module.SiteId == siteId).ToList();
|
||||||
|
@ -44,27 +43,21 @@ namespace Oqtane.Repository
|
||||||
return pagemodules;
|
return pagemodules;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public PageModule AddPageModule(PageModule pageModule)
|
public PageModule AddPageModule(PageModule pageModule)
|
||||||
{
|
{
|
||||||
using (var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
|
||||||
db.PageModule.Add(pageModule);
|
db.PageModule.Add(pageModule);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
return pageModule;
|
return pageModule;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public PageModule UpdatePageModule(PageModule pageModule)
|
public PageModule UpdatePageModule(PageModule pageModule)
|
||||||
{
|
{
|
||||||
using (var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
|
||||||
db.Entry(pageModule).State = EntityState.Modified;
|
db.Entry(pageModule).State = EntityState.Modified;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
return pageModule;
|
return pageModule;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public PageModule GetPageModule(int pageModuleId)
|
public PageModule GetPageModule(int pageModuleId)
|
||||||
{
|
{
|
||||||
|
@ -73,8 +66,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public PageModule GetPageModule(int pageModuleId, bool tracking)
|
public PageModule GetPageModule(int pageModuleId, bool tracking)
|
||||||
{
|
{
|
||||||
using (var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
|
||||||
PageModule pagemodule;
|
PageModule pagemodule;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
|
@ -94,13 +86,11 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
return pagemodule;
|
return pagemodule;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public PageModule GetPageModule(int pageId, int moduleId)
|
public PageModule GetPageModule(int pageId, int moduleId)
|
||||||
{
|
{
|
||||||
using (var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
var pagemodule = db.PageModule.Include(item => item.Module) // eager load modules
|
||||||
PageModule pagemodule = db.PageModule.Include(item => item.Module) // eager load modules
|
|
||||||
.SingleOrDefault(item => item.PageId == pageId && item.ModuleId == moduleId);
|
.SingleOrDefault(item => item.PageId == pageId && item.ModuleId == moduleId);
|
||||||
if (pagemodule != null)
|
if (pagemodule != null)
|
||||||
{
|
{
|
||||||
|
@ -110,13 +100,11 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
return pagemodule;
|
return pagemodule;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void DeletePageModule(int pageModuleId)
|
public void DeletePageModule(int pageModuleId)
|
||||||
{
|
{
|
||||||
using (var db = _dbContextFactory.CreateDbContext())
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
{
|
var pageModule = db.PageModule.Include(item => item.Module) // eager load modules
|
||||||
PageModule pageModule = db.PageModule.Include(item => item.Module) // eager load modules
|
|
||||||
.SingleOrDefault(item => item.PageModuleId == pageModuleId);
|
.SingleOrDefault(item => item.PageModuleId == pageModuleId);
|
||||||
_settings.DeleteSettings(EntityNames.PageModule, pageModuleId);
|
_settings.DeleteSettings(EntityNames.PageModule, pageModuleId);
|
||||||
db.PageModule.Remove(pageModule);
|
db.PageModule.Remove(pageModule);
|
||||||
|
@ -129,7 +117,6 @@ namespace Oqtane.Repository
|
||||||
_modules.DeleteModule(pageModule.ModuleId);
|
_modules.DeleteModule(pageModule.ModuleId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private PageModule GetPageModule(PageModule pageModule, List<ModuleDefinition> moduleDefinitions, List<Permission> modulePermissions)
|
private PageModule GetPageModule(PageModule pageModule, List<ModuleDefinition> moduleDefinitions, List<Permission> modulePermissions)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,14 +9,16 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class PageRepository : IPageRepository
|
public class PageRepository : IPageRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IPageModuleRepository _pageModules;
|
private readonly IPageModuleRepository _pageModules;
|
||||||
private readonly IPermissionRepository _permissions;
|
private readonly IPermissionRepository _permissions;
|
||||||
private readonly ISettingRepository _settings;
|
private readonly ISettingRepository _settings;
|
||||||
|
|
||||||
public PageRepository(TenantDBContext context, IPageModuleRepository pageModules, IPermissionRepository permissions, ISettingRepository settings)
|
public PageRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IPageModuleRepository pageModules, IPermissionRepository permissions, ISettingRepository settings)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_pageModules = pageModules;
|
_pageModules = pageModules;
|
||||||
_permissions = permissions;
|
_permissions = permissions;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
|
@ -24,9 +26,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<Page> GetPages(int siteId)
|
public IEnumerable<Page> GetPages(int siteId)
|
||||||
{
|
{
|
||||||
IEnumerable<Permission> permissions = _permissions.GetPermissions(siteId, EntityNames.Page).ToList();
|
var permissions = _permissions.GetPermissions(siteId, EntityNames.Page).ToList();
|
||||||
IEnumerable<Page> pages = _db.Page.Where(item => item.SiteId == siteId && item.UserId == null);
|
var pages = _queryContext.Page.Where(item => item.SiteId == siteId && item.UserId == null);
|
||||||
foreach(Page page in pages)
|
foreach (var page in pages)
|
||||||
{
|
{
|
||||||
page.PermissionList = permissions.Where(item => item.EntityId == page.PageId).ToList();
|
page.PermissionList = permissions.Where(item => item.EntityId == page.PageId).ToList();
|
||||||
}
|
}
|
||||||
|
@ -35,16 +37,18 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Page AddPage(Page page)
|
public Page AddPage(Page page)
|
||||||
{
|
{
|
||||||
_db.Page.Add(page);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Page.Add(page);
|
||||||
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(page.SiteId, EntityNames.Page, page.PageId, page.PermissionList);
|
_permissions.UpdatePermissions(page.SiteId, EntityNames.Page, page.PageId, page.PermissionList);
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page UpdatePage(Page page)
|
public Page UpdatePage(Page page)
|
||||||
{
|
{
|
||||||
_db.Entry(page).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(page).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
_permissions.UpdatePermissions(page.SiteId, EntityNames.Page, page.PageId, page.PermissionList);
|
_permissions.UpdatePermissions(page.SiteId, EntityNames.Page, page.PageId, page.PermissionList);
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
@ -56,15 +60,16 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Page GetPage(int pageId, bool tracking)
|
public Page GetPage(int pageId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
Page page;
|
Page page;
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
page = _db.Page.Find(pageId);
|
page = db.Page.Find(pageId);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
page = _db.Page.AsNoTracking().FirstOrDefault(item => item.PageId == pageId);
|
page = db.Page.AsNoTracking().FirstOrDefault(item => item.PageId == pageId);
|
||||||
}
|
}
|
||||||
if (page != null)
|
if (page != null)
|
||||||
{
|
{
|
||||||
|
@ -75,7 +80,8 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Page GetPage(string path, int siteId)
|
public Page GetPage(string path, int siteId)
|
||||||
{
|
{
|
||||||
Page page = _db.Page.FirstOrDefault(item => item.Path == path && item.SiteId == siteId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var page = db.Page.FirstOrDefault(item => item.Path == path && item.SiteId == siteId);
|
||||||
if (page != null)
|
if (page != null)
|
||||||
{
|
{
|
||||||
page.PermissionList = _permissions.GetPermissions(page.SiteId, EntityNames.Page, page.PageId)?.ToList();
|
page.PermissionList = _permissions.GetPermissions(page.SiteId, EntityNames.Page, page.PageId)?.ToList();
|
||||||
|
@ -85,18 +91,19 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeletePage(int pageId)
|
public void DeletePage(int pageId)
|
||||||
{
|
{
|
||||||
Page page = _db.Page.Find(pageId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var page = db.Page.Find(pageId);
|
||||||
_permissions.DeletePermissions(page.SiteId, EntityNames.Page, pageId);
|
_permissions.DeletePermissions(page.SiteId, EntityNames.Page, pageId);
|
||||||
_settings.DeleteSettings(EntityNames.Page, pageId);
|
_settings.DeleteSettings(EntityNames.Page, pageId);
|
||||||
// remove page modules for page
|
// remove page modules for page
|
||||||
var pageModules = _db.PageModule.Where(item => item.PageId == pageId).ToList();
|
var pageModules = db.PageModule.Where(item => item.PageId == pageId).ToList();
|
||||||
foreach (var pageModule in pageModules)
|
foreach (var pageModule in pageModules)
|
||||||
{
|
{
|
||||||
_pageModules.DeletePageModule(pageModule.PageModuleId);
|
_pageModules.DeletePageModule(pageModule.PageModuleId);
|
||||||
}
|
}
|
||||||
// must occur after page modules are deleted because of cascading delete relationship
|
// must occur after page modules are deleted because of cascading delete relationship
|
||||||
_db.Page.Remove(page);
|
db.Page.Remove(page);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,14 +10,14 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class PermissionRepository : IPermissionRepository
|
public class PermissionRepository : IPermissionRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
private readonly IRoleRepository _roles;
|
private readonly IRoleRepository _roles;
|
||||||
private readonly IMemoryCache _cache;
|
private readonly IMemoryCache _cache;
|
||||||
private readonly SiteState _siteState;
|
private readonly SiteState _siteState;
|
||||||
|
|
||||||
public PermissionRepository(TenantDBContext context, IRoleRepository roles, IMemoryCache cache, SiteState siteState)
|
public PermissionRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IRoleRepository roles, IMemoryCache cache, SiteState siteState)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
_roles = roles;
|
_roles = roles;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_siteState = siteState;
|
_siteState = siteState;
|
||||||
|
@ -25,13 +25,14 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<Permission> GetPermissions(int siteId, string entityName)
|
public IEnumerable<Permission> GetPermissions(int siteId, string entityName)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
var alias = _siteState?.Alias;
|
var alias = _siteState?.Alias;
|
||||||
if (alias != null)
|
if (alias != null)
|
||||||
{
|
{
|
||||||
return _cache.GetOrCreate($"permissions:{alias.TenantId}:{siteId}:{entityName}", entry =>
|
return _cache.GetOrCreate($"permissions:{alias.TenantId}:{siteId}:{entityName}", entry =>
|
||||||
{
|
{
|
||||||
var roles = _roles.GetRoles(siteId, true).ToList();
|
var roles = _roles.GetRoles(siteId, true).ToList();
|
||||||
var permissions = _db.Permission.Where(item => item.SiteId == siteId).Where(item => item.EntityName == entityName).ToList();
|
var permissions = db.Permission.Where(item => item.SiteId == siteId).Where(item => item.EntityName == entityName).ToList();
|
||||||
foreach (var permission in permissions)
|
foreach (var permission in permissions)
|
||||||
{
|
{
|
||||||
if (permission.RoleId != null && string.IsNullOrEmpty(permission.RoleName))
|
if (permission.RoleId != null && string.IsNullOrEmpty(permission.RoleName))
|
||||||
|
@ -69,24 +70,27 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Permission AddPermission(Permission permission)
|
public Permission AddPermission(Permission permission)
|
||||||
{
|
{
|
||||||
_db.Permission.Add(permission);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Permission.Add(permission);
|
||||||
|
db.SaveChanges();
|
||||||
ClearCache(permission.SiteId, permission.EntityName);
|
ClearCache(permission.SiteId, permission.EntityName);
|
||||||
return permission;
|
return permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Permission UpdatePermission(Permission permission)
|
public Permission UpdatePermission(Permission permission)
|
||||||
{
|
{
|
||||||
_db.Entry(permission).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(permission).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
ClearCache(permission.SiteId, permission.EntityName);
|
ClearCache(permission.SiteId, permission.EntityName);
|
||||||
return permission;
|
return permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdatePermissions(int siteId, string entityName, int entityId, List<Permission> permissions)
|
public void UpdatePermissions(int siteId, string entityName, int entityId, List<Permission> permissions)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
// ensure permissions are fully populated
|
// ensure permissions are fully populated
|
||||||
List<Role> roles = _roles.GetRoles(siteId, true).ToList();
|
var roles = _roles.GetRoles(siteId, true).ToList();
|
||||||
foreach (var permission in permissions)
|
foreach (var permission in permissions)
|
||||||
{
|
{
|
||||||
permission.SiteId = siteId;
|
permission.SiteId = siteId;
|
||||||
|
@ -115,13 +119,13 @@ namespace Oqtane.Repository
|
||||||
if (current.IsAuthorized != permission.IsAuthorized)
|
if (current.IsAuthorized != permission.IsAuthorized)
|
||||||
{
|
{
|
||||||
current.IsAuthorized = permission.IsAuthorized;
|
current.IsAuthorized = permission.IsAuthorized;
|
||||||
_db.Entry(current).State = EntityState.Modified;
|
db.Entry(current).State = EntityState.Modified;
|
||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_db.Permission.Add(permission);
|
db.Permission.Add(permission);
|
||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,13 +135,13 @@ namespace Oqtane.Repository
|
||||||
if (!permissions.Any(item => item.EntityName == permission.EntityName && item.PermissionName == permission.PermissionName
|
if (!permissions.Any(item => item.EntityName == permission.EntityName && item.PermissionName == permission.PermissionName
|
||||||
&& item.EntityId == permission.EntityId && item.RoleId == permission.RoleId && item.UserId == permission.UserId))
|
&& item.EntityId == permission.EntityId && item.RoleId == permission.RoleId && item.UserId == permission.UserId))
|
||||||
{
|
{
|
||||||
_db.Permission.Remove(permission);
|
db.Permission.Remove(permission);
|
||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (modified)
|
if (modified)
|
||||||
{
|
{
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
foreach (var entityname in permissions.Select(item => item.EntityName).Distinct())
|
foreach (var entityname in permissions.Select(item => item.EntityName).Distinct())
|
||||||
{
|
{
|
||||||
ClearCache(siteId, entityname);
|
ClearCache(siteId, entityname);
|
||||||
|
@ -147,28 +151,31 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Permission GetPermission(int permissionId)
|
public Permission GetPermission(int permissionId)
|
||||||
{
|
{
|
||||||
return _db.Permission.Find(permissionId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
return db.Permission.Find(permissionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeletePermission(int permissionId)
|
public void DeletePermission(int permissionId)
|
||||||
{
|
{
|
||||||
Permission permission = _db.Permission.Find(permissionId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Permission.Remove(permission);
|
var permission = db.Permission.Find(permissionId);
|
||||||
_db.SaveChanges();
|
db.Permission.Remove(permission);
|
||||||
|
db.SaveChanges();
|
||||||
ClearCache(permission.SiteId, permission.EntityName);
|
ClearCache(permission.SiteId, permission.EntityName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeletePermissions(int siteId, string entityName, int entityId)
|
public void DeletePermissions(int siteId, string entityName, int entityId)
|
||||||
{
|
{
|
||||||
IEnumerable<Permission> permissions = _db.Permission
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
var permissions = db.Permission
|
||||||
.Where(item => item.EntityName == entityName)
|
.Where(item => item.EntityName == entityName)
|
||||||
.Where(item => item.EntityId == entityId)
|
.Where(item => item.EntityId == entityId)
|
||||||
.Where(item => item.SiteId == siteId);
|
.Where(item => item.SiteId == siteId);
|
||||||
foreach (Permission permission in permissions)
|
foreach (Permission permission in permissions)
|
||||||
{
|
{
|
||||||
_db.Permission.Remove(permission);
|
db.Permission.Remove(permission);
|
||||||
}
|
}
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
ClearCache(siteId, entityName);
|
ClearCache(siteId, entityName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,29 +7,33 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class ProfileRepository : IProfileRepository
|
public class ProfileRepository : IProfileRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
|
|
||||||
public ProfileRepository(TenantDBContext context)
|
public ProfileRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Profile> GetProfiles(int siteId)
|
public IEnumerable<Profile> GetProfiles(int siteId)
|
||||||
{
|
{
|
||||||
return _db.Profile.Where(item => item.SiteId == siteId || item.SiteId == null);
|
return _queryContext.Profile.Where(item => item.SiteId == siteId || item.SiteId == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Profile AddProfile(Profile profile)
|
public Profile AddProfile(Profile profile)
|
||||||
{
|
{
|
||||||
_db.Profile.Add(profile);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Profile.Add(profile);
|
||||||
|
db.SaveChanges();
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Profile UpdateProfile(Profile profile)
|
public Profile UpdateProfile(Profile profile)
|
||||||
{
|
{
|
||||||
_db.Entry(profile).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(profile).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,21 +44,23 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Profile GetProfile(int profileId, bool tracking)
|
public Profile GetProfile(int profileId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.Profile.Find(profileId);
|
return db.Profile.Find(profileId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.Profile.AsNoTracking().FirstOrDefault(item => item.ProfileId == profileId);
|
return db.Profile.AsNoTracking().FirstOrDefault(item => item.ProfileId == profileId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteProfile(int profileId)
|
public void DeleteProfile(int profileId)
|
||||||
{
|
{
|
||||||
Profile profile = _db.Profile.Find(profileId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Profile.Remove(profile);
|
var profile = db.Profile.Find(profileId);
|
||||||
_db.SaveChanges();
|
db.Profile.Remove(profile);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,13 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class RoleRepository : IRoleRepository
|
public class RoleRepository : IRoleRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
|
|
||||||
public RoleRepository(TenantDBContext context)
|
public RoleRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Role> GetRoles(int siteId)
|
public IEnumerable<Role> GetRoles(int siteId)
|
||||||
|
@ -23,27 +25,29 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
if (includeGlobalRoles)
|
if (includeGlobalRoles)
|
||||||
{
|
{
|
||||||
return _db.Role.Where(item => item.SiteId == siteId || item.SiteId == null);
|
return _queryContext.Role.Where(item => item.SiteId == siteId || item.SiteId == null);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.Role.Where(item => item.SiteId == siteId);
|
return _queryContext.Role.Where(item => item.SiteId == siteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Role AddRole(Role role)
|
public Role AddRole(Role role)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
role.Description = role.Description.Substring(0, (role.Description.Length > 256) ? 256 : role.Description.Length);
|
role.Description = role.Description.Substring(0, (role.Description.Length > 256) ? 256 : role.Description.Length);
|
||||||
_db.Role.Add(role);
|
db.Role.Add(role);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Role UpdateRole(Role role)
|
public Role UpdateRole(Role role)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
role.Description = role.Description.Substring(0, (role.Description.Length > 256) ? 256 : role.Description.Length);
|
role.Description = role.Description.Substring(0, (role.Description.Length > 256) ? 256 : role.Description.Length);
|
||||||
_db.Entry(role).State = EntityState.Modified;
|
db.Entry(role).State = EntityState.Modified;
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,21 +58,23 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Role GetRole(int roleId, bool tracking)
|
public Role GetRole(int roleId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.Role.Find(roleId);
|
return db.Role.Find(roleId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.Role.AsNoTracking().FirstOrDefault(item => item.RoleId == roleId);
|
return db.Role.AsNoTracking().FirstOrDefault(item => item.RoleId == roleId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteRole(int roleId)
|
public void DeleteRole(int roleId)
|
||||||
{
|
{
|
||||||
Role role = _db.Role.Find(roleId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Role.Remove(role);
|
Role role = db.Role.Find(roleId);
|
||||||
_db.SaveChanges();
|
db.Role.Remove(role);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Internal;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
|
@ -10,14 +11,16 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class SettingRepository : ISettingRepository
|
public class SettingRepository : ISettingRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _tenant;
|
private readonly IDbContextFactory<TenantDBContext> _tenantContextFactory;
|
||||||
|
private readonly TenantDBContext _tenantQueryContext;
|
||||||
private MasterDBContext _master;
|
private MasterDBContext _master;
|
||||||
private readonly ITenantManager _tenantManager;
|
private readonly ITenantManager _tenantManager;
|
||||||
private readonly IMemoryCache _cache;
|
private readonly IMemoryCache _cache;
|
||||||
|
|
||||||
public SettingRepository(TenantDBContext tenant, MasterDBContext master, ITenantManager tenantManager, IMemoryCache cache)
|
public SettingRepository(IDbContextFactory<TenantDBContext> tenantContextFactory, MasterDBContext master, ITenantManager tenantManager, IMemoryCache cache)
|
||||||
{
|
{
|
||||||
_tenant = tenant;
|
_tenantContextFactory = tenantContextFactory;
|
||||||
|
_tenantQueryContext = tenantContextFactory.CreateDbContext();
|
||||||
_master = master;
|
_master = master;
|
||||||
_tenantManager = tenantManager;
|
_tenantManager = tenantManager;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
|
@ -31,7 +34,7 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _tenant.Setting.Where(item => item.EntityName == entityName);
|
return _tenantQueryContext.Setting.Where(item => item.EntityName == entityName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +46,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Setting AddSetting(Setting setting)
|
public Setting AddSetting(Setting setting)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(setting.EntityName))
|
if (IsMaster(setting.EntityName))
|
||||||
{
|
{
|
||||||
_master.Setting.Add(setting);
|
_master.Setting.Add(setting);
|
||||||
|
@ -50,8 +54,8 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_tenant.Setting.Add(setting);
|
tenant.Setting.Add(setting);
|
||||||
_tenant.SaveChanges();
|
tenant.SaveChanges();
|
||||||
}
|
}
|
||||||
ManageCache(setting.EntityName);
|
ManageCache(setting.EntityName);
|
||||||
return setting;
|
return setting;
|
||||||
|
@ -59,6 +63,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Setting UpdateSetting(Setting setting)
|
public Setting UpdateSetting(Setting setting)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(setting.EntityName))
|
if (IsMaster(setting.EntityName))
|
||||||
{
|
{
|
||||||
_master.Entry(setting).State = EntityState.Modified;
|
_master.Entry(setting).State = EntityState.Modified;
|
||||||
|
@ -66,8 +71,8 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_tenant.Entry(setting).State = EntityState.Modified;
|
tenant.Entry(setting).State = EntityState.Modified;
|
||||||
_tenant.SaveChanges();
|
tenant.SaveChanges();
|
||||||
}
|
}
|
||||||
ManageCache(setting.EntityName);
|
ManageCache(setting.EntityName);
|
||||||
return setting;
|
return setting;
|
||||||
|
@ -75,30 +80,33 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Setting GetSetting(string entityName, int settingId)
|
public Setting GetSetting(string entityName, int settingId)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(entityName))
|
if (IsMaster(entityName))
|
||||||
{
|
{
|
||||||
return _master.Setting.Find(settingId);
|
return _master.Setting.Find(settingId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _tenant.Setting.Find(settingId);
|
return tenant.Setting.Find(settingId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Setting GetSetting(string entityName, int entityId, string settingName)
|
public Setting GetSetting(string entityName, int entityId, string settingName)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(entityName))
|
if (IsMaster(entityName))
|
||||||
{
|
{
|
||||||
return _master.Setting.Where(item => item.EntityName == entityName && item.EntityId == entityId && item.SettingName == settingName).FirstOrDefault();
|
return _master.Setting.Where(item => item.EntityName == entityName && item.EntityId == entityId && item.SettingName == settingName).FirstOrDefault();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _tenant.Setting.Where(item => item.EntityName == entityName && item.EntityId == entityId && item.SettingName == settingName).FirstOrDefault();
|
return tenant.Setting.Where(item => item.EntityName == entityName && item.EntityId == entityId && item.SettingName == settingName).FirstOrDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteSetting(string entityName, int settingId)
|
public void DeleteSetting(string entityName, int settingId)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(entityName))
|
if (IsMaster(entityName))
|
||||||
{
|
{
|
||||||
Setting setting = _master.Setting.Find(settingId);
|
Setting setting = _master.Setting.Find(settingId);
|
||||||
|
@ -107,15 +115,16 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Setting setting = _tenant.Setting.Find(settingId);
|
Setting setting = tenant.Setting.Find(settingId);
|
||||||
_tenant.Setting.Remove(setting);
|
tenant.Setting.Remove(setting);
|
||||||
_tenant.SaveChanges();
|
tenant.SaveChanges();
|
||||||
}
|
}
|
||||||
ManageCache(entityName);
|
ManageCache(entityName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteSettings(string entityName, int entityId)
|
public void DeleteSettings(string entityName, int entityId)
|
||||||
{
|
{
|
||||||
|
using var tenant = _tenantContextFactory.CreateDbContext();
|
||||||
if (IsMaster(entityName))
|
if (IsMaster(entityName))
|
||||||
{
|
{
|
||||||
IEnumerable<Setting> settings = _master.Setting
|
IEnumerable<Setting> settings = _master.Setting
|
||||||
|
@ -129,14 +138,14 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
IEnumerable<Setting> settings = _tenant.Setting
|
IEnumerable<Setting> settings = tenant.Setting
|
||||||
.Where(item => item.EntityName == entityName)
|
.Where(item => item.EntityName == entityName)
|
||||||
.Where(item => item.EntityId == entityId);
|
.Where(item => item.EntityId == entityId);
|
||||||
foreach (Setting setting in settings)
|
foreach (Setting setting in settings)
|
||||||
{
|
{
|
||||||
_tenant.Setting.Remove(setting);
|
tenant.Setting.Remove(setting);
|
||||||
}
|
}
|
||||||
_tenant.SaveChanges();
|
tenant.SaveChanges();
|
||||||
}
|
}
|
||||||
ManageCache(entityName);
|
ManageCache(entityName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class SiteRepository : ISiteRepository
|
public class SiteRepository : ISiteRepository
|
||||||
{
|
{
|
||||||
private readonly TenantDBContext _db;
|
|
||||||
private readonly IDbContextFactory<TenantDBContext> _factory;
|
private readonly IDbContextFactory<TenantDBContext> _factory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IRoleRepository _roleRepository;
|
private readonly IRoleRepository _roleRepository;
|
||||||
private readonly IProfileRepository _profileRepository;
|
private readonly IProfileRepository _profileRepository;
|
||||||
private readonly IFolderRepository _folderRepository;
|
private readonly IFolderRepository _folderRepository;
|
||||||
|
@ -33,12 +33,12 @@ namespace Oqtane.Repository
|
||||||
private readonly ILogManager _logger;
|
private readonly ILogManager _logger;
|
||||||
private static readonly object _lock = new object();
|
private static readonly object _lock = new object();
|
||||||
|
|
||||||
public SiteRepository(TenantDBContext context, IDbContextFactory<TenantDBContext> factory, IRoleRepository roleRepository, IProfileRepository profileRepository, IFolderRepository folderRepository, IPageRepository pageRepository,
|
public SiteRepository(IDbContextFactory<TenantDBContext> factory, IRoleRepository roleRepository, IProfileRepository profileRepository, IFolderRepository folderRepository, IPageRepository pageRepository,
|
||||||
IModuleRepository moduleRepository, IPageModuleRepository pageModuleRepository, IModuleDefinitionRepository moduleDefinitionRepository, IThemeRepository themeRepository, IServiceProvider serviceProvider,
|
IModuleRepository moduleRepository, IPageModuleRepository pageModuleRepository, IModuleDefinitionRepository moduleDefinitionRepository, IThemeRepository themeRepository, IServiceProvider serviceProvider,
|
||||||
IConfigurationRoot config, IServerStateManager serverState, ILogManager logger)
|
IConfigurationRoot config, IServerStateManager serverState, ILogManager logger)
|
||||||
{
|
{
|
||||||
_db = context;
|
|
||||||
_factory = factory;
|
_factory = factory;
|
||||||
|
_queryContext = _factory.CreateDbContext();
|
||||||
_roleRepository = roleRepository;
|
_roleRepository = roleRepository;
|
||||||
_profileRepository = profileRepository;
|
_profileRepository = profileRepository;
|
||||||
_folderRepository = folderRepository;
|
_folderRepository = folderRepository;
|
||||||
|
@ -107,22 +107,24 @@ namespace Oqtane.Repository
|
||||||
// synchronous methods
|
// synchronous methods
|
||||||
public IEnumerable<Site> GetSites()
|
public IEnumerable<Site> GetSites()
|
||||||
{
|
{
|
||||||
return _db.Site.OrderBy(item => item.Name);
|
return _queryContext.Site.OrderBy(item => item.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Site AddSite(Site site)
|
public Site AddSite(Site site)
|
||||||
{
|
{
|
||||||
|
using var ctx = _factory.CreateDbContext();
|
||||||
site.SiteGuid = Guid.NewGuid().ToString();
|
site.SiteGuid = Guid.NewGuid().ToString();
|
||||||
_db.Site.Add(site);
|
ctx.Site.Add(site);
|
||||||
_db.SaveChanges();
|
ctx.SaveChanges();
|
||||||
CreateSite(site);
|
CreateSite(site);
|
||||||
return site;
|
return site;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Site UpdateSite(Site site)
|
public Site UpdateSite(Site site)
|
||||||
{
|
{
|
||||||
_db.Entry(site).State = EntityState.Modified;
|
using var ctx = _factory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
ctx.Entry(site).State = EntityState.Modified;
|
||||||
|
ctx.SaveChanges();
|
||||||
return site;
|
return site;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,21 +135,23 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public Site GetSite(int siteId, bool tracking)
|
public Site GetSite(int siteId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var ctx = _factory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.Site.Find(siteId);
|
return ctx.Site.Find(siteId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.Site.AsNoTracking().FirstOrDefault(item => item.SiteId == siteId);
|
return ctx.Site.AsNoTracking().FirstOrDefault(item => item.SiteId == siteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteSite(int siteId)
|
public void DeleteSite(int siteId)
|
||||||
{
|
{
|
||||||
var site = _db.Site.Find(siteId);
|
using var ctx = _factory.CreateDbContext();
|
||||||
_db.Site.Remove(site);
|
var site = ctx.Site.Find(siteId);
|
||||||
_db.SaveChanges();
|
ctx.Site.Remove(site);
|
||||||
|
ctx.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,14 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class UrlMappingRepository : IUrlMappingRepository
|
public class UrlMappingRepository : IUrlMappingRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly ISiteRepository _sites;
|
private readonly ISiteRepository _sites;
|
||||||
|
|
||||||
public UrlMappingRepository(TenantDBContext context, ISiteRepository sites)
|
public UrlMappingRepository(IDbContextFactory<TenantDBContext> dbContextFactory, ISiteRepository sites)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_sites = sites;
|
_sites = sites;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,49 +23,54 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
if (isMapped)
|
if (isMapped)
|
||||||
{
|
{
|
||||||
return _db.UrlMapping.Where(item => item.SiteId == siteId && !string.IsNullOrEmpty(item.MappedUrl)).Take(200);
|
return _queryContext.UrlMapping.Where(item => item.SiteId == siteId && !string.IsNullOrEmpty(item.MappedUrl)).Take(200);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.UrlMapping.Where(item => item.SiteId == siteId && string.IsNullOrEmpty(item.MappedUrl)).Take(200);
|
return _queryContext.UrlMapping.Where(item => item.SiteId == siteId && string.IsNullOrEmpty(item.MappedUrl)).Take(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UrlMapping AddUrlMapping(UrlMapping urlMapping)
|
public UrlMapping AddUrlMapping(UrlMapping urlMapping)
|
||||||
{
|
{
|
||||||
_db.UrlMapping.Add(urlMapping);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.UrlMapping.Add(urlMapping);
|
||||||
|
db.SaveChanges();
|
||||||
return urlMapping;
|
return urlMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UrlMapping UpdateUrlMapping(UrlMapping urlMapping)
|
public UrlMapping UpdateUrlMapping(UrlMapping urlMapping)
|
||||||
{
|
{
|
||||||
_db.Entry(urlMapping).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(urlMapping).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return urlMapping;
|
return urlMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UrlMapping GetUrlMapping(int urlMappingId)
|
public UrlMapping GetUrlMapping(int urlMappingId)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
return GetUrlMapping(urlMappingId, true);
|
return GetUrlMapping(urlMappingId, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UrlMapping GetUrlMapping(int urlMappingId, bool tracking)
|
public UrlMapping GetUrlMapping(int urlMappingId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.UrlMapping.Find(urlMappingId);
|
return db.UrlMapping.Find(urlMappingId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.UrlMapping.AsNoTracking().FirstOrDefault(item => item.UrlMappingId == urlMappingId);
|
return db.UrlMapping.AsNoTracking().FirstOrDefault(item => item.UrlMappingId == urlMappingId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UrlMapping GetUrlMapping(int siteId, string url)
|
public UrlMapping GetUrlMapping(int siteId, string url)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
url = (url.Length > 750) ? url.Substring(0, 750) : url;
|
url = (url.Length > 750) ? url.Substring(0, 750) : url;
|
||||||
var urlMapping = _db.UrlMapping.Where(item => item.SiteId == siteId && item.Url == url).FirstOrDefault();
|
var urlMapping = db.UrlMapping.Where(item => item.SiteId == siteId && item.Url == url).FirstOrDefault();
|
||||||
if (urlMapping == null)
|
if (urlMapping == null)
|
||||||
{
|
{
|
||||||
var site = _sites.GetSite(siteId);
|
var site = _sites.GetSite(siteId);
|
||||||
|
@ -90,9 +97,10 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeleteUrlMapping(int urlMappingId)
|
public void DeleteUrlMapping(int urlMappingId)
|
||||||
{
|
{
|
||||||
UrlMapping urlMapping = _db.UrlMapping.Find(urlMappingId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.UrlMapping.Remove(urlMapping);
|
UrlMapping urlMapping = db.UrlMapping.Find(urlMappingId);
|
||||||
_db.SaveChanges();
|
db.UrlMapping.Remove(urlMapping);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,16 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class UserRepository : IUserRepository
|
public class UserRepository : IUserRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IFolderRepository _folders;
|
private readonly IFolderRepository _folders;
|
||||||
private readonly IRoleRepository _roles;
|
private readonly IRoleRepository _roles;
|
||||||
private readonly IUserRoleRepository _userroles;
|
private readonly IUserRoleRepository _userroles;
|
||||||
|
|
||||||
public UserRepository(TenantDBContext context, IFolderRepository folders, IRoleRepository roles, IUserRoleRepository userroles)
|
public UserRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IFolderRepository folders, IRoleRepository roles, IUserRoleRepository userroles)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_folders = folders;
|
_folders = folders;
|
||||||
_roles = roles;
|
_roles = roles;
|
||||||
_userroles = userroles;
|
_userroles = userroles;
|
||||||
|
@ -23,25 +25,26 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<User> GetUsers()
|
public IEnumerable<User> GetUsers()
|
||||||
{
|
{
|
||||||
return _db.User;
|
return _queryContext.User;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User AddUser(User user)
|
public User AddUser(User user)
|
||||||
{
|
{
|
||||||
if (_db.User.AsNoTracking().FirstOrDefault(item => item.Username == user.Username) == null)
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
if (db.User.AsNoTracking().FirstOrDefault(item => item.Username == user.Username) == null)
|
||||||
{
|
{
|
||||||
_db.User.Add(user);
|
db.User.Add(user);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int siteId = user.SiteId;
|
int siteId = user.SiteId;
|
||||||
user = _db.User.AsNoTracking().First(item => item.Username == user.Username);
|
user = db.User.AsNoTracking().First(item => item.Username == user.Username);
|
||||||
user.SiteId = siteId;
|
user.SiteId = siteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add folder for user
|
// add folder for user
|
||||||
Folder folder = _folders.GetFolder(user.SiteId, "Users/");
|
var folder = _folders.GetFolder(user.SiteId, "Users/");
|
||||||
if (folder != null)
|
if (folder != null)
|
||||||
{
|
{
|
||||||
_folders.AddFolder(new Folder
|
_folders.AddFolder(new Folder
|
||||||
|
@ -65,10 +68,10 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
// add auto assigned roles to user for site
|
// add auto assigned roles to user for site
|
||||||
List<Role> roles = _roles.GetRoles(user.SiteId).Where(item => item.IsAutoAssigned).ToList();
|
var roles = _roles.GetRoles(user.SiteId).Where(item => item.IsAutoAssigned).ToList();
|
||||||
foreach (Role role in roles)
|
foreach (var role in roles)
|
||||||
{
|
{
|
||||||
UserRole userrole = new UserRole();
|
var userrole = new UserRole();
|
||||||
userrole.UserId = user.UserId;
|
userrole.UserId = user.UserId;
|
||||||
userrole.RoleId = role.RoleId;
|
userrole.RoleId = role.RoleId;
|
||||||
userrole.EffectiveDate = null;
|
userrole.EffectiveDate = null;
|
||||||
|
@ -81,8 +84,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public User UpdateUser(User user)
|
public User UpdateUser(User user)
|
||||||
{
|
{
|
||||||
_db.Entry(user).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(user).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,13 +97,14 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public User GetUser(int userId, bool tracking)
|
public User GetUser(int userId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.User.Find(userId);
|
return db.User.Find(userId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.User.AsNoTracking().FirstOrDefault(item => item.UserId == userId);
|
return db.User.AsNoTracking().FirstOrDefault(item => item.UserId == userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,23 +115,25 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public User GetUser(string username, string email)
|
public User GetUser(string username, string email)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
User user = null;
|
User user = null;
|
||||||
if (!string.IsNullOrEmpty(username))
|
if (!string.IsNullOrEmpty(username))
|
||||||
{
|
{
|
||||||
user = _db.User.Where(item => item.Username == username).FirstOrDefault();
|
user = db.User.Where(item => item.Username == username).FirstOrDefault();
|
||||||
}
|
}
|
||||||
if (user == null && !string.IsNullOrEmpty(email))
|
if (user == null && !string.IsNullOrEmpty(email))
|
||||||
{
|
{
|
||||||
user = _db.User.Where(item => item.Email == email).FirstOrDefault();
|
user = db.User.Where(item => item.Email == email).FirstOrDefault();
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteUser(int userId)
|
public void DeleteUser(int userId)
|
||||||
{
|
{
|
||||||
User user = _db.User.Find(userId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.User.Remove(user);
|
var user = db.User.Find(userId);
|
||||||
_db.SaveChanges();
|
db.User.Remove(user);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,20 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class UserRoleRepository : IUserRoleRepository
|
public class UserRoleRepository : IUserRoleRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
private readonly IRoleRepository _roles;
|
private readonly IRoleRepository _roles;
|
||||||
|
|
||||||
public UserRoleRepository(TenantDBContext context, IRoleRepository roles)
|
public UserRoleRepository(IDbContextFactory<TenantDBContext> dbContextFactory, IRoleRepository roles)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
_roles = roles;
|
_roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<UserRole> GetUserRoles(int siteId)
|
public IEnumerable<UserRole> GetUserRoles(int siteId)
|
||||||
{
|
{
|
||||||
return _db.UserRole
|
return _queryContext.UserRole
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null);
|
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null);
|
||||||
|
@ -27,7 +29,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public IEnumerable<UserRole> GetUserRoles(int userId, int siteId)
|
public IEnumerable<UserRole> GetUserRoles(int userId, int siteId)
|
||||||
{
|
{
|
||||||
return _db.UserRole.Where(item => item.UserId == userId)
|
return _queryContext.UserRole.Where(item => item.UserId == userId)
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1);
|
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1);
|
||||||
|
@ -35,8 +37,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public UserRole AddUserRole(UserRole userRole)
|
public UserRole AddUserRole(UserRole userRole)
|
||||||
{
|
{
|
||||||
_db.UserRole.Add(userRole);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.UserRole.Add(userRole);
|
||||||
|
db.SaveChanges();
|
||||||
|
|
||||||
// host roles can only exist at global level - remove any site specific user roles
|
// host roles can only exist at global level - remove any site specific user roles
|
||||||
var role = _roles.GetRole(userRole.RoleId);
|
var role = _roles.GetRole(userRole.RoleId);
|
||||||
|
@ -50,8 +53,9 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public UserRole UpdateUserRole(UserRole userRole)
|
public UserRole UpdateUserRole(UserRole userRole)
|
||||||
{
|
{
|
||||||
_db.Entry(userRole).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(userRole).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return userRole;
|
return userRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,16 +66,17 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public UserRole GetUserRole(int userRoleId, bool tracking)
|
public UserRole GetUserRole(int userRoleId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.UserRole
|
return db.UserRole
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.FirstOrDefault(item => item.UserRoleId == userRoleId);
|
.FirstOrDefault(item => item.UserRoleId == userRoleId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.UserRole.AsNoTracking()
|
return db.UserRole.AsNoTracking()
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.FirstOrDefault(item => item.UserRoleId == userRoleId);
|
.FirstOrDefault(item => item.UserRoleId == userRoleId);
|
||||||
|
@ -85,16 +90,17 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public UserRole GetUserRole(int userId, int roleId, bool tracking)
|
public UserRole GetUserRole(int userId, int roleId, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.UserRole
|
return db.UserRole
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.FirstOrDefault(item => item.UserId == userId && item.RoleId == roleId);
|
.FirstOrDefault(item => item.UserId == userId && item.RoleId == roleId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.UserRole.AsNoTracking()
|
return db.UserRole.AsNoTracking()
|
||||||
.Include(item => item.Role) // eager load roles
|
.Include(item => item.Role) // eager load roles
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.FirstOrDefault(item => item.UserId == userId && item.RoleId == roleId);
|
.FirstOrDefault(item => item.UserId == userId && item.RoleId == roleId);
|
||||||
|
@ -103,18 +109,20 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public void DeleteUserRole(int userRoleId)
|
public void DeleteUserRole(int userRoleId)
|
||||||
{
|
{
|
||||||
UserRole userRole = _db.UserRole.Find(userRoleId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.UserRole.Remove(userRole);
|
var userRole = db.UserRole.Find(userRoleId);
|
||||||
_db.SaveChanges();
|
db.UserRole.Remove(userRole);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteUserRoles(int userId)
|
public void DeleteUserRoles(int userId)
|
||||||
{
|
{
|
||||||
foreach (UserRole userRole in _db.UserRole.Where(item => item.UserId == userId && item.Role.SiteId != null))
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
foreach (var userRole in db.UserRole.Where(item => item.UserId == userId && item.Role.SiteId != null))
|
||||||
{
|
{
|
||||||
_db.UserRole.Remove(userRole);
|
db.UserRole.Remove(userRole);
|
||||||
}
|
}
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,64 +8,72 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class VisitorRepository : IVisitorRepository
|
public class VisitorRepository : IVisitorRepository
|
||||||
{
|
{
|
||||||
private TenantDBContext _db;
|
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||||
|
private readonly TenantDBContext _queryContext;
|
||||||
|
|
||||||
public VisitorRepository(TenantDBContext context)
|
public VisitorRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_dbContextFactory = dbContextFactory;
|
||||||
|
_queryContext = _dbContextFactory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Visitor> GetVisitors(int siteId, DateTime fromDate)
|
public IEnumerable<Visitor> GetVisitors(int siteId, DateTime fromDate)
|
||||||
{
|
{
|
||||||
return _db.Visitor.AsNoTracking()
|
return _queryContext.Visitor.AsNoTracking()
|
||||||
.Include(item => item.User) // eager load users
|
.Include(item => item.User) // eager load users
|
||||||
.Where(item => item.SiteId == siteId && item.VisitedOn >= fromDate);
|
.Where(item => item.SiteId == siteId && item.VisitedOn >= fromDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visitor AddVisitor(Visitor visitor)
|
public Visitor AddVisitor(Visitor visitor)
|
||||||
{
|
{
|
||||||
_db.Visitor.Add(visitor);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Visitor.Add(visitor);
|
||||||
|
db.SaveChanges();
|
||||||
return visitor;
|
return visitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visitor UpdateVisitor(Visitor visitor)
|
public Visitor UpdateVisitor(Visitor visitor)
|
||||||
{
|
{
|
||||||
_db.Entry(visitor).State = EntityState.Modified;
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry(visitor).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return visitor;
|
return visitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visitor GetVisitor(int visitorId)
|
public Visitor GetVisitor(int visitorId)
|
||||||
{
|
{
|
||||||
return _db.Visitor.Find(visitorId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
return db.Visitor.Find(visitorId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Visitor GetVisitor(int siteId, string IPAddress)
|
public Visitor GetVisitor(int siteId, string IPAddress)
|
||||||
{
|
{
|
||||||
return _db.Visitor.FirstOrDefault(item => item.SiteId == siteId && item.IPAddress == IPAddress);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
|
return db.Visitor.FirstOrDefault(item => item.SiteId == siteId && item.IPAddress == IPAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteVisitor(int visitorId)
|
public void DeleteVisitor(int visitorId)
|
||||||
{
|
{
|
||||||
Visitor visitor = _db.Visitor.Find(visitorId);
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
_db.Visitor.Remove(visitor);
|
var visitor = db.Visitor.Find(visitorId);
|
||||||
_db.SaveChanges();
|
db.Visitor.Remove(visitor);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int DeleteVisitors(int siteId, int age)
|
public int DeleteVisitors(int siteId, int age)
|
||||||
{
|
{
|
||||||
|
using var db = _dbContextFactory.CreateDbContext();
|
||||||
// delete visitors in batches of 100 records
|
// delete visitors in batches of 100 records
|
||||||
int count = 0;
|
var count = 0;
|
||||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||||
var visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
var visitors = db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
||||||
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
||||||
while (visitors.Count > 0)
|
while (visitors.Count > 0)
|
||||||
{
|
{
|
||||||
count += visitors.Count;
|
count += visitors.Count;
|
||||||
_db.Visitor.RemoveRange(visitors);
|
db.Visitor.RemoveRange(visitors);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
visitors = _db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
visitors = db.Visitor.Where(item => item.SiteId == siteId && item.Visits < 2 && item.VisitedOn < purgedate)
|
||||||
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
.OrderBy(item => item.VisitedOn).Take(100).ToList();
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -8,16 +8,18 @@ namespace [Owner].Module.[Module].Repository
|
||||||
{
|
{
|
||||||
public class [Module]Repository : I[Module]Repository, ITransientService
|
public class [Module]Repository : I[Module]Repository, ITransientService
|
||||||
{
|
{
|
||||||
private readonly [Module]Context _db;
|
private readonly IDbContextFactory<[Module]Context> _factory;
|
||||||
|
private readonly [Module]Context _queryContext;
|
||||||
|
|
||||||
public [Module]Repository([Module]Context context)
|
public [Module]Repository(IDbContextFactory<[Module]Context> factory)
|
||||||
{
|
{
|
||||||
_db = context;
|
_factory = factory;
|
||||||
|
_queryContext = _factory.CreateDbContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Models.[Module]> Get[Module]s(int ModuleId)
|
public IEnumerable<Models.[Module]> Get[Module]s(int ModuleId)
|
||||||
{
|
{
|
||||||
return _db.[Module].Where(item => item.ModuleId == ModuleId);
|
return _queryContext.[Module].Where(item => item.ModuleId == ModuleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Models.[Module] Get[Module](int [Module]Id)
|
public Models.[Module] Get[Module](int [Module]Id)
|
||||||
|
@ -27,35 +29,39 @@ namespace [Owner].Module.[Module].Repository
|
||||||
|
|
||||||
public Models.[Module] Get[Module](int [Module]Id, bool tracking)
|
public Models.[Module] Get[Module](int [Module]Id, bool tracking)
|
||||||
{
|
{
|
||||||
|
using var db = _factory.CreateDbContext();
|
||||||
if (tracking)
|
if (tracking)
|
||||||
{
|
{
|
||||||
return _db.[Module].Find([Module]Id);
|
return db.[Module].Find([Module]Id);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _db.[Module].AsNoTracking().FirstOrDefault(item => item.[Module]Id == [Module]Id);
|
return db.[Module].AsNoTracking().FirstOrDefault(item => item.[Module]Id == [Module]Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Models.[Module] Add[Module](Models.[Module] [Module])
|
public Models.[Module] Add[Module](Models.[Module] [Module])
|
||||||
{
|
{
|
||||||
_db.[Module].Add([Module]);
|
using var db = _factory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.[Module].Add([Module]);
|
||||||
|
db.SaveChanges();
|
||||||
return [Module];
|
return [Module];
|
||||||
}
|
}
|
||||||
|
|
||||||
public Models.[Module] Update[Module](Models.[Module] [Module])
|
public Models.[Module] Update[Module](Models.[Module] [Module])
|
||||||
{
|
{
|
||||||
_db.Entry([Module]).State = EntityState.Modified;
|
using var db = _factory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.Entry([Module]).State = EntityState.Modified;
|
||||||
|
db.SaveChanges();
|
||||||
return [Module];
|
return [Module];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete[Module](int [Module]Id)
|
public void Delete[Module](int [Module]Id)
|
||||||
{
|
{
|
||||||
Models.[Module] [Module] = _db.[Module].Find([Module]Id);
|
using var db = _factory.CreateDbContext();
|
||||||
_db.[Module].Remove([Module]);
|
Models.[Module] [Module] = db.[Module].Find([Module]Id);
|
||||||
_db.SaveChanges();
|
db.[Module].Remove([Module]);
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user