using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EgtBEAMWALL.DataLayer.DatabaseModels;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class ProjController : IDisposable
{
private DatabaseContext dbCtx;
public ProjController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
///
/// Get record by ProjDbId
///
///
///
public ProjModel FindByProjDbId(int ProjDbId)
{
return dbCtx
.ProjList
.Where(x => x.ProjDbId == ProjDbId)
.SingleOrDefault();
}
///
/// Get record by ProjId
///
///
///
public ProjModel FindByProjId(int ProjId)
{
return dbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
}
#if false
///
/// Get paginated data from DB (ASC ordered)
///
///
///
///
public List GetPaginatedAsc(int ProjDbIdStart, int numRecord)
{
int numEnd = ProjDbIdStart - numRecord;
// check numEnd
if (numEnd < 0)
numEnd = 0;
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProjDbId <= ProjDbIdStart)
.OrderBy(x => x.ProjDbId)
.Take(numRecord)
.ToList();
}
///
/// Get paginated data from DB (DESC ordered)
///
///
///
///
public List GetPaginatedDesc(int PartDbIdStart, int numRecord)
{
int numEnd = PartDbIdStart - numRecord;
// check numEnd
if (numEnd < 0)
numEnd = 0;
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProjDbId <= PartDbIdStart)
.OrderByDescending(x => x.ProjDbId)
.Take(numRecord)
.ToList();
}
#endif
///
/// Get LAST paginated data from DB (DESC ordered)
///
///
///
///
protected List GetLastDbModelDesc(int numRecord)
{
// se numRecord = 0 --> passo tutti
if (numRecord == 0)
{
numRecord = dbCtx.ProjList.Count();
}
// retrieve
return dbCtx
.ProjList
.OrderByDescending(x => x.ProjDbId)
.Take(numRecord)
.ToList();
}
public List GetLastDesc(int numRecord)
{
List result = new List();
var dbResult = GetLastDbModelDesc(numRecord);
// conversione
result = dbResult.Select(x => new Core.ProjectFile(Core.ConstBeam.ProjectType.PROJ, x.ProjId, (int)x.ProdDbId, x.BTLFileName)).ToList();
return result;
}
///
/// Get filtered data by ProjectId (ASC ordered)
///
///
///
public List GetByProdAsc(int ProdDbId)
{
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProdDbId == ProdDbId)
.OrderBy(x => x.ProdDbId)
.ToList();
}
///
/// Get filtered data by ProjectId (DESC ordered)
///
///
///
public List GetByProdDesc(int ProdDbId)
{
// retrieve
return dbCtx
.ProjList
.Where(x => x.ProdDbId == ProdDbId)
.OrderByDescending(x => x.ProdDbId)
.ToList();
}
///
/// Create record on DB
///
///
///
///
public ProjModel Create(int newProjId, string newBTLFileName)
{
ProjModel newProj = new ProjModel() { ProjId = newProjId, BTLFileName = newBTLFileName };
try
{
// Add to database
dbCtx.ProjList.Add(newProj);
// Commit changes
dbCtx.SaveChanges();
}
catch
{ }
return newProj;
}
///
/// Lock records by ProjDbId (proj & prod)
///
///
///
public ProjModel LockByProjDbId(int ProjDbId)
{
var currProj = dbCtx
.ProjList
.Where(x => x.ProjDbId == ProjDbId)
.SingleOrDefault();
// aggiorno stato
currProj.Locked = true;
// seleziono il prod...
var currProd = dbCtx
.ProdList
.Where(x => x.ProdDbId == currProj.ProdDbId)
.SingleOrDefault();
currProd.Locked = true;
dbCtx.SaveChanges();
return currProj;
}
///
/// Lock records by ProjId (proj & prod)
///
///
///
public ProjModel LockByProjId(int ProjId)
{
var currProj = dbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
// aggiorno stato
currProj.Locked = true;
// seleziono il prod...
var currProd = dbCtx
.ProdList
.Where(x => x.ProdDbId == currProj.ProdDbId)
.SingleOrDefault();
currProd.Locked = true;
dbCtx.SaveChanges();
return currProj;
}
}
}