Исправления и реализация хранилищ

This commit is contained in:
sardq 2024-04-21 10:46:25 +04:00
parent dd664a4ed5
commit ee40bfeb55
6 changed files with 387 additions and 2 deletions

View File

@ -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<ClientViewModel> GetFullList()
{
using var context = new FactoryDatabase();
return context.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> 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;
}
}
}

View File

@ -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<ExecutionPhaseViewModel> GetFullList()
{
using var context = new FactoryDatabase();
return context.ExecutionPhases
.Select(x => x.GetViewModel)
.ToList();
}
public List<ExecutionPhaseViewModel> 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;
}
}
}

View File

@ -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<PlanProductionViewModel> 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<PlanProductionViewModel> 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;
}
}
}

View File

@ -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<WorkpieceViewModel> 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<WorkpieceViewModel> 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;
}
}
}

View File

@ -79,7 +79,7 @@ namespace FactoryDatabaseImplement.Models
}
public PlanProductionBindingModel GetViewModel => new()
public PlanProductionViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,

View File

@ -66,7 +66,7 @@ namespace FactoryDatabaseImplement.Models
Material = model.Material;
}
public WorkpieceBindingModel GetViewModel => new()
public WorkpieceViewModel GetViewModel => new()
{
Id = Id,
WorkpieceName = WorkpieceName,