Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4010: update repositories to using db context factory. #4011

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions Oqtane.Server/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Oqtane.Repository
{
public class FileRepository : IFileRepository
{
private TenantDBContext _db;
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
private readonly IPermissionRepository _permissions;
private readonly IFolderRepository _folderRepository;
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;
_permissions = permissions;
_folderRepository = folderRepository;
_tenants = tenants;
Expand All @@ -32,19 +32,21 @@ public IEnumerable<File> GetFiles(int folderId)

public IEnumerable<File> GetFiles(int folderId, bool tracking)
{
using var db = _dbContextFactory.CreateDbContext();
var folder = _folderRepository.GetFolder(folderId, false);
IEnumerable<Permission> permissions = _permissions.GetPermissions(folder.SiteId, EntityNames.Folder, folderId).ToList();
var permissions = _permissions.GetPermissions(folder.SiteId, EntityNames.Folder, folderId).ToList();

IEnumerable<File> files;
if (tracking)
{
files = _db.File.Where(item => item.FolderId == folderId).Include(item => item.Folder);
files = db.File.Where(item => item.FolderId == folderId).Include(item => item.Folder).ToList();
}
else
{
files = _db.File.AsNoTracking().Where(item => item.FolderId == folderId).Include(item => item.Folder);
files = db.File.AsNoTracking().Where(item => item.FolderId == folderId).Include(item => item.Folder).ToList();
}
foreach (File file in files)

foreach (var file in files)
{
file.Folder.PermissionList = permissions.ToList();
var alias = _tenants.GetAlias();
Expand All @@ -55,18 +57,20 @@ public IEnumerable<File> GetFiles(int folderId, bool tracking)

public File AddFile(File file)
{
using var db = _dbContextFactory.CreateDbContext();
file.IsDeleted = false;
_db.File.Add(file);
_db.SaveChanges();
db.File.Add(file);
db.SaveChanges();
file.Folder = _folderRepository.GetFolder(file.FolderId);
file.Url = GetFileUrl(file, _tenants.GetAlias());
return file;
}

public File UpdateFile(File file)
{
_db.Entry(file).State = EntityState.Modified;
_db.SaveChanges();
using var db = _dbContextFactory.CreateDbContext();
db.Entry(file).State = EntityState.Modified;
db.SaveChanges();
file.Folder = _folderRepository.GetFolder(file.FolderId);
file.Url = GetFileUrl(file, _tenants.GetAlias());
return file;
Expand All @@ -80,14 +84,15 @@ public File GetFile(int fileId)

public File GetFile(int fileId, bool tracking)
{
using var db = _dbContextFactory.CreateDbContext();
File file;
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
{
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)
{
Expand All @@ -99,10 +104,11 @@ public File GetFile(int fileId, bool tracking)

public File GetFile(int folderId, string fileName)
{
var file = _db.File.AsNoTracking()
.Include(item => item.Folder)
.FirstOrDefault(item => item.FolderId == folderId &&
item.Name.ToLower() == fileName);
using var db = _dbContextFactory.CreateDbContext();
var file = db.File.AsNoTracking()
.Include(item => item.Folder)
.FirstOrDefault(item => item.FolderId == folderId &&
item.Name.ToLower() == fileName);

if (file != null)
{
Expand All @@ -115,15 +121,16 @@ public File GetFile(int folderId, 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)
.FirstOrDefault(item => item.Folder.SiteId == siteId &&
item.Folder.Path.ToLower() == folderPath &&
item.Name.ToLower() == fileName);

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.Url = GetFileUrl(file, _tenants.GetAlias());
}
Expand All @@ -133,21 +140,24 @@ public File GetFile(int siteId, string folderPath, string fileName)

public void DeleteFile(int fileId)
{
File file = _db.File.Find(fileId);
_db.File.Remove(file);
_db.SaveChanges();
using var db = _dbContextFactory.CreateDbContext();
var file = db.File.Find(fileId);
db.File.Remove(file);
db.SaveChanges();
}

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);
}

public string GetFilePath(File file)
{
using var db = _dbContextFactory.CreateDbContext();
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);
return filepath;
}
Expand Down
41 changes: 24 additions & 17 deletions Oqtane.Server/Repository/FolderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ namespace Oqtane.Repository
{
public class FolderRepository : IFolderRepository
{
private TenantDBContext _db;
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
private readonly IPermissionRepository _permissions;
private readonly IWebHostEnvironment _environment;
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;
_permissions = permissions;
_environment = environment;
_tenants = tenants;
}

public IEnumerable<Folder> GetFolders(int siteId)
{
IEnumerable<Permission> permissions = _permissions.GetPermissions(siteId, EntityNames.Folder).ToList();
IEnumerable<Folder> folders = _db.Folder.Where(item => item.SiteId == siteId);
foreach(Folder folder in folders)
using var db = _dbContextFactory.CreateDbContext();
var permissions = _permissions.GetPermissions(siteId, EntityNames.Folder).ToList();
var folders = db.Folder.Where(item => item.SiteId == siteId).ToList();
foreach (var folder in folders)
{
folder.PermissionList = permissions.Where(item => item.EntityId == folder.FolderId).ToList();
}
Expand All @@ -37,17 +38,19 @@ public IEnumerable<Folder> GetFolders(int siteId)

public Folder AddFolder(Folder folder)
{
using var db = _dbContextFactory.CreateDbContext();
folder.IsDeleted = false;
_db.Folder.Add(folder);
_db.SaveChanges();
db.Folder.Add(folder);
db.SaveChanges();
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
return folder;
}

public Folder UpdateFolder(Folder folder)
{
_db.Entry(folder).State = EntityState.Modified;
_db.SaveChanges();
using var db = _dbContextFactory.CreateDbContext();
db.Entry(folder).State = EntityState.Modified;
db.SaveChanges();
_permissions.UpdatePermissions(folder.SiteId, EntityNames.Folder, folder.FolderId, folder.PermissionList);
return folder;
}
Expand All @@ -59,14 +62,15 @@ public Folder GetFolder(int folderId)

public Folder GetFolder(int folderId, bool tracking)
{
using var db = _dbContextFactory.CreateDbContext();
Folder folder;
if (tracking)
{
folder = _db.Folder.Find(folderId);
folder = db.Folder.Find(folderId);
}
else
{
folder = _db.Folder.AsNoTracking().Where(item => item.FolderId == folderId).FirstOrDefault();
folder = db.Folder.AsNoTracking().Where(item => item.FolderId == folderId).FirstOrDefault();
}
if (folder != null)
{
Expand All @@ -77,7 +81,8 @@ public Folder GetFolder(int folderId, bool tracking)

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)
{
folder.PermissionList = _permissions.GetPermissions(folder.SiteId, EntityNames.Folder, folder.FolderId)?.ToList();
Expand All @@ -87,15 +92,17 @@ public Folder GetFolder(int siteId, string path)

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);
_db.Folder.Remove(folder);
_db.SaveChanges();
db.Folder.Remove(folder);
db.SaveChanges();
}

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);
}

Expand Down
33 changes: 19 additions & 14 deletions Oqtane.Server/Repository/LanguageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,69 @@ namespace Oqtane.Repository
{
public class LanguageRepository : ILanguageRepository
{
private TenantDBContext _db;
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;

public LanguageRepository(TenantDBContext context)
public LanguageRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
{
_db = context;
_dbContextFactory = dbContextFactory;
}

public IEnumerable<Language> GetLanguages(int siteId)
{
return _db.Language.Where(l => l.SiteId == siteId);
using var db = _dbContextFactory.CreateDbContext();
return db.Language.Where(l => l.SiteId == siteId).ToList();
}

public Language AddLanguage(Language language)
{
using var db = _dbContextFactory.CreateDbContext();
if (language.IsDefault)
{
// Ensure all other languages are not set to default
_db.Language
db.Language
.Where(l => l.SiteId == language.SiteId)
.ToList()
.ForEach(l => l.IsDefault = false);
}

_db.Language.Add(language);
_db.SaveChanges();
db.Language.Add(language);
db.SaveChanges();

return language;
}

public void UpdateLanguage(Language language)
{
using var db = _dbContextFactory.CreateDbContext();
if (language.LanguageId != 0)
{
_db.Entry(language).State = EntityState.Modified;
db.Entry(language).State = EntityState.Modified;
}
if (language.IsDefault)
{
// Ensure all other languages are not set to default
_db.Language
db.Language
.Where(l => l.SiteId == language.SiteId &&
l.LanguageId != language.LanguageId)
.ToList()
.ForEach(l => l.IsDefault = false);
}

_db.SaveChanges();
db.SaveChanges();
}

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)
{
var language = _db.Language.Find(languageId);
_db.Language.Remove(language);
_db.SaveChanges();
using var db = _dbContextFactory.CreateDbContext();
var language = db.Language.Find(languageId);
db.Language.Remove(language);
db.SaveChanges();
}
}
}
Loading