Merge pull request #4045 from sbwalker/dev
use DbContextFactory in all Html/Text methods
This commit is contained in:
commit
0309a866c8
@ -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")]
|
[PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
|
||||||
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
||||||
{
|
{
|
||||||
private readonly HtmlTextContext _db;
|
|
||||||
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
||||||
private readonly IMemoryCache _cache;
|
private readonly IMemoryCache _cache;
|
||||||
private readonly SiteState _siteState;
|
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;
|
_factory = factory;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_siteState = siteState;
|
_siteState = siteState;
|
||||||
@ -30,29 +28,33 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry =>
|
return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry =>
|
||||||
{
|
{
|
||||||
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
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)
|
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)
|
public Models.HtmlText AddHtmlText(Models.HtmlText htmlText)
|
||||||
{
|
{
|
||||||
_db.HtmlText.Add(htmlText);
|
using var db = _factory.CreateDbContext();
|
||||||
_db.SaveChanges();
|
db.HtmlText.Add(htmlText);
|
||||||
|
db.SaveChanges();
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
return htmlText;
|
return htmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteHtmlText(int htmlTextId)
|
public void DeleteHtmlText(int htmlTextId)
|
||||||
{
|
{
|
||||||
Models.HtmlText htmlText = _db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
using var db = _factory.CreateDbContext();
|
||||||
if (htmlText != null) _db.HtmlText.Remove(htmlText);
|
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
||||||
|
if (htmlText != null) db.HtmlText.Remove(htmlText);
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
_db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Models.HtmlText>> GetHtmlTextsAsync(int moduleId)
|
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 =>
|
return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry =>
|
||||||
{
|
{
|
||||||
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
||||||
using var ctx = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
return await ctx.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync();
|
return await db.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId)
|
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId)
|
||||||
{
|
{
|
||||||
using var ctx = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
return await ctx.HtmlText.FindAsync(htmlTextId);
|
return await db.HtmlText.FindAsync(htmlTextId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
|
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
|
||||||
{
|
{
|
||||||
using var ctx = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
ctx.HtmlText.Add(htmlText);
|
db.HtmlText.Add(htmlText);
|
||||||
await ctx.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
return htmlText;
|
return htmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteHtmlTextAsync(int htmlTextId)
|
public async Task DeleteHtmlTextAsync(int htmlTextId)
|
||||||
{
|
{
|
||||||
using var ctx = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
Models.HtmlText htmlText = ctx.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
||||||
ctx.HtmlText.Remove(htmlText);
|
db.HtmlText.Remove(htmlText);
|
||||||
ClearCache(htmlText.ModuleId);
|
ClearCache(htmlText.ModuleId);
|
||||||
await ctx.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ClearCache(int moduleId)
|
private void ClearCache(int moduleId)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user