using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EgtBEAMWALL.DataLayer.DatabaseModels;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class LogMachineController : IDisposable
{
#region Private Fields
private DatabaseContext dbCtx;
#endregion Private Fields
#region Public Constructors
public LogMachineController()
{
// Initialize database context
dbCtx = new DatabaseContext(Constants.CONNECTION_STRING);
}
#endregion Public Constructors
#region Public Methods
///
/// Conversion of base class to DB model class
///
///
///
public static LogMachineModel ConvertFromCore(Core.MachLog coreMacLog)
{
LogMachineModel answ = new LogMachineModel();
if (coreMacLog != null)
{
answ = new LogMachineModel()
{
AlarmCode = coreMacLog.AlarmCode,
AlarmDatetime = coreMacLog.AlarmDateTime,
AlarmMessage = coreMacLog.AlarmMessage,
AlarmOperation = coreMacLog.AlarmOperation,
AlarmType = coreMacLog.AlarmType,
CommandExecutedCorrectly = coreMacLog.CommandExecutedCorrectly,
CommandState = coreMacLog.CommandState,
CommandType = coreMacLog.CommandType,
Description = coreMacLog.Description,
NewOpState = coreMacLog.newOpState,
ResultType = coreMacLog.ResultType,
VarAddress = coreMacLog.VarAddress,
VarValue = coreMacLog.VarValue
};
}
return answ;
}
///
/// Conversion from DB to Core class
///
///
///
public static Core.MachLog ConvertToCore(LogMachineModel dbLog)
{
var newRecord = Core.MachLog.CreateMacLog(dbLog.AlarmCode, dbLog.AlarmDatetime, dbLog.AlarmMessage, dbLog.AlarmOperation, dbLog.AlarmType, dbLog.CommandExecutedCorrectly, dbLog.CommandState, dbLog.CommandType, dbLog.Description, dbLog.NewOpState, dbLog.ResultType, dbLog.VarAddress, dbLog.VarValue);
return newRecord;
}
///
/// Create machine LOG record
///
///
///
public bool Create(LogMachineModel newLogMac)
{
bool fatto = false;
try
{
try
{
// Add to database
dbCtx.LogMachineList.Add(newLogMac);
// Commit changes
dbCtx.SaveChanges();
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on Create LogMachine: {exc}");
}
}
catch
{ }
return fatto;
}
///
/// Create machine LOG record (da modello dati CORE)
///
///
///
public bool Create(Core.MachLog newMachLog)
{
var dbLogModel = ConvertFromCore(newMachLog);
return Create(dbLogModel);
}
///
/// Delete by key
///
///
///
public bool DeleteByKey(int LogDbId)
{
bool done = false;
var items2del = dbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId);
try
{
// Add to database
dbCtx.LogMachineList.RemoveRange(items2del);
// Commit changes
dbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on DeleteByKey: {exc}");
}
return done;
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
///
/// Get record by LogDbId
///
///
///
public LogMachineModel FindByDbId(int LogDbId)
{
return dbCtx
.LogMachineList
.Where(x => x.LogDbId == LogDbId)
.SingleOrDefault();
}
///
/// Get paginated data from DB (ASC ordered)
///
///
///
///
public List GetAsc(DateTime dtStart, DateTime dtEnd)
{
// retrieve
return dbCtx
.LogMachineList
.Where(x => x.AlarmDatetime >= dtStart && x.AlarmDatetime <= dtEnd)
.OrderBy(x => x.AlarmDatetime)
.ToList();
}
///
/// Get paginated data from DB (DESC ordered)
///
///
///
///
public List GetDesc(DateTime dtStart, DateTime dtEnd)
{
// retrieve
return dbCtx
.LogMachineList
.Where(x => x.AlarmDatetime >= dtStart && x.AlarmDatetime <= dtEnd)
.OrderByDescending(x => x.AlarmDatetime)
.ToList();
}
///
/// Update single LogMachineModel
///
///
///
public bool Update(LogMachineModel updItem)
{
bool done = false;
var item2update = dbCtx
.LogMachineList
.Where(x => x.LogDbId == updItem.LogDbId)
.SingleOrDefault();
try
{
// update, vers 1...
dbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
// Commit changes
dbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on Update: {exc}");
}
return done;
}
#endregion Public Methods
}
}