update bd

This commit is contained in:
Дмитрий Блохин 2024-04-27 22:13:33 +04:00
parent a87ad2e062
commit e4533c4b03
13 changed files with 384 additions and 57 deletions

View File

@ -11,15 +11,15 @@ namespace BusinessLogic.BusinessLogic
{
private readonly ILogger _logger;
private readonly IMachineStorage _machineStorage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage)
public MachineLogic(ILogger<MachineLogic> logger, IMachineStorage machineStorage)
{
_logger = logger;
_productStorage = productStorage;
_machineStorage = machineStorage;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
public List<MachineViewModel>? 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;

View File

@ -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<WorkshopLogic> logger, IWorkshopStorage machineStorage)
{
_logger = logger;
_machineStorage = machineStorage;
}
public List<WorkshopViewModel>? 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;
}
}
}

View File

@ -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<int, (IWorkerModel, int)> MachineWorker { get; set; } = new();
public Dictionary<int, IWorkerModel> MachineWorker { get; set; } = new();
}
}

View File

@ -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<int, IWorkerModel> WorkshopWorker { get; set; } = new();
}
}

View File

@ -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<int, (IWorkerModel, int)>? WorkerMachines { get; set; }
public Dictionary<int, IWorkerModel>? WorkerMachines { get; set; }
}
}

View File

@ -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<int, (IWorkerModel, int)>? WorkerWorkshops { get; set; }
public int? ProductionId { get; set; }
public Dictionary<int, IWorkerModel>? WorkerWorkshops { get; set; }
}
}

View File

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

View File

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

View File

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

View File

@ -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<int, (IWorkerModel, int)>? _workerMachines = null;
private Dictionary<int, IWorkerModel>? _workerMachines = null;
[NotMapped]
public Dictionary<int, (IWorkerModel, int)>? WorkerMachines
public Dictionary<int, IWorkerModel>? 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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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<int, (IWorkerModel, int)>? _workerWorkshops = null;
public int? ProductionId { get; set; }
public virtual Production? Production { get; set; }
private Dictionary<int, IWorkerModel>? _workerWorkshops = null;
[NotMapped]
public Dictionary<int, (IWorkerModel, int)>? WorkerWorkshops
public Dictionary<int, IWorkerModel>? 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<WorkerWorkshop> 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
{
@ -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();
}