using System; using System.Data; using System.Linq; using System.Linq.Dynamic.Core; using System.Collections.Generic; using System.Threading.Tasks; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Components; using Microsoft.EntityFrameworkCore; using Radzen; using ILoan.Rules.Web.Data; using DocumentFormat.OpenXml.InkML; using ILoan.Rules.Web.Models.Enums; using ILoan.Rules.Web.Extensions; namespace ILoan.Rules.Web { public partial class RulesService { RulesContext Context { get { return this.context; } } private readonly RulesContext context; private readonly NavigationManager navigationManager; public RulesService(RulesContext context, NavigationManager navigationManager) { this.context = context; this.navigationManager = navigationManager; } public void Reset() => Context.ChangeTracker.Entries().Where(e => e.Entity != null).ToList().ForEach(e => e.State = EntityState.Detached); public void ApplyQuery(ref IQueryable 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); } } } public async Task ExportCoreRuleCriteriaOperatorsToExcel(Query query = null, string fileName = null) { navigationManager.NavigateTo(query != null ? query.ToUrl($"export/rules/corerulecriteriaoperators/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')") : $"export/rules/corerulecriteriaoperators/excel(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')", true); } public async Task ExportCoreRuleCriteriaOperatorsToCSV(Query query = null, string fileName = null) { navigationManager.NavigateTo(query != null ? query.ToUrl($"export/rules/corerulecriteriaoperators/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')") : $"export/rules/corerulecriteriaoperators/csv(fileName='{(!string.IsNullOrEmpty(fileName) ? UrlEncoder.Default.Encode(fileName) : "Export")}')", true); } partial void OnCoreRuleCriteriaOperatorsRead(ref IQueryable items); public async Task> GetCoreRuleCriteriaOperators(Query query = null) { 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(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); partial void OnGetCoreRuleCriteriaOperatorById(ref IQueryable items); public async Task GetCoreRuleCriteriaOperatorById(int id) { 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(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); partial void OnAfterCoreRuleCriteriaOperatorCreated(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); public async Task CreateCoreRuleCriteriaOperator(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator corerulecriteriaoperator) { OnCoreRuleCriteriaOperatorCreated(corerulecriteriaoperator); var existingItem = Context.CoreRuleCriteriaOperators .Where(i => i.ID == corerulecriteriaoperator.ID) .FirstOrDefault(); if (existingItem != null) { throw new Exception("Item already available"); } try { Context.CoreRuleCriteriaOperators.Add(corerulecriteriaoperator); Context.SaveChanges(); } catch { Context.Entry(corerulecriteriaoperator).State = EntityState.Detached; throw; } OnAfterCoreRuleCriteriaOperatorCreated(corerulecriteriaoperator); return corerulecriteriaoperator; } public async Task CancelCoreRuleCriteriaOperatorChanges(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item) { var entityToCancel = Context.Entry(item); if (entityToCancel.State == EntityState.Modified) { entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues); entityToCancel.State = EntityState.Unchanged; } return item; } partial void OnCoreRuleCriteriaOperatorUpdated(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); partial void OnAfterCoreRuleCriteriaOperatorUpdated(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); public async Task UpdateCoreRuleCriteriaOperator(int id, ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator corerulecriteriaoperator) { OnCoreRuleCriteriaOperatorUpdated(corerulecriteriaoperator); var itemToUpdate = Context.CoreRuleCriteriaOperators .Where(i => i.ID == corerulecriteriaoperator.ID) .FirstOrDefault(); if (itemToUpdate == null) { throw new Exception("Item no longer available"); } var entryToUpdate = Context.Entry(itemToUpdate); entryToUpdate.CurrentValues.SetValues(corerulecriteriaoperator); entryToUpdate.State = EntityState.Modified; Context.SaveChanges(); OnAfterCoreRuleCriteriaOperatorUpdated(corerulecriteriaoperator); return corerulecriteriaoperator; } partial void OnCoreRuleCriteriaOperatorDeleted(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); partial void OnAfterCoreRuleCriteriaOperatorDeleted(ILoan.Rules.Web.Models.Rules.CoreRuleCriteriaOperator item); public async Task DeleteCoreRuleCriteriaOperator(int id) { var itemToDelete = Context.CoreRuleCriteriaOperators .Where(i => i.ID == id) .FirstOrDefault(); if (itemToDelete == null) { throw new Exception("Item no longer available"); } OnCoreRuleCriteriaOperatorDeleted(itemToDelete); Context.CoreRuleCriteriaOperators.Remove(itemToDelete); try { Context.SaveChanges(); } 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 items); public async Task> GetCoreRules(Query query = null) { var items = Context.CoreRules.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(ILoan.Rules.Web.Models.Rules.CoreRule item); partial void OnGetCoreRuleById(ref IQueryable items); public async Task GetCoreRuleById(int id) { 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(ILoan.Rules.Web.Models.Rules.CoreRule item); partial void OnAfterCoreRuleCreated(ILoan.Rules.Web.Models.Rules.CoreRule item); public async Task CreateCoreRule(ILoan.Rules.Web.Models.Rules.CoreRule corerule) { OnCoreRuleCreated(corerule); var existingItem = Context.CoreRules .Where(i => i.ID == corerule.ID) .FirstOrDefault(); if (existingItem != null) { throw new Exception("Item already available"); } try { Context.CoreRules.Add(corerule); Context.SaveChanges(); } catch { Context.Entry(corerule).State = EntityState.Detached; throw; } OnAfterCoreRuleCreated(corerule); return corerule; } public async Task CancelCoreRuleChanges(ILoan.Rules.Web.Models.Rules.CoreRule item) { var entityToCancel = Context.Entry(item); if (entityToCancel.State == EntityState.Modified) { entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues); entityToCancel.State = EntityState.Unchanged; } return item; } partial void OnCoreRuleUpdated(ILoan.Rules.Web.Models.Rules.CoreRule item); partial void OnAfterCoreRuleUpdated(ILoan.Rules.Web.Models.Rules.CoreRule item); public async Task UpdateCoreRule(int id, ILoan.Rules.Web.Models.Rules.CoreRule corerule) { OnCoreRuleUpdated(corerule); var itemToUpdate = Context.CoreRules .Where(i => i.ID == corerule.ID) .FirstOrDefault(); if (itemToUpdate == null) { throw new Exception("Item no longer available"); } var entryToUpdate = Context.Entry(itemToUpdate); entryToUpdate.CurrentValues.SetValues(corerule); entryToUpdate.State = EntityState.Modified; Context.SaveChanges(); OnAfterCoreRuleUpdated(corerule); return corerule; } partial void OnCoreRuleDeleted(ILoan.Rules.Web.Models.Rules.CoreRule item); partial void OnAfterCoreRuleDeleted(ILoan.Rules.Web.Models.Rules.CoreRule item); public async Task DeleteCoreRule(int id) { 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 { Context.SaveChanges(); } 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 items); public async Task> GetCoreRuleCriteria(Query query = null) { 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(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); partial void OnGetCoreRuleCriterionById(ref IQueryable items); public async Task GetCoreRuleCriterionById(int id) { 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(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); partial void OnAfterCoreRuleCriterionCreated(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); public async Task CreateCoreRuleCriterion(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion corerulecriterion) { OnCoreRuleCriterionCreated(corerulecriterion); var existingItem = Context.CoreRuleCriteria .Where(i => i.ID == corerulecriterion.ID) .FirstOrDefault(); if (existingItem != null) { throw new Exception("Item already available"); } try { Context.CoreRuleCriteria.Add(corerulecriterion); Context.SaveChanges(); } catch { Context.Entry(corerulecriterion).State = EntityState.Detached; throw; } OnAfterCoreRuleCriterionCreated(corerulecriterion); return corerulecriterion; } public async Task CancelCoreRuleCriterionChanges(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item) { var entityToCancel = Context.Entry(item); if (entityToCancel.State == EntityState.Modified) { entityToCancel.CurrentValues.SetValues(entityToCancel.OriginalValues); entityToCancel.State = EntityState.Unchanged; } return item; } partial void OnCoreRuleCriterionUpdated(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); partial void OnAfterCoreRuleCriterionUpdated(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); public async Task UpdateCoreRuleCriterion(int id, ILoan.Rules.Web.Models.Rules.CoreRuleCriterion corerulecriterion) { OnCoreRuleCriterionUpdated(corerulecriterion); var itemToUpdate = Context.CoreRuleCriteria .Where(i => i.ID == corerulecriterion.ID) .FirstOrDefault(); if (itemToUpdate == null) { throw new Exception("Item no longer available"); } var entryToUpdate = Context.Entry(itemToUpdate); entryToUpdate.CurrentValues.SetValues(corerulecriterion); entryToUpdate.State = EntityState.Modified; Context.SaveChanges(); OnAfterCoreRuleCriterionUpdated(corerulecriterion); return corerulecriterion; } partial void OnCoreRuleCriterionDeleted(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); partial void OnAfterCoreRuleCriterionDeleted(ILoan.Rules.Web.Models.Rules.CoreRuleCriterion item); public async Task DeleteCoreRuleCriterion(int id) { var itemToDelete = Context.CoreRuleCriteria .Where(i => i.ID == id) .FirstOrDefault(); if (itemToDelete == null) { throw new Exception("Item no longer available"); } OnCoreRuleCriterionDeleted(itemToDelete); Context.CoreRuleCriteria.Remove(itemToDelete); try { Context.SaveChanges(); } 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 GetProperties() { var properties = context.CoreRuleCriteria.Select(c => c.Property).Distinct().OrderBy(x => x).ToList(); return properties; } public List GetComparisons() { return EnumExtensions.ToList(); } } }