я тучи разгоню, РУКААААМИ

This commit is contained in:
Дмитрий Блохин 2024-04-27 20:23:32 +04:00
parent a111f8e999
commit 605be259cc
12 changed files with 608 additions and 30 deletions

View File

@ -0,0 +1,112 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BusinessLogic.BusinessLogic
{
public class GuarantorLogic : IGuarantorLogic
{
private readonly ILogger _logger;
private readonly IGuarantorStorage _storage;
public GuarantorLogic(ILogger<GuarantorLogic> logger, IGuarantorStorage storage)
{
_logger = logger;
_storage = storage;
}
public List<GuarantorViewModel>? ReadList(GuarantorSearchModel? model)
{
_logger.LogInformation("ReadList. Login:{Login}. Id:{Id}", model!.Login, model.Id);
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null");
return null;
}
return list;
}
public GuarantorViewModel? ReadElement(GuarantorSearchModel? model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login:{Login}. Id:{Id}.", model.Login, model.Id);
var elem = _storage.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(GuarantorBindingModel? model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина пользователя", nameof(model));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты пользователя", nameof(model));
}
var elem = _storage.GetElement(new GuarantorSearchModel
{
Login = model.Login
});
if (elem != null && model.Id != elem.Id)
{
throw new InvalidOperationException("Такой логин уже используется в системе");
}
}
public bool Create(GuarantorBindingModel? model)
{
CheckModel(model);
if (_storage.Insert(model!) == null)
{
_logger.LogWarning("Insert error");
return false;
}
return true;
}
public bool Update(GuarantorBindingModel? model)
{
CheckModel(model);
if (_storage.Update(model!) == null)
{
_logger.LogWarning("Update error");
return false;
}
return true;
}
public bool Delete(GuarantorBindingModel? model)
{
CheckModel(model, false);
_logger.LogInformation("Delete Guarantor. Id:{Id}", model!.Id);
if (_storage.Delete(model!) == null)
{
_logger.LogWarning("Delete error");
return false;
}
return true;
}
}
}

View File

@ -0,0 +1,107 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BusinessLogic.BusinessLogic
{
public class MachineLogic : IMachineLogic
{
private readonly ILogger _logger;
private readonly IMachineStorage _machineStorage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage)
{
_logger = logger;
_productStorage = productStorage;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? 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);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogWarning("ReadList. Count:{Count}", list.Count);
return list;
}
public ProductViewModel? ReadElement(ProductSearchModel? 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);
if (elem == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", elem.Id);
return elem;
}
private void CheckModel(ProductBindingModel? model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.Name));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Cost));
}
_logger.LogInformation("Product. ProductName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id);
var elem = _productStorage.GetElement(new ProductSearchModel
{
Name = model.Name
});
if (elem != null && elem.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже существует");
}
}
public bool Create(ProductBindingModel? model)
{
CheckModel(model);
if (_productStorage.Insert(model!) == null)
{
_logger.LogWarning("Insert error");
return false;
}
return true;
}
public bool Update(ProductBindingModel? model)
{
CheckModel(model);
if (_productStorage.Update(model!) == null)
{
_logger.LogWarning("Update error");
return false;
}
return true;
}
public bool Delete(ProductBindingModel? model)
{
CheckModel(model, false);
_logger.LogInformation("Delete Id:{Id}", model!.Id);
if (_productStorage.Delete(model!) == null)
{
_logger.LogWarning("Delete error");
return false;
}
return true;
}
}
}

View File

@ -6,10 +6,10 @@ namespace Contracts.BusinessLogicsContracts
{ {
public interface IGuarantorLogic public interface IGuarantorLogic
{ {
List<ImplementerViewModel>? ReadList(ImplementerSearchModel? model); List<GuarantorViewModel>? ReadList(GuarantorSearchModel? model);
ImplementerViewModel? ReadElement(ImplementerSearchModel? model); GuarantorViewModel? ReadElement(GuarantorSearchModel? model);
bool Create(ImplementerBindingModel? model); bool Create(GuarantorBindingModel? model);
bool Update(ImplementerBindingModel? model); bool Update(GuarantorBindingModel? model);
bool Delete(ImplementerBindingModel? model); bool Delete(GuarantorBindingModel? model);
} }
} }

View File

@ -12,5 +12,6 @@ namespace Contracts.ViewModels
public string Country { get; set; } = string.Empty; public string Country { get; set; } = string.Empty;
public int UserId { get; set; } public int UserId { get; set; }
public int ProductId { get; set; } public int ProductId { get; set; }
public Dictionary<int, (IWorkerModel, int)>? WorkerMachines { get; set; }
} }
} }

View File

@ -14,5 +14,6 @@ namespace Contracts.ViewModels
public string Director { get; set; } = string.Empty; public string Director { get; set; } = string.Empty;
public int UserId { get; set; } public int UserId { get; set; }
public int ProductionId { get; set; } public int ProductionId { get; set; }
public Dictionary<int, (IWorkerModel, int)>? WorkerWorkshops { get; set; }
} }
} }

View File

@ -0,0 +1,72 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StoragesContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
namespace DatabaseImplement.Implements
{
public class GuarantorStorage : IGuarantorStorage
{
public GuarantorViewModel? Delete(GuarantorBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newGuarantor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
if (newGuarantor == null)
return null;
context.Guarantors.Remove(newGuarantor);
context.SaveChanges();
return newGuarantor.GetViewModel;
}
public GuarantorViewModel? GetElement(GuarantorSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; }
using var context = new FactoryGoWorkDatabase();
return context.Guarantors.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ;
}
public List<GuarantorViewModel> GetFilteredList(GuarantorSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login))
return new();
using var context = new FactoryGoWorkDatabase();
if (model.Id.HasValue)
{
return context.Guarantors.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
else
{
return context.Guarantors.Where(x => x.Login.Equals(model.Login)).Select(x => x.GetViewModel).ToList();
}
}
public List<GuarantorViewModel> GetFullList()
{
using var context = new FactoryGoWorkDatabase();
return context.Guarantors.Select(x => x.GetViewModel).ToList();
}
public GuarantorViewModel? Insert(GuarantorBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newGuarantor = Guarantor.Create(model);
if (newGuarantor == null)
return null;
context.Guarantors.Add(newGuarantor);
context.SaveChanges();
return newGuarantor.GetViewModel;
}
public GuarantorViewModel? Update(GuarantorBindingModel model)
{
using var context = new FactoryGoWorkDatabase();
var newGuarantor = context.Guarantors.FirstOrDefault(x => x.Id == model.Id);
if (newGuarantor == null)
return null;
newGuarantor.Update(model);
context.SaveChanges();
return newGuarantor.GetViewModel;
}
}
}

View File

@ -0,0 +1,70 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace DatabaseImplement.Models
{
public class Guarantor : IGuarantorModel
{
public int Id { get; set; }
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[ForeignKey("UserId")]
public virtual List<Machine> Machines { get; set; } = new();
[ForeignKey("UserId")]
public virtual List<Worker> Workers { get; set; } = new();
[ForeignKey("UserId")]
public virtual List<Workshop> Workshops { get; set; } = new();
public static Guarantor? Create(GuarantorBindingModel model)
{
if (model == null)
{
return null;
}
return new Guarantor
{
Id = model.Id,
Email = model.Email,
Name = model.Name,
Login = model.Login,
Password = model.Password
};
}
public static Guarantor Create(GuarantorViewModel model)
{
return new Guarantor
{
Id = model.Id,
Email = model.Email,
Name = model.Name,
Login = model.Login,
Password = model.Password
};
}
public void Update(GuarantorBindingModel model)
{
if (model == null)
return;
Email = model.Email;
Name = model.Name;
Password = model.Password;
}
public GuarantorViewModel GetViewModel => new()
{
Id = Id,
Email = Email,
Name = Name,
Login = Login,
Password = Password
};
}
}

View File

@ -24,14 +24,24 @@ namespace DatabaseImplement.Models
[Required] [Required]
public int ProductId { get; private set; } public int ProductId { get; private set; }
public virtual Product Product { get; set; } public virtual Product Product { get; set; }
private Dictionary<int, (IWorkerModel, int)>? _workerMachines = null;
[NotMapped]
public Dictionary<int, (IWorkerModel, int)>? WorkerMachines
{
get
{
if (_workerMachines == null)
{
_workerMachines = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel, recDp.Count));
}
return _workerMachines;
}
}
[ForeignKey("WorkerId")] [ForeignKey("WorkerId")]
public virtual List<WorkerMachine> WorkerMachines { get; set; } = new(); public virtual List<WorkerMachine> Workers { get; set; } = new();
public virtual Guarantor Guarantor { get; set; }
public static Machine? Create(MachineBindingModel model, FactoryGoWorkDatabase context) public static Machine? Create(MachineBindingModel model, FactoryGoWorkDatabase context)
{ {
if (model == null)
{
return null;
}
return new Machine() return new Machine()
{ {
Id = model.Id, Id = model.Id,
@ -39,23 +49,15 @@ namespace DatabaseImplement.Models
Country = model.Country, Country = model.Country,
ProductId = model.ProductId, ProductId = model.ProductId,
Product = context.Products.FirstOrDefault(x => x.Id == model.ProductId)!, Product = context.Products.FirstOrDefault(x => x.Id == model.ProductId)!,
UserId = model.UserId UserId = model.UserId,
}; Workers = model.MachineWorker.Select(x => new WorkerMachine
}
public static Machine Create(MachineViewModel model)
{ {
return new Machine Worker = context.Workers.First(y => y.Id == x.Key),
{ }).ToList(),
Id = model.Id,
Title = model.Title,
Country = model.Country,
UserId = model.UserId
}; };
} }
public void Update(MachineBindingModel model) public void Update(MachineBindingModel model)
{ {
if (model == null)
return;
Title = model.Title; Title = model.Title;
Country = model.Country; Country = model.Country;
} }
@ -65,7 +67,35 @@ namespace DatabaseImplement.Models
Title = Title, Title = Title,
Country = Country, Country = Country,
UserId = UserId, UserId = UserId,
ProductId = ProductId ProductId = ProductId,
WorkerMachines = WorkerMachines
}; };
public void UpdateWorkers(FactoryGoWorkDatabase context, MachineBindingModel model)
{
var machineWorkers = context.WorkerMachines.Where(rec => rec.MachineId == model.Id).ToList();
if (machineWorkers != null && machineWorkers.Count > 0)
{
context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => !model.MachineWorker.ContainsKey(rec.WorkerId)));
context.SaveChanges();
foreach (var upWorker in machineWorkers)
{
upWorker.Count = model.MachineWorker[upWorker.WorkerId].Item2;
model.MachineWorker.Remove(upWorker.WorkerId);
}
context.SaveChanges();
}
var machine = context.Machines.First(x => x.Id == model.Id);
foreach (var dp in model.MachineWorker)
{
context.WorkerMachines.Add(new WorkerMachine
{
Machine = machine,
Worker = context.Workers.First(x => x.Id == dp.Key),
Count = dp.Value.Item2
});
context.SaveChanges();
}
_workerMachines = null;
}
} }
} }

View File

@ -0,0 +1,73 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DatabaseImplement.Models
{
public class Worker : IWorkerModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime Birthday { get; set; }
[Required]
public string Specialization { get; set; } = string.Empty;
[Required]
public double Salary { get; set; }
[Required]
public int UserId { get; set; }
[ForeignKey("WorkerId")]
public virtual List<WorkerMachine> WorkerMachines { get; set; } = new();
[ForeignKey("WorkerId")]
public virtual List<WorkerWorkshop> WorkerWorkshops { get; set; } = new();
public virtual Guarantor Guarantor { get; set; }
public static Worker? Create(WorkerBindingModel model)
{
if (model == null)
{
return null;
}
return new Worker
{
Id = model.Id,
Name = model.Name,
Birthday = model.Birthday,
Specialization = model.Specialization,
Salary = model.Salary,
UserId = model.UserId
};
}
public static Worker Create(WorkerViewModel model)
{
return new Worker
{
Id = model.Id,
Name = model.Name,
Birthday = model.Birthday,
Specialization = model.Specialization,
Salary = model.Salary,
UserId = model.UserId
};
}
public void Update(WorkerBindingModel model)
{
if (model == null)
return;
Name = model.Name;
Specialization = model.Specialization;
Salary = model.Salary;
}
public WorkerViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Birthday = Birthday,
Specialization = Specialization,
Salary = Salary,
UserId = UserId
};
}
}

View File

@ -1,9 +1,4 @@
using System; using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models namespace DatabaseImplement.Models
{ {

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace DatabaseImplement.Models
{
public class WorkerWorkshop
{
public int Id { get; set; }
[Required]
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

@ -0,0 +1,100 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace DatabaseImplement.Models
{
public class Workshop : IWorkshopModel
{
public int Id { get; set; }
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public string Director { get; set; } = string.Empty;
[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;
[NotMapped]
public Dictionary<int, (IWorkerModel, int)>? WorkerWorkshops
{
get
{
if(_workerWorkshops == null)
{
_workerWorkshops = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel, recDp.Count));
}
return _workerWorkshops;
}
}
[ForeignKey("WorkshopId")]
public List<WorkerWorkshop> Workers { get; set; } = new();
public virtual Guarantor Guarantor { get; set; }
public static Workshop Create(FactoryGoWorkDatabase context, WorkshopBindingModel model)
{
return new Workshop()
{
Id = model.Id,
Title = model.Title,
Address = model.Address,
Director = model.Director,
ProductionId = model.ProductionId,
Production = context.Productions.FirstOrDefault(x => x.Id == model.ProductionId)!,
UserId = model.UserId,
Workers = model.WorkshopWorker.Select(x => new WorkerWorkshop
{
Worker = context.Workers.First(y => y.Id == x.Key),
}).ToList(),
};
}
public void Update(WorkshopBindingModel model)
{
Title = model.Title;
Address = model.Address;
Director = model.Director;
}
public WorkshopViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
Address = Address,
Director = Director,
ProductionId= ProductionId,
UserId = UserId,
WorkerWorkshops = WorkerWorkshops
};
public void UpdateWorkers(FactoryGoWorkDatabase context, WorkshopBindingModel model)
{
var workshopWorkers = context.WorkerWorkshops.Where(rec => rec.WorkshopId == model.Id).ToList();
if (workshopWorkers != null && workshopWorkers.Count > 0)
{
context.WorkerWorkshops.RemoveRange(workshopWorkers.Where(rec => !model.WorkshopWorker.ContainsKey(rec.WorkerId)));
context.SaveChanges();
foreach (var upWorker in workshopWorkers)
{
upWorker.Count = model.WorkshopWorker[upWorker.WorkerId].Item2;
model.WorkshopWorker.Remove(upWorker.WorkerId);
}
context.SaveChanges();
}
var workshop = context.Workshops.First(x => x.Id == model.Id);
foreach (var dp in model.WorkshopWorker)
{
context.WorkerWorkshops.Add(new WorkerWorkshop
{
Workshop = workshop,
Worker = context.Workers.First(x => x.Id == dp.Key),
Count = dp.Value.Item2
});
context.SaveChanges();
}
_workerWorkshops = null;
}
}
}