From e4533c4b03eda93ed02607019f809c958cd96cbd Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Sat, 27 Apr 2024 22:13:33 +0400 Subject: [PATCH] update bd --- .../BusinessLogic/MachineLogic.cs | 46 ++++---- .../BusinessLogic/WorkshopLogic.cs | 111 ++++++++++++++++++ .../BindingModels/MachineBindingModel.cs | 3 +- .../BindingModels/WorkshopBindingModel.cs | 2 +- .../Contracts/ViewModels/MachineViewModel.cs | 3 +- .../Contracts/ViewModels/WorkshopViewModel.cs | 4 +- .../Implements/MachineStorage.cs | 77 ++++++++++++ .../Implements/WorkerStorage.cs | 77 ++++++++++++ .../Implements/WorkshopStorage.cs | 78 ++++++++++++ Course/DatabaseImplement/Models/Machine.cs | 16 +-- .../DatabaseImplement/Models/WorkerMachine.cs | 2 - .../Models/WorkerWorkshop.cs | 2 - Course/DatabaseImplement/Models/Workshop.cs | 20 ++-- 13 files changed, 384 insertions(+), 57 deletions(-) create mode 100644 Course/BusinessLogic/BusinessLogic/WorkshopLogic.cs create mode 100644 Course/DatabaseImplement/Implements/MachineStorage.cs create mode 100644 Course/DatabaseImplement/Implements/WorkerStorage.cs create mode 100644 Course/DatabaseImplement/Implements/WorkshopStorage.cs diff --git a/Course/BusinessLogic/BusinessLogic/MachineLogic.cs b/Course/BusinessLogic/BusinessLogic/MachineLogic.cs index 8f5345e..dac014b 100644 --- a/Course/BusinessLogic/BusinessLogic/MachineLogic.cs +++ b/Course/BusinessLogic/BusinessLogic/MachineLogic.cs @@ -11,15 +11,15 @@ namespace BusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IMachineStorage _machineStorage; - public ProductLogic(ILogger logger, IProductStorage productStorage) + public MachineLogic(ILogger logger, IMachineStorage machineStorage) { _logger = logger; - _productStorage = productStorage; + _machineStorage = machineStorage; } - public List? ReadList(ProductSearchModel? model) + public List? ReadList(MachineSearchModel? model) { - _logger.LogInformation("ReadList. ProductName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId); - var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model); + _logger.LogInformation("ReadList. MachineTitle:{Title}. Id:{Id}. UserId:{UserId}", model?.Title, model?.Id, model?.UserId); + var list = model == null ? _machineStorage.GetFullList() : _machineStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -28,14 +28,14 @@ namespace BusinessLogic.BusinessLogic _logger.LogWarning("ReadList. Count:{Count}", list.Count); return list; } - public ProductViewModel? ReadElement(ProductSearchModel? model) + public MachineViewModel? ReadElement(MachineSearchModel? model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement. ProductName:{Name}. Id:{Id}", model.Name, model.Id); - var elem = _productStorage.GetElement(model); + _logger.LogInformation("ReadElement. MachineTitle:{Title}. Id:{Id}", model.Title, model.Id); + var elem = _machineStorage.GetElement(model); if (elem == null) { _logger.LogWarning("ReadElement element not found"); @@ -44,7 +44,7 @@ namespace BusinessLogic.BusinessLogic _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); return elem; } - private void CheckModel(ProductBindingModel? model, bool withParams = true) + private void CheckModel(MachineBindingModel? model, bool withParams = true) { if (model == null) { @@ -54,49 +54,49 @@ namespace BusinessLogic.BusinessLogic { return; } - if (string.IsNullOrEmpty(model.Name)) + if (string.IsNullOrEmpty(model.Title)) { - throw new ArgumentNullException("Нет названия изделия", nameof(model.Name)); + throw new ArgumentNullException("Нет названия изделия", nameof(model.Title)); } - if (model.Cost <= 0) + if (string.IsNullOrEmpty(model.Country)) { - throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Cost)); + throw new ArgumentNullException("Нет страны изготовителя", nameof(model.Country)); } - _logger.LogInformation("Product. ProductName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); - var elem = _productStorage.GetElement(new ProductSearchModel + _logger.LogInformation("Machine. MachineTitle:{Title}. Country:{Country}. Id:{Id}", model.Title, model.Country, model.Id); + var elem = _machineStorage.GetElement(new MachineSearchModel { - Name = model.Name + Title = model.Title }); if (elem != null && elem.Id != model.Id) { - throw new InvalidOperationException("Изделие с таким названием уже существует"); + throw new InvalidOperationException("Станок с таким названием уже существует"); } } - public bool Create(ProductBindingModel? model) + public bool Create(MachineBindingModel? model) { CheckModel(model); - if (_productStorage.Insert(model!) == null) + if (_machineStorage.Insert(model!) == null) { _logger.LogWarning("Insert error"); return false; } return true; } - public bool Update(ProductBindingModel? model) + public bool Update(MachineBindingModel? model) { CheckModel(model); - if (_productStorage.Update(model!) == null) + if (_machineStorage.Update(model!) == null) { _logger.LogWarning("Update error"); return false; } return true; } - public bool Delete(ProductBindingModel? model) + public bool Delete(MachineBindingModel? model) { CheckModel(model, false); _logger.LogInformation("Delete Id:{Id}", model!.Id); - if (_productStorage.Delete(model!) == null) + if (_machineStorage.Delete(model!) == null) { _logger.LogWarning("Delete error"); return false; diff --git a/Course/BusinessLogic/BusinessLogic/WorkshopLogic.cs b/Course/BusinessLogic/BusinessLogic/WorkshopLogic.cs new file mode 100644 index 0000000..3858c2e --- /dev/null +++ b/Course/BusinessLogic/BusinessLogic/WorkshopLogic.cs @@ -0,0 +1,111 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicsContracts; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace BusinessLogic.BusinessLogic +{ + public class WorkshopLogic : IWorkshopLogic + { + private readonly ILogger _logger; + private readonly IWorkshopStorage _machineStorage; + public WorkshopLogic(ILogger logger, IWorkshopStorage machineStorage) + { + _logger = logger; + _machineStorage = machineStorage; + } + public List? ReadList(WorkshopSearchModel? model) + { + _logger.LogInformation("ReadList. WorkshopTitle:{Title}. Id:{Id}. UserId:{UserId}", model?.Title, model?.Id, model?.UserId); + var list = model == null ? _machineStorage.GetFullList() : _machineStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogWarning("ReadList. Count:{Count}", list.Count); + return list; + } + public WorkshopViewModel? ReadElement(WorkshopSearchModel? model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. WorkshopTitle:{Title}. Id:{Id}", model.Title, model.Id); + var elem = _machineStorage.GetElement(model); + if (elem == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); + return elem; + } + private void CheckModel(WorkshopBindingModel? model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Title)) + { + throw new ArgumentNullException("Нет названия изделия", nameof(model.Title)); + } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адреса цеха", nameof(model.Address)); + } + if (string.IsNullOrEmpty(model.Director)) + { + throw new ArgumentNullException("Нет директора цеха", nameof(model.Director)); + } + _logger.LogInformation("Workshop. WorkshopTitle:{Title}. Address:{Address}. Director:{Director}. Id:{Id}", model.Title, model.Address, model.Director, model.Id); + var elem = _machineStorage.GetElement(new WorkshopSearchModel + { + Title = model.Title + }); + if (elem != null && elem.Id != model.Id) + { + throw new InvalidOperationException("Цех с таким названием уже существует"); + } + } + public bool Create(WorkshopBindingModel? model) + { + CheckModel(model); + if (_machineStorage.Insert(model!) == null) + { + _logger.LogWarning("Insert error"); + return false; + } + return true; + } + public bool Update(WorkshopBindingModel? model) + { + CheckModel(model); + if (_machineStorage.Update(model!) == null) + { + _logger.LogWarning("Update error"); + return false; + } + return true; + } + public bool Delete(WorkshopBindingModel? model) + { + CheckModel(model, false); + _logger.LogInformation("Delete Id:{Id}", model!.Id); + if (_machineStorage.Delete(model!) == null) + { + _logger.LogWarning("Delete error"); + return false; + } + return true; + } + } +} diff --git a/Course/Contracts/BindingModels/MachineBindingModel.cs b/Course/Contracts/BindingModels/MachineBindingModel.cs index f8e0999..f0de0a0 100644 --- a/Course/Contracts/BindingModels/MachineBindingModel.cs +++ b/Course/Contracts/BindingModels/MachineBindingModel.cs @@ -8,7 +8,6 @@ namespace Contracts.BindingModels public int UserId { get; set; } public string Title { get; set; } = string.Empty; public string Country { get; set; } = string.Empty; - public int ProductId { get; set; } - public Dictionary MachineWorker { get; set; } = new(); + public Dictionary MachineWorker { get; set; } = new(); } } diff --git a/Course/Contracts/BindingModels/WorkshopBindingModel.cs b/Course/Contracts/BindingModels/WorkshopBindingModel.cs index 3b262b7..fc8160e 100644 --- a/Course/Contracts/BindingModels/WorkshopBindingModel.cs +++ b/Course/Contracts/BindingModels/WorkshopBindingModel.cs @@ -9,7 +9,7 @@ namespace Contracts.BindingModels public string Title { get; set; } = string.Empty; public string Address { get; set; } = string.Empty; public string Director { get; set; } = string.Empty; - public int ProductionId { get; set; } + public int? ProductionId { get; set; } public Dictionary WorkshopWorker { get; set; } = new(); } } diff --git a/Course/Contracts/ViewModels/MachineViewModel.cs b/Course/Contracts/ViewModels/MachineViewModel.cs index 49cd82b..ef66147 100644 --- a/Course/Contracts/ViewModels/MachineViewModel.cs +++ b/Course/Contracts/ViewModels/MachineViewModel.cs @@ -11,7 +11,6 @@ namespace Contracts.ViewModels [DisplayName("Страна производитель")] public string Country { get; set; } = string.Empty; public int UserId { get; set; } - public int ProductId { get; set; } - public Dictionary? WorkerMachines { get; set; } + public Dictionary? WorkerMachines { get; set; } } } diff --git a/Course/Contracts/ViewModels/WorkshopViewModel.cs b/Course/Contracts/ViewModels/WorkshopViewModel.cs index 358d929..872b782 100644 --- a/Course/Contracts/ViewModels/WorkshopViewModel.cs +++ b/Course/Contracts/ViewModels/WorkshopViewModel.cs @@ -13,7 +13,7 @@ namespace Contracts.ViewModels [DisplayName("ФИО директора цеха")] public string Director { get; set; } = string.Empty; public int UserId { get; set; } - public int ProductionId { get; set; } - public Dictionary? WorkerWorkshops { get; set; } + public int? ProductionId { get; set; } + public Dictionary? WorkerWorkshops { get; set; } } } diff --git a/Course/DatabaseImplement/Implements/MachineStorage.cs b/Course/DatabaseImplement/Implements/MachineStorage.cs new file mode 100644 index 0000000..2bf4a81 --- /dev/null +++ b/Course/DatabaseImplement/Implements/MachineStorage.cs @@ -0,0 +1,77 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; + +namespace DatabaseImplement.Implements +{ + public class MachineStorage : IMachineStorage + { + public MachineViewModel? Delete(MachineBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newMachine = context.Machines.FirstOrDefault(x => x.Id == model.Id); + if (newMachine == null) + return null; + context.Machines.Remove(newMachine); + context.SaveChanges(); + return newMachine.GetViewModel; + } + + public MachineViewModel? GetElement(MachineSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Machines.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title.Contains(model.Title)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(MachineSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Machines.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Machines.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Machines.Where(x => model.Title == x.Title).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Machines.Select(x => x.GetViewModel).ToList(); + } + + public MachineViewModel? Insert(MachineBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newMachine = Machine.Create(model, context); + if (newMachine == null) + return null; + context.Machines.Add(newMachine); + context.SaveChanges(); + return newMachine.GetViewModel; + } + + public MachineViewModel? Update(MachineBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newMachine = context.Machines.FirstOrDefault(x => x.Id == model.Id); + if (newMachine == null) + return null; + newMachine.Update(model); + context.SaveChanges(); + return newMachine.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Implements/WorkerStorage.cs b/Course/DatabaseImplement/Implements/WorkerStorage.cs new file mode 100644 index 0000000..400c5be --- /dev/null +++ b/Course/DatabaseImplement/Implements/WorkerStorage.cs @@ -0,0 +1,77 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; + +namespace DatabaseImplement.Implements +{ + public class WorkerStorage : IWorkerStorage + { + public WorkerViewModel? Delete(WorkerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorker = context.Workers.FirstOrDefault(x => x.Id == model.Id); + if (newWorker == null) + return null; + context.Workers.Remove(newWorker); + context.SaveChanges(); + return newWorker.GetViewModel; + } + + public WorkerViewModel? GetElement(WorkerSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Workers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(WorkerSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Workers.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Workers.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Workers.Where(x => model.Name == x.Name).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Workers.Select(x => x.GetViewModel).ToList(); + } + + public WorkerViewModel? Insert(WorkerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorker = Worker.Create(model); + if (newWorker == null) + return null; + context.Workers.Add(newWorker); + context.SaveChanges(); + return newWorker.GetViewModel; + } + + public WorkerViewModel? Update(WorkerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorker = context.Workers.FirstOrDefault(x => x.Id == model.Id); + if (newWorker == null) + return null; + newWorker.Update(model); + context.SaveChanges(); + return newWorker.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Implements/WorkshopStorage.cs b/Course/DatabaseImplement/Implements/WorkshopStorage.cs new file mode 100644 index 0000000..219008a --- /dev/null +++ b/Course/DatabaseImplement/Implements/WorkshopStorage.cs @@ -0,0 +1,78 @@ + +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; + +namespace DatabaseImplement.Implements +{ + public class WorkshopStorage : IWorkshopStorage + { + public WorkshopViewModel? Delete(WorkshopBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id); + if (newWorkshop == null) + return null; + context.Workshops.Remove(newWorkshop); + context.SaveChanges(); + return newWorkshop.GetViewModel; + } + + public WorkshopViewModel? GetElement(WorkshopSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Workshops.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title.Contains(model.Title)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(WorkshopSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Workshops.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Workshops.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Workshops.Where(x => model.Title == x.Title).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Workshops.Select(x => x.GetViewModel).ToList(); + } + + public WorkshopViewModel? Insert(WorkshopBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorkshop = Workshop.Create(context, model); + if (newWorkshop == null) + return null; + context.Workshops.Add(newWorkshop); + context.SaveChanges(); + return newWorkshop.GetViewModel; + } + + public WorkshopViewModel? Update(WorkshopBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newWorkshop = context.Workshops.FirstOrDefault(x => x.Id == model.Id); + if (newWorkshop == null) + return null; + newWorkshop.Update(model); + context.SaveChanges(); + return newWorkshop.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Models/Machine.cs b/Course/DatabaseImplement/Models/Machine.cs index 65ecae6..b006585 100644 --- a/Course/DatabaseImplement/Models/Machine.cs +++ b/Course/DatabaseImplement/Models/Machine.cs @@ -21,18 +21,15 @@ namespace DatabaseImplement.Models public string Country { get; private set; } = string.Empty; [Required] public int UserId { get; private set; } - [Required] - public int ProductId { get; private set; } - public virtual Product Product { get; set; } - private Dictionary? _workerMachines = null; + private Dictionary? _workerMachines = null; [NotMapped] - public Dictionary? WorkerMachines + public Dictionary? WorkerMachines { get { if (_workerMachines == null) { - _workerMachines = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel, recDp.Count)); + _workerMachines = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel)); } return _workerMachines; } @@ -47,8 +44,6 @@ namespace DatabaseImplement.Models Id = model.Id, Title = model.Title, Country = model.Country, - ProductId = model.ProductId, - Product = context.Products.FirstOrDefault(x => x.Id == model.ProductId)!, UserId = model.UserId, Workers = model.MachineWorker.Select(x => new WorkerMachine { @@ -67,7 +62,6 @@ namespace DatabaseImplement.Models Title = Title, Country = Country, UserId = UserId, - ProductId = ProductId, WorkerMachines = WorkerMachines }; public void UpdateWorkers(FactoryGoWorkDatabase context, MachineBindingModel model) @@ -79,7 +73,6 @@ namespace DatabaseImplement.Models context.SaveChanges(); foreach (var upWorker in machineWorkers) { - upWorker.Count = model.MachineWorker[upWorker.WorkerId].Item2; model.MachineWorker.Remove(upWorker.WorkerId); } context.SaveChanges(); @@ -90,8 +83,7 @@ namespace DatabaseImplement.Models context.WorkerMachines.Add(new WorkerMachine { Machine = machine, - Worker = context.Workers.First(x => x.Id == dp.Key), - Count = dp.Value.Item2 + Worker = context.Workers.First(x => x.Id == dp.Key) }); context.SaveChanges(); } diff --git a/Course/DatabaseImplement/Models/WorkerMachine.cs b/Course/DatabaseImplement/Models/WorkerMachine.cs index ed0d687..0388ade 100644 --- a/Course/DatabaseImplement/Models/WorkerMachine.cs +++ b/Course/DatabaseImplement/Models/WorkerMachine.cs @@ -9,8 +9,6 @@ namespace DatabaseImplement.Models public int WorkerId { get; set; } [Required] public int MachineId { get; set; } - [Required] - public int Count { get; set; } public virtual Worker Worker { get; set; } = new(); public virtual Machine Machine { get; set; } = new(); } diff --git a/Course/DatabaseImplement/Models/WorkerWorkshop.cs b/Course/DatabaseImplement/Models/WorkerWorkshop.cs index cd417c1..cd6f145 100644 --- a/Course/DatabaseImplement/Models/WorkerWorkshop.cs +++ b/Course/DatabaseImplement/Models/WorkerWorkshop.cs @@ -9,8 +9,6 @@ namespace DatabaseImplement.Models public int WorkerId { get; set; } [Required] public int WorkshopId { get; set; } - [Required] - public int Count { get; set; } public virtual Worker Worker { get; set; } = new(); public virtual Workshop Workshop { get; set; } = new(); } diff --git a/Course/DatabaseImplement/Models/Workshop.cs b/Course/DatabaseImplement/Models/Workshop.cs index c2777d2..7a13aec 100644 --- a/Course/DatabaseImplement/Models/Workshop.cs +++ b/Course/DatabaseImplement/Models/Workshop.cs @@ -18,17 +18,17 @@ namespace DatabaseImplement.Models [Required] public int UserId { get; set; } [Required] - public int ProductionId { get; set; } - public virtual Production Production { get; set; } - private Dictionary? _workerWorkshops = null; + public int? ProductionId { get; set; } + public virtual Production? Production { get; set; } + private Dictionary? _workerWorkshops = null; [NotMapped] - public Dictionary? WorkerWorkshops + public Dictionary? WorkerWorkshops { get { if(_workerWorkshops == null) { - _workerWorkshops = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel, recDp.Count)); + _workerWorkshops = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel)); } return _workerWorkshops; } @@ -36,7 +36,7 @@ namespace DatabaseImplement.Models [ForeignKey("WorkshopId")] public List Workers { get; set; } = new(); public virtual Guarantor Guarantor { get; set; } - public static Workshop Create(FactoryGoWorkDatabase context, WorkshopBindingModel model) + public static Workshop? Create(FactoryGoWorkDatabase context, WorkshopBindingModel model) { return new Workshop() { @@ -45,7 +45,7 @@ namespace DatabaseImplement.Models Address = model.Address, Director = model.Director, ProductionId = model.ProductionId, - Production = context.Productions.FirstOrDefault(x => x.Id == model.ProductionId)!, + Production = model.ProductionId.HasValue ? context.Productions.FirstOrDefault(x => x.Id == model.ProductionId) : null, UserId = model.UserId, Workers = model.WorkshopWorker.Select(x => new WorkerWorkshop { @@ -65,7 +65,7 @@ namespace DatabaseImplement.Models Title = Title, Address = Address, Director = Director, - ProductionId= ProductionId, + ProductionId = ProductionId, UserId = UserId, WorkerWorkshops = WorkerWorkshops }; @@ -78,7 +78,6 @@ namespace DatabaseImplement.Models context.SaveChanges(); foreach (var upWorker in workshopWorkers) { - upWorker.Count = model.WorkshopWorker[upWorker.WorkerId].Item2; model.WorkshopWorker.Remove(upWorker.WorkerId); } context.SaveChanges(); @@ -89,8 +88,7 @@ namespace DatabaseImplement.Models context.WorkerWorkshops.Add(new WorkerWorkshop { Workshop = workshop, - Worker = context.Workers.First(x => x.Id == dp.Key), - Count = dp.Value.Item2 + Worker = context.Workers.First(x => x.Id == dp.Key) }); context.SaveChanges(); }