diff --git a/Factory/FactoryDatabaseImplement/Implements/ClientStorage.cs b/Factory/FactoryDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..e5c61e2 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,84 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.SearchModels; +using FactoryContracts.StoragesContracts; +using FactoryContracts.ViewModels; +using FactoryDatabaseImplement.Models; + +namespace FactoryDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new FactoryDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new FactoryDatabase(); + return context.Clients + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new FactoryDatabase(); + return context.Clients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new FactoryDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new FactoryDatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new FactoryDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Implements/ExecutionPhaseStorage.cs b/Factory/FactoryDatabaseImplement/Implements/ExecutionPhaseStorage.cs new file mode 100644 index 0000000..0a16fa0 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Implements/ExecutionPhaseStorage.cs @@ -0,0 +1,84 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.SearchModels; +using FactoryContracts.StoragesContracts; +using FactoryContracts.ViewModels; +using FactoryDatabaseImplement.Models; + +namespace FactoryDatabaseImplement.Implements +{ + public class ExecutionPhaseStorage : IExecutionPhaseStorage + { + public List GetFullList() + { + using var context = new FactoryDatabase(); + return context.ExecutionPhases + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ExecutionPhaseSearchModel model) + { + if (string.IsNullOrEmpty(model.ExecutionPhaseName)) + { + return new(); + } + using var context = new FactoryDatabase(); + return context.ExecutionPhases + .Where(x => x.ExecutionPhaseName.Contains(model.ExecutionPhaseName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ExecutionPhaseViewModel? GetElement(ExecutionPhaseSearchModel model) + { + if (string.IsNullOrEmpty(model.ExecutionPhaseName) && !model.Id.HasValue) + { + return null; + } + using var context = new FactoryDatabase(); + return context.ExecutionPhases + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExecutionPhaseName) && x.ExecutionPhaseName == model.ExecutionPhaseName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ExecutionPhaseViewModel? Insert(ExecutionPhaseBindingModel model) + { + var newExecutionPhase = ExecutionPhase.Create(model); + if (newExecutionPhase == null) + { + return null; + } + using var context = new FactoryDatabase(); + context.ExecutionPhases.Add(newExecutionPhase); + context.SaveChanges(); + return newExecutionPhase.GetViewModel; + } + + public ExecutionPhaseViewModel? Update(ExecutionPhaseBindingModel model) + { + using var context = new FactoryDatabase(); + var component = context.ExecutionPhases.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ExecutionPhaseViewModel? Delete(ExecutionPhaseBindingModel model) + { + using var context = new FactoryDatabase(); + var element = context.ExecutionPhases.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ExecutionPhases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs b/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs new file mode 100644 index 0000000..c1d8d28 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs @@ -0,0 +1,111 @@ +using Microsoft.EntityFrameworkCore; +using FactoryContracts.BindingModels; +using FactoryContracts.SearchModels; +using FactoryContracts.StoragesContracts; +using FactoryContracts.ViewModels; +using FactoryDatabaseImplement.Models; + +namespace FactoryDatabaseImplement.Implements +{ + public class PlanProductionStorage : IPlanProductionStorage + { + public PlanProductionViewModel? Delete(PlanProductionBindingModel model) + { + using var context = new FactoryDatabase(); + var element = context.PlanProductions + .Include(x => x.ExecutionPhase) + .Include(x => x.Workpieces) + .ThenInclude( x => x.Workpiece) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.PlanProductions.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public PlanProductionViewModel? GetElement(PlanProductionSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FactoryDatabase(); + if (model.Id.HasValue) + return context.PlanProductions + .Include(x => x.ExecutionPhase) + .Include(x =>x.Workpieces) + .ThenInclude(x => x.Workpiece) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + return null; + } + + public List GetFilteredList(PlanProductionSearchModel model) + { + using var context = new FactoryDatabase(); + + + if (model.Id.HasValue) + { + return context.PlanProductions + .Where(x => x.Id == model.Id) + .Include(x => x.ExecutionPhase) + .Include(x => x.Workpieces) + .ThenInclude(x => x.Workpiece) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + + } + + public List GetFullList() + { + using var context = new FactoryDatabase(); + return context.PlanProductions + .Include(x => x.ExecutionPhase) + .Include(x => x.Workpieces) + .ThenInclude(x => x.Workpiece) + .ToList() + .Select(x =>x.GetViewModel) + .ToList(); + } + + public PlanProductionViewModel? Insert(PlanProductionBindingModel model) + { + using var context = new FactoryDatabase(); + var newPlanProduction = PlanProduction.Create(context, model); + if (newPlanProduction == null) + { + return null; + } + + + context.PlanProductions.Add(newPlanProduction); + context.SaveChanges(); + return newPlanProduction.GetViewModel; + } + + public PlanProductionViewModel? Update(PlanProductionBindingModel model) + { + using var context = new FactoryDatabase(); + var order = context.PlanProductions + .Include(x => x.ExecutionPhase) + .Include(x => x.Workpieces) + .ThenInclude(x => x.Workpiece) + .FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Implements/WorkpieceStorage.cs b/Factory/FactoryDatabaseImplement/Implements/WorkpieceStorage.cs new file mode 100644 index 0000000..ba30a8d --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Implements/WorkpieceStorage.cs @@ -0,0 +1,106 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.SearchModels; +using FactoryContracts.StoragesContracts; +using FactoryContracts.ViewModels; +using FactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FactoryDatabaseImplement.Implements +{ + public class WorkpieceStorage : IWorkpieceStorage + { + public List GetFullList() + { + using var context = new FactoryDatabase(); + return context.Workpieces + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(WorkpieceSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkpieceName)) + { + return new(); + } + using var context = new FactoryDatabase(); + return context.Workpieces + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.WorkpieceName.Contains(model.WorkpieceName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public WorkpieceViewModel? GetElement(WorkpieceSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkpieceName) && !model.Id.HasValue) + { + return null; + } + using var context = new FactoryDatabase(); + return context.Workpieces + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkpieceName) && x.WorkpieceName == model.WorkpieceName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public WorkpieceViewModel? Insert(WorkpieceBindingModel model) + { + using var context = new FactoryDatabase(); + var newWorkpiece = Workpiece.Create(context, model); + if (newWorkpiece == null) + { + return null; + } + context.Workpieces.Add(newWorkpiece); + context.SaveChanges(); + return newWorkpiece.GetViewModel; + } + + public WorkpieceViewModel? Update(WorkpieceBindingModel model) + { + using var context = new FactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var work = context.Workpieces.FirstOrDefault(rec => rec.Id == model.Id); + if (work == null) + { + return null; + } + work.Update(model); + context.SaveChanges(); + work.UpdateProducts(context, model); + transaction.Commit(); + return work.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public WorkpieceViewModel? Delete(WorkpieceBindingModel model) + { + using var context = new FactoryDatabase(); + var element = context.Workpieces + .Include(x => x.Products) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Workpieces.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs index 338d4db..8e87651 100644 --- a/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs +++ b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs @@ -79,7 +79,7 @@ namespace FactoryDatabaseImplement.Models } - public PlanProductionBindingModel GetViewModel => new() + public PlanProductionViewModel GetViewModel => new() { Id = Id, ClientId = ClientId, diff --git a/Factory/FactoryDatabaseImplement/Models/Workpiece.cs b/Factory/FactoryDatabaseImplement/Models/Workpiece.cs index 5c68e7e..7471cf6 100644 --- a/Factory/FactoryDatabaseImplement/Models/Workpiece.cs +++ b/Factory/FactoryDatabaseImplement/Models/Workpiece.cs @@ -66,7 +66,7 @@ namespace FactoryDatabaseImplement.Models Material = model.Material; } - public WorkpieceBindingModel GetViewModel => new() + public WorkpieceViewModel GetViewModel => new() { Id = Id, WorkpieceName = WorkpieceName,