Iloan.Rules/src/iLoan.Rules.Web/Services/RulesService.cs
2024-06-20 09:48:12 +02:00

512 lines
17 KiB
C#

namespace ILoan.Rules.Web.Services;
public partial class RulesService(IDbContextFactory<RulesContext> factory, NavigationManager navigationManager)
{
public void ApplyQuery<T>(ref IQueryable<T> items, Query query = null)
{
if (query != null)
{
if (!string.IsNullOrEmpty(query.Filter))
{
if (query.FilterParameters != null)
items = items.Where(query.Filter, query.FilterParameters);
else
items = items.Where(query.Filter);
}
if (!string.IsNullOrEmpty(query.OrderBy)) items = items.OrderBy(query.OrderBy);
if (query.Skip.HasValue) items = items.Skip(query.Skip.Value);
if (query.Top.HasValue) items = items.Take(query.Top.Value);
}
}
partial void OnCoreRuleCriteriaOperatorsRead(ref IQueryable<CoreRuleCriteriaOperator> items);
public async Task<IQueryable<CoreRuleCriteriaOperator>> GetCoreRuleCriteriaOperators(Query query = null)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRuleCriteriaOperators.AsQueryable();
items = items.Include(i => i.CoreRule);
if (query != null)
{
if (!string.IsNullOrEmpty(query.Expand))
{
var propertiesToExpand = query.Expand.Split(',');
foreach (var p in propertiesToExpand) items = items.Include(p.Trim());
}
ApplyQuery(ref items, query);
}
OnCoreRuleCriteriaOperatorsRead(ref items);
return await Task.FromResult(items);
}
partial void OnCoreRuleCriteriaOperatorGet(CoreRuleCriteriaOperator item);
partial void OnGetCoreRuleCriteriaOperatorById(ref IQueryable<CoreRuleCriteriaOperator> items);
public async Task<CoreRuleCriteriaOperator> GetCoreRuleCriteriaOperatorById(int id)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRuleCriteriaOperators
.AsNoTracking()
.Where(i => i.ID == id);
items = items.Include(i => i.CoreRule);
OnGetCoreRuleCriteriaOperatorById(ref items);
var itemToReturn = items.FirstOrDefault();
OnCoreRuleCriteriaOperatorGet(itemToReturn);
return await Task.FromResult(itemToReturn);
}
partial void OnCoreRuleCriteriaOperatorCreated(CoreRuleCriteriaOperator item);
partial void OnAfterCoreRuleCriteriaOperatorCreated(CoreRuleCriteriaOperator item);
public async Task<CoreRuleCriteriaOperator> CreateCoreRuleCriteriaOperator(
CoreRuleCriteriaOperator corerulecriteriaoperator)
{
OnCoreRuleCriteriaOperatorCreated(corerulecriteriaoperator);
var context = await factory.CreateDbContextAsync();
var existingItem = context.CoreRuleCriteriaOperators
.FirstOrDefault(i => i.ID == corerulecriteriaoperator.ID);
if (existingItem != null) throw new Exception("Item already available");
try
{
context.CoreRuleCriteriaOperators.Add(corerulecriteriaoperator);
await context.SaveChangesAsync();
}
catch
{
context.Entry(corerulecriteriaoperator).State = EntityState.Detached;
throw;
}
OnAfterCoreRuleCriteriaOperatorCreated(corerulecriteriaoperator);
return corerulecriteriaoperator;
}
public async Task<CoreRuleCriteriaOperator> CancelCoreRuleCriteriaOperatorChanges(CoreRuleCriteriaOperator item)
{
var context = await factory.CreateDbContextAsync();
var entityToCancel = context.Entry(item);
if (entityToCancel.State == EntityState.Modified)
{
entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues);
entityToCancel.State = EntityState.Unchanged;
}
return item;
}
partial void OnCoreRuleCriteriaOperatorUpdated(CoreRuleCriteriaOperator item);
partial void OnAfterCoreRuleCriteriaOperatorUpdated(CoreRuleCriteriaOperator item);
public async Task<CoreRuleCriteriaOperator> UpdateCoreRuleCriteriaOperator(int id,
CoreRuleCriteriaOperator corerulecriteriaoperator)
{
OnCoreRuleCriteriaOperatorUpdated(corerulecriteriaoperator);
var context = await factory.CreateDbContextAsync();
var itemToUpdate = context.CoreRuleCriteriaOperators
.FirstOrDefault(i => i.ID == corerulecriteriaoperator.ID);
if (itemToUpdate == null) throw new Exception("Item no longer available");
var entryToUpdate = context.Entry(itemToUpdate);
entryToUpdate.CurrentValues.SetValues(corerulecriteriaoperator);
entryToUpdate.State = EntityState.Modified;
await context.SaveChangesAsync();
OnAfterCoreRuleCriteriaOperatorUpdated(corerulecriteriaoperator);
return corerulecriteriaoperator;
}
partial void OnCoreRuleCriteriaOperatorDeleted(CoreRuleCriteriaOperator item);
partial void OnAfterCoreRuleCriteriaOperatorDeleted(CoreRuleCriteriaOperator item);
public async Task<CoreRuleCriteriaOperator> DeleteCoreRuleCriteriaOperator(int id)
{
var context = await factory.CreateDbContextAsync();
var itemToDelete = context.CoreRuleCriteriaOperators
.FirstOrDefault(i => i.ID == id);
if (itemToDelete == null) throw new Exception("Item no longer available");
OnCoreRuleCriteriaOperatorDeleted(itemToDelete);
context.CoreRuleCriteriaOperators.Remove(itemToDelete);
try
{
await context.SaveChangesAsync();
}
catch
{
context.Entry(itemToDelete).State = EntityState.Unchanged;
throw;
}
OnAfterCoreRuleCriteriaOperatorDeleted(itemToDelete);
return itemToDelete;
}
public async Task ExportCoreRulesToExcel(Query query = null, string fileName = null)
{
navigationManager.NavigateTo(
query != null
? query.ToUrl(
$"export/rules/corerules/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')")
: $"export/rules/corerules/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')",
true);
}
public async Task ExportCoreRulesToCSV(Query query = null, string fileName = null)
{
navigationManager.NavigateTo(
query != null
? query.ToUrl(
$"export/rules/corerules/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')")
: $"export/rules/corerules/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')",
true);
}
partial void OnCoreRulesRead(ref IQueryable<CoreRule> items);
public async Task<IQueryable<CoreRule>> GetCoreRules(Query query = null)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRules.OrderBy(r=>r.ID).AsQueryable();
if (query != null)
{
if (!string.IsNullOrEmpty(query.Expand))
{
var propertiesToExpand = query.Expand.Split(',');
foreach (var p in propertiesToExpand) items = items.Include(p.Trim());
}
ApplyQuery(ref items, query);
}
OnCoreRulesRead(ref items);
return await Task.FromResult(items);
}
partial void OnCoreRuleGet(CoreRule item);
partial void OnGetCoreRuleById(ref IQueryable<CoreRule> items);
public async Task<CoreRule> GetCoreRuleById(int id)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRules
.AsNoTracking()
.Where(i => i.ID == id);
OnGetCoreRuleById(ref items);
var itemToReturn = items.FirstOrDefault();
OnCoreRuleGet(itemToReturn);
return await Task.FromResult(itemToReturn);
}
partial void OnCoreRuleCreated(CoreRule item);
partial void OnAfterCoreRuleCreated(CoreRule item);
public async Task<CoreRule> CreateCoreRule(CoreRule corerule)
{
OnCoreRuleCreated(corerule);
var context = await factory.CreateDbContextAsync();
var existingItem = context.CoreRules
.FirstOrDefault(i => i.ID == corerule.ID);
if (existingItem != null) throw new Exception("Item already available");
try
{
context.CoreRules.Add(corerule);
await context.SaveChangesAsync();
}
catch
{
context.Entry(corerule).State = EntityState.Detached;
throw;
}
OnAfterCoreRuleCreated(corerule);
return corerule;
}
public async Task<CoreRule> CancelCoreRuleChanges(CoreRule item)
{
var context = await factory.CreateDbContextAsync();
var entityToCancel = context.Entry(item);
if (entityToCancel.State == EntityState.Modified)
{
entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues);
entityToCancel.State = EntityState.Unchanged;
}
return item;
}
partial void OnCoreRuleUpdated(CoreRule item);
partial void OnAfterCoreRuleUpdated(CoreRule item);
public async Task<CoreRule> UpdateCoreRule(int id, CoreRule corerule)
{
OnCoreRuleUpdated(corerule);
var context = await factory.CreateDbContextAsync();
var itemToUpdate = context.CoreRules
.FirstOrDefault(i => i.ID == corerule.ID);
if (itemToUpdate == null) throw new Exception("Item no longer available");
var entryToUpdate = context.Entry(itemToUpdate);
entryToUpdate.CurrentValues.SetValues(corerule);
entryToUpdate.State = EntityState.Modified;
await context.SaveChangesAsync();
OnAfterCoreRuleUpdated(corerule);
return corerule;
}
partial void OnCoreRuleDeleted(CoreRule item);
partial void OnAfterCoreRuleDeleted(CoreRule item);
public async Task<CoreRule> DeleteCoreRule(int id)
{
var context = await factory.CreateDbContextAsync();
var itemToDelete = context.CoreRules
.Where(i => i.ID == id)
.Include(i => i.CoreRuleCriteria)
.Include(i => i.CoreRuleCriteriaOperators)
.FirstOrDefault();
if (itemToDelete == null) throw new Exception("Item no longer available");
OnCoreRuleDeleted(itemToDelete);
context.CoreRules.Remove(itemToDelete);
try
{
await context.SaveChangesAsync();
}
catch
{
context.Entry(itemToDelete).State = EntityState.Unchanged;
throw;
}
OnAfterCoreRuleDeleted(itemToDelete);
return itemToDelete;
}
public async Task ExportCoreRuleCriteriaToExcel(Query query = null, string fileName = null)
{
navigationManager.NavigateTo(
query != null
? query.ToUrl(
$"export/rules/corerulecriteria/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')")
: $"export/rules/corerulecriteria/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')",
true);
}
public async Task ExportCoreRuleCriteriaToCSV(Query query = null, string fileName = null)
{
navigationManager.NavigateTo(
query != null
? query.ToUrl(
$"export/rules/corerulecriteria/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')")
: $"export/rules/corerulecriteria/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')",
true);
}
partial void OnCoreRuleCriteriaRead(ref IQueryable<CoreRuleCriterion> items);
public async Task<IQueryable<CoreRuleCriterion>> GetCoreRuleCriteria(Query query = null)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRuleCriteria.AsQueryable();
items = items.Include(i => i.CoreRule);
if (query != null)
{
if (!string.IsNullOrEmpty(query.Expand))
{
var propertiesToExpand = query.Expand.Split(',');
foreach (var p in propertiesToExpand) items = items.Include(p.Trim());
}
ApplyQuery(ref items, query);
}
OnCoreRuleCriteriaRead(ref items);
return await Task.FromResult(items);
}
partial void OnCoreRuleCriterionGet(CoreRuleCriterion item);
partial void OnGetCoreRuleCriterionById(ref IQueryable<CoreRuleCriterion> items);
public async Task<CoreRuleCriterion> GetCoreRuleCriterionById(int id)
{
var context = await factory.CreateDbContextAsync();
var items = context.CoreRuleCriteria
.AsNoTracking()
.Where(i => i.ID == id);
items = items.Include(i => i.CoreRule);
OnGetCoreRuleCriterionById(ref items);
var itemToReturn = items.FirstOrDefault();
OnCoreRuleCriterionGet(itemToReturn);
return await Task.FromResult(itemToReturn);
}
partial void OnCoreRuleCriterionCreated(CoreRuleCriterion item);
partial void OnAfterCoreRuleCriterionCreated(CoreRuleCriterion item);
public async Task<CoreRuleCriterion> CreateCoreRuleCriterion(CoreRuleCriterion corerulecriterion)
{
OnCoreRuleCriterionCreated(corerulecriterion);
var context = await factory.CreateDbContextAsync();
var existingItem = context.CoreRuleCriteria
.FirstOrDefault(i => i.ID == corerulecriterion.ID);
if (existingItem != null) throw new Exception("Item already available");
try
{
context.CoreRuleCriteria.Add(corerulecriterion);
await context.SaveChangesAsync();
}
catch
{
context.Entry(corerulecriterion).State = EntityState.Detached;
throw;
}
OnAfterCoreRuleCriterionCreated(corerulecriterion);
return corerulecriterion;
}
public async Task<CoreRuleCriterion> CancelCoreRuleCriterionChanges(CoreRuleCriterion item)
{
var context = await factory.CreateDbContextAsync();
var entityToCancel = context.Entry(item);
if (entityToCancel.State == EntityState.Modified)
{
entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues);
entityToCancel.State = EntityState.Unchanged;
}
return item;
}
partial void OnCoreRuleCriterionUpdated(CoreRuleCriterion item);
partial void OnAfterCoreRuleCriterionUpdated(CoreRuleCriterion item);
public async Task<CoreRuleCriterion> UpdateCoreRuleCriterion(int id, CoreRuleCriterion corerulecriterion)
{
OnCoreRuleCriterionUpdated(corerulecriterion);
var context = await factory.CreateDbContextAsync();
var itemToUpdate = context.CoreRuleCriteria
.FirstOrDefault(i => i.ID == corerulecriterion.ID);
if (itemToUpdate == null) throw new Exception("Item no longer available");
var entryToUpdate = context.Entry(itemToUpdate);
entryToUpdate.CurrentValues.SetValues(corerulecriterion);
entryToUpdate.State = EntityState.Modified;
await context.SaveChangesAsync();
OnAfterCoreRuleCriterionUpdated(corerulecriterion);
return corerulecriterion;
}
partial void OnCoreRuleCriterionDeleted(CoreRuleCriterion item);
partial void OnAfterCoreRuleCriterionDeleted(CoreRuleCriterion item);
public async Task<CoreRuleCriterion> DeleteCoreRuleCriterion(int id)
{
var context = await factory.CreateDbContextAsync();
var itemToDelete = context.CoreRuleCriteria
.FirstOrDefault(i => i.ID == id);
if (itemToDelete == null) throw new Exception("Item no longer available");
OnCoreRuleCriterionDeleted(itemToDelete);
context.CoreRuleCriteria.Remove(itemToDelete);
try
{
await context.SaveChangesAsync();
}
catch
{
context.Entry(itemToDelete).State = EntityState.Unchanged;
throw;
}
OnAfterCoreRuleCriterionDeleted(itemToDelete);
return itemToDelete;
}
// get properties from CoreRuleCriterion as a disctinct list for type ahead
public List<string> GetProperties()
{
var context = factory.CreateDbContext();
var properties = context.CoreRuleCriteria.Select(c => c.Property).Distinct().OrderBy(x => x).ToList();
return properties;
}
public List<string> GetComparisons()
{
return EnumExtensions.ToList<RuleComparison>();
}
}