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
///
/// Create machine LOG record
///
///
///
///
///
///
///
///
///
///
///
public bool Create(string machineId, DateTime dtCreated, string fileName, DateTime dtExported, int sn, string description, Core.EventType eventType, string message, double value)
{
bool fatto = false;
try
{
try
{
LogMachineModel newItem = new LogMachineModel() { MachineId = machineId, DtEvent = dtCreated, CncFileName = fileName, DtExported = dtExported, SN = sn, Description = description, EventType = eventType, Message = message, Value = value };
// Add to database
dbCtx.LogMachineList.Add(newItem);
// Commit changes
dbCtx.SaveChanges();
}
catch (Exception exc)
{
Console.WriteLine($"EXCEPTION on Create LogMachine: {exc}");
}
}
catch
{ }
return fatto;
}
///
/// 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 GetPaginatedAsc(DateTime dtStart, DateTime dtEnd)
{
// retrieve
return dbCtx
.LogMachineList
.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
return dbCtx
.LogMachineList
.Where(x => x.DtEvent >= dtStart && x.DtEvent <= dtEnd)
.OrderByDescending(x => x.DtEvent)
.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
}
}