using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MP.Data.DbModels; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MP.Data.Repository.System { public class SystemRepository : ISystemRepository { #region Private Fields private readonly IDbContextFactory _ctxFactory; private readonly IConfiguration _configuration; #endregion #region Public Constructors public SystemRepository(IDbContextFactory ctxFactory, IConfiguration configuration) { _ctxFactory = ctxFactory; _configuration = configuration; } #endregion #region Public Methods /// public async Task> ConfigGetAllAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetConfig .AsNoTracking() .OrderBy(x => x.Chiave) .ToListAsync() ?? new(); } /// public async Task ConfigUpdateAsync(ConfigModel updRec) { bool fatto = false; ConfigModel dbResult = new(); await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); dbResult = await dbCtx .DbSetConfig .Where(x => x.Chiave == updRec.Chiave) .FirstOrDefaultAsync(); if (dbResult != null) { dbResult.Valore = updRec.Valore; fatto = await dbCtx.SaveChangesAsync() > 0; } return fatto; } /// public async Task EvListInsertAsync(EventListModel newRec) { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); _ = await dbCtx .DbSetEvList .AddAsync(newRec); return await dbCtx.SaveChangesAsync() > 0; } /// public async Task ForceDbMaintAsync(bool doExec, bool doUpdStat, bool doSave, int minPgCnt, int minAvgFrag, int maxAvgFragReb) { await using var dbCtx = new MoonProAdminContext(_configuration); _ = await dbCtx .Database .ExecuteSqlRawAsync("EXEC man.stp_Utility_Maintanance"); return true; } /// public async Task> ListLinkAllAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetLinkMenu .AsNoTracking() .OrderBy(x => x.Ordine) .ToListAsync(); } /// public async Task> ListLinkFiltAsync(string tipoLink) { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetLinkMenu .Where(x => x.TipoLink == tipoLink) .AsNoTracking() .OrderBy(x => x.Ordine) .ToListAsync(); } /// public Task> ElencoLinkAsync() { return ListLinkFiltAsync("SpecLink"); } #endregion } }