197 lines
6.5 KiB
C#
197 lines
6.5 KiB
C#
using EgtBEAMWALL.DataLayer.DatabaseModels;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace EgtBEAMWALL.DataLayer.Controllers
|
|
{
|
|
public class LogSupportController : IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
public LogSupportController()
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Create support LOG record
|
|
/// </summary>
|
|
/// <param name="dtCreated"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="dtExported"></param>
|
|
/// <param name="sn"></param>
|
|
/// <param name="description"></param>
|
|
/// <param name="eventType"></param>
|
|
/// <param name="message"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string errMessage = $"EXCEPTION on LogSupport.Create: {Environment.NewLine}{exc}";
|
|
Console.WriteLine(errMessage);
|
|
Log.Error(errMessage);
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete by key
|
|
/// </summary>
|
|
/// <param name="LogDbId"></param>
|
|
/// <returns></returns>
|
|
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();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string errMessage = $"EXCEPTION on LogSupport.DeleteByKey: {Environment.NewLine}{exc}";
|
|
Console.WriteLine(errMessage);
|
|
Log.Error(errMessage);
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get record by LogDbId
|
|
/// </summary>
|
|
/// <param name="LogDbId"></param>
|
|
/// <returns></returns>
|
|
public LogSupportModel FindByDbId(int LogDbId)
|
|
{
|
|
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
|
|
{
|
|
return localDbCtx
|
|
.LogSupportList
|
|
.Where(x => x.LogDbId == LogDbId)
|
|
.SingleOrDefault();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get paginated data from DB (ASC ordered)
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <returns></returns>
|
|
public List<LogSupportModel> 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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get paginated data from DB (DESC ordered)
|
|
/// </summary>
|
|
/// <param name="dtStart"></param>
|
|
/// <param name="dtEnd"></param>
|
|
/// <returns></returns>
|
|
public List<LogSupportModel> 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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update single LogMachineModel
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
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();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string errMessage = $"EXCEPTION on LogSupport.Update: {Environment.NewLine}{exc}";
|
|
Console.WriteLine(errMessage);
|
|
Log.Error(errMessage);
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Istanza logger
|
|
/// </summary>
|
|
private NLog.Logger Log = NLog.LogManager
|
|
.Setup()
|
|
.LoadConfigurationFromFile(DbConfig.NLOG_PATH + @"\NLog.config")
|
|
.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |