use DbContextFactory in all Html/Text methods

This commit is contained in:
sbwalker 2024-03-21 15:25:17 -04:00
parent e0ef3ca39a
commit bbc77f81ca

View File

@ -12,14 +12,12 @@ namespace Oqtane.Modules.HtmlText.Repository
[PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
{
private readonly HtmlTextContext _db;
private readonly IDbContextFactory<HtmlTextContext> _factory;
private readonly IMemoryCache _cache;
private readonly SiteState _siteState;
public HtmlTextRepository(HtmlTextContext context, IDbContextFactory<HtmlTextContext> factory, IMemoryCache cache, SiteState siteState)
public HtmlTextRepository(IDbContextFactory<HtmlTextContext> factory, IMemoryCache cache, SiteState siteState)
{
_db = context;
_factory = factory;
_cache = cache;
_siteState = siteState;
@ -30,29 +28,33 @@ namespace Oqtane.Modules.HtmlText.Repository
return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return _db.HtmlText.Where(item => item.ModuleId == moduleId).ToList();
using var db = _factory.CreateDbContext();
return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList();
});
}
public Models.HtmlText GetHtmlText(int htmlTextId)
{
return _db.HtmlText.Find(htmlTextId);
using var db = _factory.CreateDbContext();
return db.HtmlText.Find(htmlTextId);
}
public Models.HtmlText AddHtmlText(Models.HtmlText htmlText)
{
_db.HtmlText.Add(htmlText);
_db.SaveChanges();
using var db = _factory.CreateDbContext();
db.HtmlText.Add(htmlText);
db.SaveChanges();
ClearCache(htmlText.ModuleId);
return htmlText;
}
public void DeleteHtmlText(int htmlTextId)
{
Models.HtmlText htmlText = _db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
if (htmlText != null) _db.HtmlText.Remove(htmlText);
using var db = _factory.CreateDbContext();
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
if (htmlText != null) db.HtmlText.Remove(htmlText);
ClearCache(htmlText.ModuleId);
_db.SaveChanges();
db.SaveChanges();
}
public async Task<IEnumerable<Models.HtmlText>> GetHtmlTextsAsync(int moduleId)
@ -60,33 +62,33 @@ namespace Oqtane.Modules.HtmlText.Repository
return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
using var ctx = _factory.CreateDbContext();
return await ctx.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync();
using var db = _factory.CreateDbContext();
return await db.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync();
});
}
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId)
{
using var ctx = _factory.CreateDbContext();
return await ctx.HtmlText.FindAsync(htmlTextId);
using var db = _factory.CreateDbContext();
return await db.HtmlText.FindAsync(htmlTextId);
}
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
{
using var ctx = _factory.CreateDbContext();
ctx.HtmlText.Add(htmlText);
await ctx.SaveChangesAsync();
using var db = _factory.CreateDbContext();
db.HtmlText.Add(htmlText);
await db.SaveChangesAsync();
ClearCache(htmlText.ModuleId);
return htmlText;
}
public async Task DeleteHtmlTextAsync(int htmlTextId)
{
using var ctx = _factory.CreateDbContext();
Models.HtmlText htmlText = ctx.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
ctx.HtmlText.Remove(htmlText);
using var db = _factory.CreateDbContext();
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
db.HtmlText.Remove(htmlText);
ClearCache(htmlText.ModuleId);
await ctx.SaveChangesAsync();
await db.SaveChangesAsync();
}
private void ClearCache(int moduleId)