using EgtBEAMWALL.DataLayer.DatabaseModels; using System; using System.Collections.Generic; using System.Linq; namespace EgtBEAMWALL.DataLayer.Controllers { public class BTLPartController : IDisposable { #region Public Constructors public BTLPartController() { } #endregion Public Constructors #region Public Methods /// /// Conversion of base class to DB model class /// /// /// /// public static BTLPartModel ConvertFromCore(Core.BTLPartM corePart, int currProjDbId) { BTLPartModel answ = new BTLPartModel(); if (corePart != null) { answ = new BTLPartModel() { PartId = corePart.nPartId, PDN = corePart.nPDN, DO = corePart.bDO, NAM = corePart.sNAM, W = corePart.dBtlW, L = corePart.dBtlL, H = corePart.dBtlH, Material = corePart.sMATERIAL, CNT = corePart.nCNT, //TBP = corePart.nTBP, //DON = corePart.nDON, //ROT = corePart.nROT, //GRP = corePart.sGRP, //UNT = corePart.nUNT, CALC_State = (int)corePart.nState, ProjDbId = currProjDbId }; } return answ; } /// /// Conversion of base class to DB model class /// /// /// public BTLPartModel Convert(Core.BTLPartM corePart) { BTLPartModel answ = new BTLPartModel(); if (corePart != null) { answ = new BTLPartModel() { PartId = corePart.nPartId, PDN = corePart.nPDN, DO = corePart.bDO, NAM = corePart.sNAM, W = corePart.dBtlW, L = corePart.dBtlL, H = corePart.dBtlH, Material = corePart.sMATERIAL, CNT = corePart.nCNT, //TBP = corePart.nTBP, //DON = corePart.nDON, //ROT = corePart.nROT, //GRP = corePart.sGRP, //UNT = corePart.nUNT, CALC_State = (int)corePart.nState }; } return answ; } /// /// Create multiple BTLPart record associated to Project /// /// /// /// public List Create(int ProjID, List partData) { // se è adv mode --> uso TUTTI i dati if (DbManager.AdvDataModel) { try { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // Add to database localDbCtx.BTLPartList.AddRange(partData); // Commit changes localDbCtx.SaveChanges(); } } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Create: {exc}"); } return partData; } // se non adv --> faccio solo copia degli id... else { List partIdList = new List(); foreach (var item in partData) { partIdList.Add(item.PartId); } return CreateBaseObj(ProjID, partIdList); } } /// /// Create multiple (empty) BTLPart record associated to Project /// /// /// /// public List CreateBaseObj(int ProjDbId, List rawListData) { List partData = new List(); foreach (var item in rawListData) { BTLPartModel newItem = new BTLPartModel() { ProjDbId = ProjDbId, PartId = item }; partData.Add(newItem); } try { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // Add to database localDbCtx.BTLPartList.AddRange(partData); // Commit changes localDbCtx.SaveChanges(); } } catch (Exception exc) { Console.WriteLine($"EXCEPTION on CreateBaseObj: {exc}"); } return partData; } /// /// Delete single BTLPart /// /// /// public bool Delete(int PartDbId) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var item2del = localDbCtx .BTLPartList .Where(x => x.BTLPartDbId == PartDbId) .SingleOrDefault(); try { // Add to database localDbCtx.BTLPartList.Remove(item2del); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Delete: {exc}"); } } return done; } /// /// Delete BTLPart by project /// /// /// public bool DeleteByProject(int ProjDbId) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var items2del = localDbCtx .BTLPartList .Where(x => x.ProjDbId == ProjDbId); try { // Add to database localDbCtx.BTLPartList.RemoveRange(items2del); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on DeleteByProject: {exc}"); } } return done; } public void Dispose() { } /// /// Get record by DBId /// /// /// public BTLPartModel FindByDbId(int PartDbId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .BTLPartList .Where(x => x.BTLPartDbId == PartDbId) .SingleOrDefault(); } } /// /// Get record by ExtId /// /// /// public BTLPartModel FindByPartId(int PartId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .BTLPartList .Where(x => x.PartId == PartId) .SingleOrDefault(); } } /// /// Get record by ProjDbId + PartId /// /// /// /// public BTLPartModel FindByPartIdProjDbId(int PartId, int ProjDbId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { return localDbCtx .BTLPartList .Where(x => x.PartId == PartId && x.ProjDbId == ProjDbId) .FirstOrDefault(); } } /// /// Get filtered data by ProjectId (ASC ordered) /// /// /// public List GetByProjectAsc(int ProjDbId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // retrieve return localDbCtx .BTLPartList .Where(x => x.ProjDbId == ProjDbId) .OrderBy(x => x.BTLPartDbId) .ToList(); } } /// /// Get filtered data by ProjectId (DESC ordered) /// /// /// public List GetByProjectDesc(int ProjDbId) { using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // retrieve return localDbCtx .BTLPartList .Where(x => x.ProjDbId == ProjDbId) .OrderByDescending(x => x.BTLPartDbId) .ToList(); } } /// /// Get paginated data from DB (ASC ordered) /// /// /// /// public List GetPaginatedAsc(int PartDbIdStart, int numRecord) { List answ = new List(); int numEnd = PartDbIdStart - numRecord; // check numEnd if (numEnd < 0) numEnd = 0; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // retrieve answ = localDbCtx .BTLPartList .Where(x => x.BTLPartDbId <= PartDbIdStart) .OrderBy(x => x.BTLPartDbId) .Take(numRecord) .ToList(); } return answ; } /// /// Get paginated data from DB (DESC ordered) /// /// /// /// public List GetPaginatedDesc(int PartDbIdStart, int numRecord) { List answ = new List(); int numEnd = PartDbIdStart - numRecord; // check numEnd if (numEnd < 0) numEnd = 0; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { // retrieve answ = localDbCtx .BTLPartList .Where(x => x.BTLPartDbId <= PartDbIdStart) .OrderByDescending(x => x.BTLPartDbId) .Take(numRecord) .ToList(); } return answ; } /// /// Update single BTLPart /// /// /// public bool Update(BTLPartModel updItem) { bool done = false; using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING)) { var item2update = localDbCtx .BTLPartList .Where(x => x.BTLPartDbId == updItem.BTLPartDbId) .SingleOrDefault(); try { // update, vers 1... localDbCtx.Entry(item2update).CurrentValues.SetValues(updItem); //// update, vers 2 //localDbCtx.BTLPartList.Remove(item2del); //localDbCtx.BTLPartList.Add(updItem); // Commit changes localDbCtx.SaveChanges(); done = true; } catch (Exception exc) { Console.WriteLine($"EXCEPTION on Update: {exc}"); } } return done; } #endregion Public Methods } }