using System; using System.Collections.Generic; using System.Linq; using System.Text; using EgtBEAMWALL.DataLayer.DatabaseModels; namespace EgtBEAMWALL.DataLayer.Controllers { public class LogSupportController : IDisposable { #region Private Fields #if false private DatabaseContext localDbCtx; #endif #endregion Private Fields #region Public Constructors public LogSupportController() { #if false // Initialize database context localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING); #endif } #endregion Public Constructors #region Public Methods /// /// Create support LOG record /// /// /// /// /// /// /// /// /// /// public bool Create(DateTime dtCreated, Core.LogSupportLevel level, Core.LogSupportTarget target, string message) { bool fatto = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { try { try { LogSupportModel newItem = new LogSupportModel() { DtEvent = dtCreated, Level = level, Target = target, Message = message }; // Add to database localDbCtx.LogSupportList.Add(newItem); // Commit changes localDbCtx.SaveChanges(); #if false ResetController(); #endif } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Create LogSupport: {exc}"); } } catch { } } return fatto; } /// /// Delete by key /// /// /// public bool DeleteByKey(int LogDbId) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var items2del = localDbCtx .LogSupportList .Where(x => x.LogDbId == LogDbId); try { // Add to database localDbCtx.LogSupportList.RemoveRange(items2del); // Commit changes localDbCtx.SaveChanges(); #if false ResetController(); #endif done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on DeleteByKey: {exc}"); } } return done; } public void Dispose() { #if false // Clear database context localDbCtx.Dispose(); #endif } /// /// Get record by LogDbId /// /// /// public LogSupportModel FindByDbId(int LogDbId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .LogSupportList .Where(x => x.LogDbId == LogDbId) .SingleOrDefault(); } } /// /// Get paginated data from DB (ASC ordered) /// /// /// /// public List GetPaginatedAsc(DateTime dtStart, DateTime dtEnd) { // retrieve using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .LogSupportList .Where(x => x.DtEvent >= dtStart && x.DtEvent <= dtEnd) .OrderBy(x => x.DtEvent) .ToList(); } } /// /// Get paginated data from DB (DESC ordered) /// /// /// /// public List GetPaginatedDesc(DateTime dtStart, DateTime dtEnd) { // retrieve using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .LogSupportList .Where(x => x.DtEvent >= dtStart && x.DtEvent <= dtEnd) .OrderByDescending(x => x.DtEvent) .ToList(); } } #if false /// /// Reinizializzaizone del controller /// public void ResetController() { // Re-Initialize database context localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING); } #endif /// /// Update single LogMachineModel /// /// /// public bool Update(LogSupportModel updItem) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var item2update = localDbCtx .LogSupportList .Where(x => x.LogDbId == updItem.LogDbId) .SingleOrDefault(); try { // update, vers 1... localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); // Commit changes localDbCtx.SaveChanges(); #if false ResetController(); #endif done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Update: {exc}"); } } return done; } #endregion Public Methods } }