using Step.Model.DatabaseModels; using Step.Model.DTOModels.ToolModels; using Step.Utils; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; namespace Step.Database.Controllers { public class NcToolManagerController : IDisposable { public DatabaseContext dbCtx; public NcToolManagerController() { // Initialize database context dbCtx = new DatabaseContext(); } public void Dispose() { // Clear database context dbCtx.Dispose(); } public List FindFamilies() { List families = dbCtx .Families .Include("Tools") .ToList(); return families; } public List GetFamilies() { List dbFamilies = FindFamilies(); return dbFamilies .Select(x => (DTONcFamilyModel)x) .ToList(); } public List FindFamiliesByShankId(int shankId) { DbNcShankModel shank = FindShankWithTools(shankId); // Get only families id int[] ids = shank.Tools.Select(x => x.FamilyId).ToArray(); return FindFamilies() // Get Families .Where(x => ids.Contains(x.FamilyId)) // Filter by ids .ToList(); } public DbNcToolModel FindTool(int toolId) { return dbCtx.Tools .Where(x => x.ToolId == toolId) .FirstOrDefault(); } public DbNcShankModel FindShankWithTools(int shankId) { return dbCtx.Shanks .Include("Tools") .Where(x => x.ShankId == shankId) .FirstOrDefault(); } public DbNcFamilyModel FindFamily(int familyId) { return dbCtx .Families .Where(x => x.FamilyId == familyId) .FirstOrDefault(); } public List FindToolsWithDependencies() { List tools = dbCtx .Tools .Include("Family") .Include("Shank") .ToList(); return tools; } public List FindToolsByShankIdWithDependencies(int shankId) { List tools = FindToolsWithDependencies() .Where(x => x.ShankId == shankId) .ToList(); return tools; } public List FindTools() { List tools = dbCtx .Tools .ToList(); return tools; } public List GetTools() { List dbTools = FindToolsWithDependencies(); return dbTools .Select(x => (DTONcToolModel)x) .ToList(); } public List FindShanks() { List shanks = dbCtx .Shanks .Include("MagazinePosition") .ToList(); return shanks; } public DbNcShankModel FindShanksByPositions(int magazineId, int positionId) { DbNcShankModel shank = FindShanks() .Where(x => x.MagazineId == magazineId && x.PositionId == positionId) .FirstOrDefault(); return shank; } public List GetShanks() { // Get shank from database List dbShanks = dbCtx .Shanks .Include("Tools") .ToList(); // Populate db shanks List dtoShanks = dbShanks .Select(x => (DTONcShankModel)x) .ToList(); // new List(); //foreach (var shank in dbShanks) //{ // //dbCtx.Shanks.Attach(shank); // DTONcShankModel dtoShank = (DTONcShankModel)shank; // dtoShanks.Add(dtoShank); //} return dtoShanks; } public DTONcShankModel GetShank(int shankId) { // Get shank from database DbNcShankModel dbShank = dbCtx .Shanks .Where(x => x.ShankId == shankId) .Include("Tools") .FirstOrDefault(); // Convert into DTOModel DTONcShankModel dtoShanks = (DTONcShankModel)dbShank; return dtoShanks; } public List FindMagazinePositions() { List positions = dbCtx .MagazinePositions .ToList(); return positions; } public List FindMagazinePositions(byte magId) { return dbCtx .MagazinePositions .Where(x => x.MagazineId == magId) .ToList(); } public List GetMagazinePositions(byte magId) { // Get only magazine positions that match with magazineId List magPos = FindMagazinePositions(magId).Select(x => (DTONcMagazinePositionModel)x).ToList(); // Get&filter shanks by magazineId in order to get only mounted shanks in the current magazineId List shanks = dbCtx.Shanks.Where(x => x.MagazineId == magId).ToList(); foreach(DbNcShankModel shank in shanks) { // Populate magazinePosition shank Id magPos .Where(x => x.PositionId == shank.PositionId) .FirstOrDefault() .ShankId = shank.ShankId; } // Convert in DTOModel and return return magPos; } public DbNcMagazinePositionModel FindMagazinePosition(byte magId, byte posId) { DbNcMagazinePositionModel positions = dbCtx .MagazinePositions .Where(x => x.MagazineId == magId && x.PositionId == posId) .FirstOrDefault(); return positions; } public List GetMountedShanks(int magazineId) { List dtoShanks = GetShanks() .Where(x => x.MagazineId != null) .ToList(); return dtoShanks; } public List GetMountedTools() { List tools = FindToolsWithDependencies() .Where(x => x.Shank.MagazineId != null) .ToList(); return tools; } public List GetAvailableShanks() { List dtoShanks = GetShanks() .Where(x => x.MagazineId == null && x.Tools.Count > 0) .ToList(); return dtoShanks; } public DbNcToolModel AddTool(DTONewNcToolModel dtoTool) { DbNcToolModel tool = (DbNcToolModel)dtoTool; dbCtx.Tools.Add(tool); dbCtx.SaveChanges(); // Get foreign key data dbCtx.Entry(tool).Reference(x => x.Family).Load(); dbCtx.Entry(tool).Reference(x => x.Shank).Load(); return tool; } public DbNcToolModel UpdateTool(int toolId, DTONewNcToolModel dtoTool) { DbNcToolModel tool = FindTool(toolId); // Update db model SupportFunctions.CopyProperties(dtoTool, tool); // Save dbCtx.SaveChanges(); return tool; } public void DeleteTool(int toolId) { DbNcToolModel tool = FindTool(toolId); DeleteTool(tool); } public void DeleteTool(DbNcToolModel tool) { dbCtx.Tools.Remove(tool); dbCtx.SaveChanges(); } public DbNcFamilyModel AddFamily(DTONewNcFamilyModel family) { DbNcFamilyModel dbFamily = (DbNcFamilyModel)family; dbCtx.Families.Add(dbFamily); dbCtx.SaveChanges(); return dbFamily; } public DbNcFamilyModel UpdateFamily(int familyId, DTONewNcFamilyModel family) { DbNcFamilyModel dbFamily = FindFamily(familyId); // Copy data from NewModel to DbModel SupportFunctions.CopyProperties(family, dbFamily); dbCtx.SaveChanges(); // Connect tools data dbCtx.Entry(dbFamily).Collection(x => x.Tools).Load(); return dbFamily; } public void DeleteFamily(DbNcFamilyModel family) { dbCtx.Families.Attach(family); dbCtx.Families.Remove(family); dbCtx.SaveChanges(); } public DbNcShankModel AddShank(DTONewNcShankModel shank) { DbNcShankModel dbShank = (DbNcShankModel)shank; dbCtx.Shanks.Add(dbShank); dbCtx.SaveChanges(); return dbShank; } public DbNcShankModel UpdateShank(int shankId, DTONewNcShankModel dtoShank) { DbNcShankModel ncShank = FindShankWithTools(shankId); return UpdateShank(ncShank, dtoShank); } public DbNcShankModel UpdateShank(DbNcShankModel dbShank, DTONewNcShankModel dtoShank) { dbShank.MagazinePositionType = dtoShank.MagazinePositionType; dbCtx.SaveChanges(); dbCtx.Entry(dbShank).Collection(x => x.Tools).Load(); return dbShank; } public void DeleteShank(int shankId) { DbNcShankModel shank = FindShankWithTools(shankId); DeleteShank(shank); } public void DeleteShank(DbNcShankModel shank) { dbCtx.Shanks.Remove(shank); dbCtx.SaveChanges(); } public DbNcMagazinePositionModel UpdatePosition(DbNcMagazinePositionModel dbPos, DTONcMagazinePositionModel dtoPos) { dbPos.Type = dtoPos.Type; dbPos.Disabled = dtoPos.Disabled; dbCtx.SaveChanges(); return dbPos; } public DbNcMagazinePositionModel LoadShankInMagazine(byte magazineId, byte positionId, DbNcShankModel shank) { dbCtx.Shanks.Attach(shank); // Set ids with new positions shank.MagazineId = magazineId; shank.PositionId = positionId; dbCtx.SaveChanges(); return FindMagazinePosition(magazineId, positionId); } public DTONcShankModel UnloadShankInMagazine(byte magazineId, byte positionId, DbNcShankModel shank) { dbCtx.Shanks.Attach(shank); // set id to null shank.MagazineId = null; shank.PositionId = null; dbCtx.SaveChanges(); return (DTONcShankModel)shank; } public DTONcShankModel LoadToolIntoShank(DbNcToolModel tool, int shankId) { dbCtx.Tools.Attach(tool); // Set tool shankId tool.ShankId = shankId; dbCtx.SaveChanges(); return GetShank(shankId); } public DTONcShankModel UnloadToolFromShank(DbNcToolModel tool) { dbCtx.Tools.Attach(tool); int? shankId = tool.ShankId; // Set to null shankId tool.ShankId = null; dbCtx.SaveChanges(); return GetShank(shankId.Value); } public void SetupMagazinePositions(List config) { dbCtx.MagazinePositions.AddRange(config); dbCtx.SaveChanges(); } public void UpdateToolsData(List tools) { foreach (var tool in tools) { DbNcToolModel tmpTool = dbCtx.Tools.Where(x => x.ToolId == tool.ToolId).FirstOrDefault(); tmpTool = tool; } dbCtx.SaveChanges(); } } }