a2f7a97485
Testing transaction
384 lines
11 KiB
C#
384 lines
11 KiB
C#
using Step.Model.DatabaseModels;
|
|
using Step.Model.DTOModels.ToolModels;
|
|
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<DbNcFamilyModel> FindFamilies()
|
|
{
|
|
List<DbNcFamilyModel> families = dbCtx
|
|
.Families
|
|
.Include("Tools")
|
|
.ToList();
|
|
|
|
return families;
|
|
}
|
|
|
|
public List<DTONcFamilyModel> GetFamilies()
|
|
{
|
|
List<DbNcFamilyModel> dbFamilies = FindFamilies();
|
|
|
|
return dbFamilies
|
|
.Select(x => (DTONcFamilyModel)x)
|
|
.ToList();
|
|
}
|
|
|
|
public DbNcToolModel FindTool(int toolId)
|
|
{
|
|
return dbCtx.Tools
|
|
.Where(x => x.ToolId == toolId)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public DbNcShankModel FindShank(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<DbNcToolModel> FindToolsWithDependencies()
|
|
{
|
|
List<DbNcToolModel> tools = dbCtx
|
|
.Tools
|
|
.Include("Family")
|
|
.Include("Shank")
|
|
.ToList();
|
|
|
|
return tools;
|
|
}
|
|
|
|
public List<DbNcToolModel> FindTools()
|
|
{
|
|
List<DbNcToolModel> tools = dbCtx
|
|
.Tools
|
|
.ToList();
|
|
|
|
return tools;
|
|
}
|
|
|
|
public List<DTONcToolModel> GetTools()
|
|
{
|
|
List<DbNcToolModel> dbTools = FindToolsWithDependencies();
|
|
|
|
return dbTools
|
|
.Select(x => (DTONcToolModel)x)
|
|
.ToList();
|
|
}
|
|
|
|
public List<DbNcShankModel> FindShanks()
|
|
{
|
|
List<DbNcShankModel> shanks = dbCtx
|
|
.Shanks
|
|
.Include("MagazinePosition")
|
|
.ToList();
|
|
|
|
return shanks;
|
|
}
|
|
|
|
public List<DTONcShankModel> GetShanks()
|
|
{
|
|
// Get shank from database
|
|
List<DbNcShankModel> dbShanks = dbCtx
|
|
.Shanks
|
|
.Include("Tools")
|
|
.ToList();
|
|
|
|
// Populate db shanks
|
|
List<DTONcShankModel> dtoShanks = dbShanks
|
|
.Select(x => (DTONcShankModel)x)
|
|
.ToList();
|
|
// new List<DTONcShankModel>();
|
|
//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<DbNcMagazinePositionModel> FindPositions()
|
|
{
|
|
List<DbNcMagazinePositionModel> positions = dbCtx
|
|
.MagazinePositions
|
|
.ToList();
|
|
|
|
return positions;
|
|
}
|
|
|
|
public List<DbNcMagazinePositionModel> FindMagazinePositions(byte magId)
|
|
{
|
|
return dbCtx
|
|
.MagazinePositions
|
|
.Where(x => x.MagazineId == magId)
|
|
.ToList();
|
|
}
|
|
|
|
|
|
public List<DTONcMagazinePositionModel> GetMagazinePositions(byte magId)
|
|
{
|
|
// Get only magazine positions that match with magazineId
|
|
List<DTONcMagazinePositionModel> 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<DbNcShankModel> 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<DTONcShankModel> GetMountedShanks(int magazineId)
|
|
{
|
|
List<DTONcShankModel> dtoShanks = GetShanks()
|
|
.Where(x => x.MagazineId != null)
|
|
.ToList();
|
|
|
|
return dtoShanks;
|
|
}
|
|
|
|
public List<DTONcShankModel> GetAvailableShanks()
|
|
{
|
|
List<DTONcShankModel> 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
|
|
tool.FamilyId = dtoTool.FamilyId;
|
|
tool.OffsetLength = dtoTool.OffsetLength;
|
|
tool.ResidualLife = dtoTool.ResidualLife;
|
|
tool.ResidualRevive = dtoTool.ResidualRevive;
|
|
tool.OffsetId1 = dtoTool.OffsetId1;
|
|
tool.OffsetId2 = dtoTool.OffsetId2;
|
|
tool.OffsetId3 = dtoTool.OffsetId3;
|
|
// Save
|
|
dbCtx.SaveChanges();
|
|
|
|
return tool;
|
|
}
|
|
|
|
public void DeleleTool(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();
|
|
dbCtx.Families.Attach(dbFamily);
|
|
|
|
return dbFamily;
|
|
}
|
|
|
|
public DbNcFamilyModel UpdateFamily(int familyId, DTONewNcFamilyModel family)
|
|
{
|
|
DbNcFamilyModel dbFamily = FindFamily(familyId);
|
|
dbFamily = (DbNcFamilyModel)family;
|
|
|
|
dbCtx.SaveChanges();
|
|
dbCtx.Families.Attach(dbFamily);
|
|
|
|
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();
|
|
|
|
dbCtx.Shanks.Attach(dbShank);
|
|
|
|
return dbShank;
|
|
}
|
|
|
|
public DbNcShankModel UpdateShank(int shankId, DTONewNcShankModel dtoShank)
|
|
{
|
|
DbNcShankModel ncShank = FindShank(shankId);
|
|
|
|
return UpdateShank(ncShank, dtoShank);
|
|
}
|
|
|
|
public DbNcShankModel UpdateShank(DbNcShankModel dbShank, DTONewNcShankModel dtoShank)
|
|
{
|
|
dbShank.MagazinePositionType = dtoShank.MagazinePositionType;
|
|
|
|
dbCtx.SaveChanges();
|
|
|
|
dbCtx.Shanks.Attach(dbShank);
|
|
|
|
return dbShank;
|
|
}
|
|
|
|
public void DeleteShank(int shankId)
|
|
{
|
|
DbNcShankModel shank = FindShank(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);
|
|
}
|
|
}
|
|
} |