performance optimization to limit the number of HtmlText content versions
This commit is contained in:
@@ -31,8 +31,8 @@
|
||||
<th style="width: 1px;"> </th>
|
||||
<th style="width: 1px;"> </th>
|
||||
<th style="width: 1px;"> </th>
|
||||
<th>@SharedLocalizer["CreatedOn"]</th>
|
||||
<th>@SharedLocalizer["CreatedBy"]</th>
|
||||
<th>@Localizer["CreatedOn"]</th>
|
||||
<th>@Localizer["CreatedBy"]</th>
|
||||
</Header>
|
||||
<Row>
|
||||
<td><ActionLink Action="View" Security="SecurityAccessLevel.Edit" OnClick="@(async () => await View(context))" ResourceKey="View" /></td>
|
||||
|
||||
@@ -16,6 +16,12 @@
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-1 align-items-center">
|
||||
<Label Class="col-sm-3" For="versions" ResourceKey="Versions" ResourceType="@resourceType" HelpText="The number of content versions to preserve (note that zero means unlimited)">Versions: </Label>
|
||||
<div class="col-sm-9">
|
||||
<input id="versions" type="number" min="0" max="9" step="1" class="form-control" @bind="@_versions" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -26,12 +32,14 @@
|
||||
private bool validated = false;
|
||||
|
||||
private string _dynamictokens;
|
||||
private string _versions = "5";
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
try
|
||||
{
|
||||
_dynamictokens = SettingService.GetSetting(ModuleState.Settings, "DynamicTokens", "false");
|
||||
_versions = SettingService.GetSetting(ModuleState.Settings, "Versions", "3");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -45,6 +53,10 @@
|
||||
{
|
||||
var settings = await SettingService.GetModuleSettingsAsync(ModuleState.ModuleId);
|
||||
settings = SettingService.SetSetting(settings, "DynamicTokens", _dynamictokens);
|
||||
if (int.TryParse(_versions, out int versions) && versions >= 0 && versions <= 9)
|
||||
{
|
||||
settings = SettingService.SetSetting(settings, "Versions", versions.ToString());
|
||||
}
|
||||
await SettingService.UpdateModuleSettingsAsync(settings, ModuleState.ModuleId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -123,4 +123,10 @@
|
||||
<data name="DynamicTokens.Text" xml:space="preserve">
|
||||
<value>Dynamic Tokens?</value>
|
||||
</data>
|
||||
<data name="Versions.HelpText" xml:space="preserve">
|
||||
<value>The number of content versions to preserve (note that zero means unlimited)</value>
|
||||
</data>
|
||||
<data name="Versions.Text" xml:space="preserve">
|
||||
<value>Versions:</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Linq;
|
||||
using Oqtane.Documentation;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Documentation;
|
||||
using Oqtane.Repository;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Modules.HtmlText.Repository
|
||||
{
|
||||
@@ -18,10 +20,12 @@ namespace Oqtane.Modules.HtmlText.Repository
|
||||
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
||||
{
|
||||
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
||||
private readonly ISettingRepository _settingRepository;
|
||||
|
||||
public HtmlTextRepository(IDbContextFactory<HtmlTextContext> factory)
|
||||
public HtmlTextRepository(IDbContextFactory<HtmlTextContext> factory, ISettingRepository settingRepository)
|
||||
{
|
||||
_factory = factory;
|
||||
_settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
public IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId)
|
||||
@@ -39,6 +43,17 @@ namespace Oqtane.Modules.HtmlText.Repository
|
||||
public Models.HtmlText AddHtmlText(Models.HtmlText htmlText)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
|
||||
var versions = int.Parse(_settingRepository.GetSettingValue(EntityNames.Module, htmlText.ModuleId, "Versions", "3"));
|
||||
if (versions > 0)
|
||||
{
|
||||
var htmlTexts = db.HtmlText.Where(item => item.ModuleId == htmlText.ModuleId).OrderByDescending(item => item.CreatedOn).ToList();
|
||||
for (int i = versions - 1; i < htmlTexts.Count; i++)
|
||||
{
|
||||
db.HtmlText.Remove(htmlTexts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
db.HtmlText.Add(htmlText);
|
||||
db.SaveChanges();
|
||||
return htmlText;
|
||||
|
||||
Reference in New Issue
Block a user