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; using static Step.Utils.SupportFunctions; 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 FindMagazinesPositions() { 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 != null && x.Shank.MagazineId != null) .ToList(); return tools; } public List GetAvailableShanks() { List dtoShanks = GetShanks() .Where(x => x.MagazineId == null) .ToList(); return dtoShanks; } public List GetAvailableShanksWithChilds() { List dtoShanks = GetShanks() .Where(x => x.MagazineId == null && x.ChildsTools.Count > 0) .ToList(); return dtoShanks; } public List GetAvailableTools() { List dtoTools = GetTools() .Where(x => x.ShankId == null || x.ShankId == 0) .ToList(); return dtoTools; } public DbNcToolModel AddTool(DTONewNcToolModel dtoTool, int toolId) { // Copy data DbNcToolModel dbTool = (DbNcToolModel)dtoTool; if (toolId == 0) // Get next id dbTool.ToolId = GetFirstFreeId(dbCtx.Tools.Select(x => x.ToolId).ToList()); else dbTool.ToolId = toolId; dbCtx.Tools.Add(dbTool); dbCtx.SaveChanges(); // Get foreign key data dbCtx.Entry(dbTool).Reference(x => x.Family).Load(); dbCtx.Entry(dbTool).Reference(x => x.Shank).Load(); return dbTool; } public DbNcToolModel UpdateTool(int toolId, DTONewNcToolModel dtoTool) { DbNcToolModel tool = FindTool(toolId); tool.Status = ((DbNcToolModel)dtoTool).Status; var shankId = tool.ShankId; // Update db model SupportFunctions.CopyProperties(dtoTool, tool); tool.ShankId = shankId; // Save dbCtx.SaveChanges(); return tool; } public DbNcToolModel UpdateToolOffset(int toolId, int position, int offsetId) { DbNcToolModel tool = FindTool(toolId); switch (position) { case 1: tool.OffsetId1 = offsetId; break; case 2: tool.OffsetId2 = offsetId; break; case 3: tool.OffsetId3 = offsetId; break; } // 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; if(dbFamily.FamilyId == 0) // Get next id dbFamily.FamilyId = GetFirstFreeId(dbCtx.Families.Select(x => x.FamilyId).ToList()); 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); // Set cooling byte dbFamily.CoolingByte = ((DbNcFamilyModel)family).CoolingByte; dbCtx.SaveChanges(); // Connect tools data dbCtx.Entry(dbFamily).Collection(x => x.Tools).Load(); return dbFamily; } public void DeleteFamily(int famId) { var family = FindFamily(famId); dbCtx.Families.Remove(family); dbCtx.SaveChanges(); } public DbNcShankModel AddShank(DTONewNcShankModel shank, int shankId = 0) { DbNcShankModel dbShank = (DbNcShankModel)shank; if (shankId == 0) // Get next id dbShank.ShankId = GetFirstFreeId(dbCtx.Shanks.Select(x => x.ShankId).ToList()); else dbShank.ShankId = shankId; 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.Balluf = dtoShank.Balluf; dbShank.MagazinePositionType = dtoShank.MagazinePositionType; dbCtx.SaveChanges(); dbCtx.Entry(dbShank).Collection(x => x.Tools).Load(); return dbShank; } public DbNcShankModel DeleteShank(int shankId) { DbNcShankModel shank = FindShankWithTools(shankId); DeleteShank(shank); return shank; } public void DeleteShank(DbNcShankModel shank) { dbCtx.Shanks.Remove(shank); dbCtx.SaveChanges(); } public DbNcMagazinePositionModel UpdatePosition(DbNcMagazinePositionModel dbPos, DTONcMagazinePositionModel dtoPos) { dbCtx.MagazinePositions.Attach(dbPos); 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 DbNcMagazinePositionModel UnloadShankInMagazine(byte magazineId, byte positionId, DbNcShankModel shank) { dbCtx.Shanks.Attach(shank); // set id to null shank.MagazineId = null; shank.PositionId = null; dbCtx.SaveChanges(); return FindMagazinePosition(magazineId, positionId); } 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(); } } }