diff --git a/CarCenter/CarCenterBusinessLogic/BusinessLogics/PreSaleWorkLogic.cs b/CarCenter/CarCenterBusinessLogic/BusinessLogics/PreSaleWorkLogic.cs index 65ed5e2..e4bc85b 100644 --- a/CarCenter/CarCenterBusinessLogic/BusinessLogics/PreSaleWorkLogic.cs +++ b/CarCenter/CarCenterBusinessLogic/BusinessLogics/PreSaleWorkLogic.cs @@ -86,7 +86,7 @@ namespace CarCenterBusinessLogic.BusinessLogics return true; } - public PreSaleWorkModel? ReadElement(PreSaleWorkSearchModel model) + public PreSaleWorkViewModel? ReadElement(PreSaleWorkSearchModel model) { if (model == null) { @@ -108,7 +108,7 @@ namespace CarCenterBusinessLogic.BusinessLogics return element; } - public List? ReadList(PreSaleWorkSearchModel? model) + public List? ReadList(PreSaleWorkSearchModel? model) { _logger.LogInformation("ReadList. PreSaleWorkType:{PreSaleWorkType}.Id:{ Id}", model?.PreSaleWorkType, model?.Id); diff --git a/CarCenter/CarCenterBusinessLogic/CarCenterBusinessLogic.csproj b/CarCenter/CarCenterBusinessLogic/CarCenterBusinessLogic.csproj index 0e458bd..5480315 100644 --- a/CarCenter/CarCenterBusinessLogic/CarCenterBusinessLogic.csproj +++ b/CarCenter/CarCenterBusinessLogic/CarCenterBusinessLogic.csproj @@ -12,6 +12,7 @@ + diff --git a/CarCenter/CarCenterContracts/BusinessLogicsContracts/IPreSaleWorkLogic.cs b/CarCenter/CarCenterContracts/BusinessLogicsContracts/IPreSaleWorkLogic.cs index e8b230b..c04c471 100644 --- a/CarCenter/CarCenterContracts/BusinessLogicsContracts/IPreSaleWorkLogic.cs +++ b/CarCenter/CarCenterContracts/BusinessLogicsContracts/IPreSaleWorkLogic.cs @@ -7,8 +7,8 @@ namespace CarCenterContracts.BusinessLogicsContracts { public interface IPreSaleWorkLogic { - List? ReadList(PreSaleWorkSearchModel? model); - PreSaleWorkModel? ReadElement(PreSaleWorkSearchModel model); + List? ReadList(PreSaleWorkSearchModel? model); + PreSaleWorkViewModel? ReadElement(PreSaleWorkSearchModel model); bool AddSaleToPreSaleWork(PreSaleWorkSearchModel model, ISaleModel Sale); bool Create(PreSaleWorkBindingModel model); bool Update(PreSaleWorkBindingModel model); diff --git a/CarCenter/CarCenterContracts/StoragesContracts/IPreSaleWorkStorage.cs b/CarCenter/CarCenterContracts/StoragesContracts/IPreSaleWorkStorage.cs index f36eee4..c43dba2 100644 --- a/CarCenter/CarCenterContracts/StoragesContracts/IPreSaleWorkStorage.cs +++ b/CarCenter/CarCenterContracts/StoragesContracts/IPreSaleWorkStorage.cs @@ -6,16 +6,16 @@ namespace CarCenterContracts.StoragesContracts { public interface IPreSaleWorkStorage { - List GetFullList(); + List GetFullList(); - List GetFilteredList(PreSaleWorkSearchModel model); + List GetFilteredList(PreSaleWorkSearchModel model); - PreSaleWorkModel? GetElement(PreSaleWorkSearchModel model); + PreSaleWorkViewModel? GetElement(PreSaleWorkSearchModel model); - PreSaleWorkModel? Insert(PreSaleWorkBindingModel model); + PreSaleWorkViewModel? Insert(PreSaleWorkBindingModel model); - PreSaleWorkModel? Update(PreSaleWorkBindingModel model); + PreSaleWorkViewModel? Update(PreSaleWorkBindingModel model); - PreSaleWorkModel? Delete(PreSaleWorkBindingModel model); + PreSaleWorkViewModel? Delete(PreSaleWorkBindingModel model); } } diff --git a/CarCenter/CarCenterContracts/ViewModels/PreSaleWorkModel.cs b/CarCenter/CarCenterContracts/ViewModels/PreSaleWorkViewModel.cs similarity index 80% rename from CarCenter/CarCenterContracts/ViewModels/PreSaleWorkModel.cs rename to CarCenter/CarCenterContracts/ViewModels/PreSaleWorkViewModel.cs index bceb89d..2c51e97 100644 --- a/CarCenter/CarCenterContracts/ViewModels/PreSaleWorkModel.cs +++ b/CarCenter/CarCenterContracts/ViewModels/PreSaleWorkViewModel.cs @@ -4,7 +4,7 @@ using Newtonsoft.Json; namespace CarCenterContracts.ViewModels { - public class PreSaleWorkModel : IPreSaleWorkModel + public class PreSaleWorkViewModel : IPreSaleWorkModel { [DisplayName("Тип предпродажной работы")] public string PreSaleWorkType { get; set; } = string.Empty; @@ -19,10 +19,10 @@ namespace CarCenterContracts.ViewModels public Dictionary PreSaleWorkSale { get; set; } = new(); public Dictionary PreSaleWorkCars { get; set; } = new(); - public PreSaleWorkModel() { } + public PreSaleWorkViewModel() { } [JsonConstructor] - public PreSaleWorkModel(Dictionary PreSaleWorkSale, Dictionary PreSaleWorkCars) + public PreSaleWorkViewModel(Dictionary PreSaleWorkSale, Dictionary PreSaleWorkCars) { this.PreSaleWorkSale = PreSaleWorkSale.ToDictionary(x => x.Key, x => x.Value as ISaleModel); this.PreSaleWorkCars = PreSaleWorkCars.ToDictionary(x => x.Key, x => x.Value as ICarModel); diff --git a/CarCenter/CarCenterDataBaseImplement/CarCenterDataBase.cs b/CarCenter/CarCenterDataBaseImplement/CarCenterDataBase.cs new file mode 100644 index 0000000..b640833 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/CarCenterDataBase.cs @@ -0,0 +1,30 @@ +using CarCenterDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDataBaseImplement +{ + public class CarCenterDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=CarCenterDataBaseFu;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Employees { set; get; } + public virtual DbSet Equipments { set; get; } + public virtual DbSet PreSaleWorks { set; get; } + public virtual DbSet Inspections { set; get; } + public virtual DbSet Sales { set; get; } + public virtual DbSet Cars { set; get; } + public virtual DbSet Managers { set; get; } + public virtual DbSet Administrators { set; get; } + public virtual DbSet EmployeeSales { set; get; } + public virtual DbSet EquipmentCars { set; get; } + public virtual DbSet PreSaleWorkSales { set; get; } + public virtual DbSet InspectionCars { set; get; } + + } +} \ No newline at end of file diff --git a/CarCenter/CarCenterDataBaseImplement/CarCenterDataBaseImplement.csproj b/CarCenter/CarCenterDataBaseImplement/CarCenterDataBaseImplement.csproj index 66e3a1c..d3ebafb 100644 --- a/CarCenter/CarCenterDataBaseImplement/CarCenterDataBaseImplement.csproj +++ b/CarCenter/CarCenterDataBaseImplement/CarCenterDataBaseImplement.csproj @@ -6,13 +6,13 @@ enable + + + + - - - - diff --git a/CarCenter/CarCenterDataBaseImplement/Implements/EmployeeStorage.cs b/CarCenter/CarCenterDataBaseImplement/Implements/EmployeeStorage.cs new file mode 100644 index 0000000..023bd85 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Implements/EmployeeStorage.cs @@ -0,0 +1,165 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDataBaseImplement.Implemets +{ + public class EmployeeStorage : IEmployeeStorage + { + public EmployeeViewModel? Delete(EmployeeBindingModel model) + { + using var context = new CarCenterDataBase(); + + var element = context.Employees + .Include(x => x.Sales) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Employees.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public EmployeeViewModel? GetElement(EmployeeSearchModel model) + { + if (string.IsNullOrEmpty(model.EmployeeFIO) && !model.Id.HasValue) + { + return null; + } + + using var context = new CarCenterDataBase(); + + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.EmployeeFIO) && x.EmployeeFIO == model.EmployeeFIO) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public List GetFilteredList(EmployeeSearchModel model) + { + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ManagerId.HasValue) + { + return new(); + } + using var context = new CarCenterDataBase(); + + if (model.DateFrom.HasValue) + { + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .Where(x => x.Specialization >= model.DateFrom && x.Specialization <= model.DateTo && x.ManagerId == model.ManagerId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ManagerId.HasValue) + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .Where(x => x.ManagerId == model.ManagerId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .Where(x => x.EmployeeFIO.Contains(model.EmployeeFIO)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarCenterDataBase(); + + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public EmployeeViewModel? Insert(EmployeeBindingModel model) + { + using var context = new CarCenterDataBase(); + var newEmployee = Employee.Create(context,model); + + if (newEmployee == null) + { + return null; + } + + context.Employees.Add(newEmployee); + context.SaveChanges(); + + return context.Employees + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Inspection) + .Include(x => x.Manager) + .FirstOrDefault(x => x.Id == newEmployee.Id) + ?.GetViewModel; + } + + + public EmployeeViewModel? Update(EmployeeBindingModel model) + { + using var context = new CarCenterDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.Employees.FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.EmployeeSales != null) + elem.UpdateSales(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Implements/ManagerStorage.cs b/CarCenter/CarCenterDataBaseImplement/Implements/ManagerStorage.cs new file mode 100644 index 0000000..6d09992 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Implements/ManagerStorage.cs @@ -0,0 +1,122 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDataBaseImplement.Implemets +{ + public class ManagerStorage : IManagerStorage + { + public ManagerViewModel? Delete(ManagerBindingModel model) + { + using var context = new CarCenterDataBase(); + + var element = context.Managers.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Managers.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public ManagerViewModel? GetElement(ManagerSearchModel model) + { + using var context = new CarCenterDataBase(); + + if (model.Id.HasValue) + return context.Managers + .Include(x => x.PreSaleWorks) + .Include(x => x.Sales) + .Include(x => x.Employees) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.ManagerEmail) && !string.IsNullOrEmpty(model.ManagerPassword)) + return context.Managers + .Include(x => x.PreSaleWorks) + .Include(x => x.Sales) + .Include(x => x.Employees) + .FirstOrDefault(x => x.ManagerEmail.Equals(model.ManagerEmail) && x.ManagerPassword.Equals(model.ManagerPassword))? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.ManagerEmail)) + return context.Managers + .Include(x => x.PreSaleWorks) + .Include(x => x.Sales) + .Include(x => x.Employees) + .FirstOrDefault(x => x.ManagerEmail.Equals(model.ManagerEmail))? + .GetViewModel; + + return null; + } + + public List GetFilteredList(ManagerSearchModel model) + { + if (string.IsNullOrEmpty(model.ManagerFIO)) + { + return new(); + } + + using var context = new CarCenterDataBase(); + + return context.Managers + .Include(x => x.PreSaleWorks) + .Include(x => x.Sales) + .Include(x => x.Employees) + .Where(x => x.ManagerLogin.Contains(model.ManagerLogin) && x.ManagerPassword == model.ManagerPassword) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarCenterDataBase(); + + return context.Managers + .Select(x => x.GetViewModel) + .ToList(); + } + + public ManagerViewModel? Insert(ManagerBindingModel model) + { + var newManager = Manager.Create(model); + + if (newManager == null) + { + return null; + } + + using var context = new CarCenterDataBase(); + + context.Managers.Add(newManager); + context.SaveChanges(); + + return newManager.GetViewModel; + } + + public ManagerViewModel? Update(ManagerBindingModel model) + { + using var context = new CarCenterDataBase(); + + var Manager = context.Managers + .FirstOrDefault(x => x.Id == model.Id); + + if (Manager == null) + { + return null; + } + + Manager.Update(model); + context.SaveChanges(); + + return Manager.GetViewModel; + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Implements/PreSaleWorkStorage.cs b/CarCenter/CarCenterDataBaseImplement/Implements/PreSaleWorkStorage.cs new file mode 100644 index 0000000..ac8beea --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Implements/PreSaleWorkStorage.cs @@ -0,0 +1,142 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDataBaseImplement.Implemets +{ + public class PreSaleWorkStorage : IPreSaleWorkStorage + { + public PreSaleWorkViewModel? Delete(PreSaleWorkBindingModel model) + { + using var context = new CarCenterDataBase(); + + var element = context.PreSaleWorks + .Include(x => x.Sales) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.PreSaleWorks.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public PreSaleWorkViewModel? GetElement(PreSaleWorkSearchModel model) + { + if (string.IsNullOrEmpty(model.PreSaleWorkType) && !model.Id.HasValue) + { + return null; + } + + using var context = new CarCenterDataBase(); + + return context.PreSaleWorks + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.Equipments) + .Include(x => x.Manager) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PreSaleWorkType) && x.PreSaleWorkType == model.PreSaleWorkType) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public List GetFilteredList(PreSaleWorkSearchModel model) + { + if (string.IsNullOrEmpty(model.PreSaleWorkType) && !model.ManagerId.HasValue) + { + return new(); + } + + using var context = new CarCenterDataBase(); + + if (model.ManagerId.HasValue) + { + return context.PreSaleWorks + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.Equipments) + .Include(x => x.Manager) + .Where(x => x.ManagerId == model.ManagerId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.PreSaleWorks + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.Equipments) + .Include(x => x.Manager) + .Where(x => x.PreSaleWorkType.Contains(model.PreSaleWorkType)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarCenterDataBase(); + return context.PreSaleWorks + .Include(x => x.Sales) + .ThenInclude(x => x.Sale) + .ThenInclude(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.Equipments) + .Include(x => x.Manager) + .Select(x => x.GetViewModel) + .ToList(); + } + + public PreSaleWorkViewModel? Insert(PreSaleWorkBindingModel model) + { + using var context = new CarCenterDataBase(); + var newPreSaleWork = PreSaleWork.Create(context,model); + + if (newPreSaleWork == null) + { + return null; + } + + context.PreSaleWorks.Add(newPreSaleWork); + context.SaveChanges(); + return newPreSaleWork.GetViewModel; + } + + public PreSaleWorkViewModel? Update(PreSaleWorkBindingModel model) + { + using var context = new CarCenterDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.PreSaleWorks.FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.PreSaleWorkSales != null) + elem.UpdateSales(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Implements/SaleStorage.cs b/CarCenter/CarCenterDataBaseImplement/Implements/SaleStorage.cs new file mode 100644 index 0000000..405da76 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Implements/SaleStorage.cs @@ -0,0 +1,129 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDataBaseImplement.Implemets +{ + public class SaleStorage : ISaleStorage + { + public SaleViewModel? Delete(SaleBindingModel model) + { + using var context = new CarCenterDataBase(); + + var element = context.Sales.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Sales.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public SaleViewModel? GetElement(SaleSearchModel model) + { + if (string.IsNullOrEmpty(model.SaleDate) && !model.Id.HasValue) + { + return null; + } + + using var context = new CarCenterDataBase(); + + return context.Sales + .Include(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Manager) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SaleDate) && x.SaleDate == model.SaleDate) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public List GetFilteredList(SaleSearchModel model) + { + if (string.IsNullOrEmpty(model.SaleDate) && !model.ManagerId.HasValue) + { + return new(); + } + + using var context = new CarCenterDataBase(); + + if (model.ManagerId.HasValue) + { + return context.Sales + .Include(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Manager) + .Where(x => x.ManagerId == model.ManagerId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Sales + .Include(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Manager) + .Where(x => x.SaleDate.Contains(model.SaleDate)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarCenterDataBase(); + + return context.Sales + .Include(x => x.EmployeeSales) + .ThenInclude(x => x.Employee) + .Include(x => x.PreSaleWorkSale) + .ThenInclude(x => x.PreSaleWork) + .Include(x => x.Manager) + .Select(x => x.GetViewModel) + .ToList(); + } + + public SaleViewModel? Insert(SaleBindingModel model) + { + using var context = new CarCenterDataBase(); + + var newSale = Sale.Create(model); + + if (newSale == null) + { + return null; + } + + context.Sales.Add(newSale); + context.SaveChanges(); + + return newSale.GetViewModel; + } + + public SaleViewModel? Update(SaleBindingModel model) + { + using var context = new CarCenterDataBase(); + + var Sale = context.Sales.FirstOrDefault(x => x.Id == model.Id); + + if (Sale == null) + { + return null; + } + + Sale.Update(model); + context.SaveChanges(); + + return Sale.GetViewModel; + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/Employee.cs b/CarCenter/CarCenterDataBaseImplement/Models/Employee.cs new file mode 100644 index 0000000..2bb2bd6 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/Employee.cs @@ -0,0 +1,107 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.ViewModels; +using CarCenterDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; + +namespace CarCenterDataBaseImplement.Models +{ + public class Employee : IEmployeeModel + { + [Required] + public string EmployeeFIO { get; set; } = string.Empty; + [Required] + public string Specialization { get; set; } = string.Empty; + + public int ManagerId { get; private set; } + + public int Id { get; private set; } + + public virtual Manager Manager { get; set; } + + private Dictionary _employeeSale = null; + + [NotMapped] + public Dictionary EmployeeSale + { + get + { + if (_employeeSale == null) + { + using var context = new CarCenterDataBase(); + _employeeSale = Sale + .ToDictionary(x => x.SaleId, x => (context.Sale + .FirstOrDefault(y => y.Id == x.SaleId)! as ISaleModel)); + } + return _employeeSale; + } + } + + [ForeignKey("EmployeeId")] + public virtual List Inspections { get; set; } = new(); + + [ForeignKey("EmployeeId")] + public virtual List Sale { get; set; } = new(); + + public static Employee Create(CarCenterDataBase context, EmployeeBindingModel model) + { + return new Employee() + { + Id = model.Id, + EmployeeFIO = model.EmployeeFIO, + Specialization = model.Specialization, + ManagerId=model.ManagerId, + Sale = model.EmployeeSale.Select(x => new EmployeeSale + { + Sale = context.Sale.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(EmployeeBindingModel model) + { + EmployeeFIO = model.EmployeeFIO; + Specialization = model.Specialization; + ManagerId = model.ManagerId; + } + + public EmployeeViewModel GetViewModel => new() + { + Id = Id, + EmployeeFIO = EmployeeFIO, + Specialization = Specialization, + ManagerId = ManagerId, + EmployeeSale = EmployeeSale + }; + + public void UpdateSale(CarCenterDataBase context, EmployeeBindingModel model) + { + var EmployeeSale = context.EmployeeSale.Where(rec => rec.EmployeeId == model.Id).ToList(); + + if (EmployeeSale != null && EmployeeSale.Any()) + { + context.EmployeeSale.RemoveRange(EmployeeSale.Where(rec => !model.EmployeeSale.ContainsKey(rec.SaleId))); + context.SaveChanges(); + + foreach (var updateSale in EmployeeSale) + { + model.EmployeeSale.Remove(updateSale.SaleId); + } + context.SaveChanges(); + } + + var Employee = context.Employees.First(x => x.Id == Id); + foreach (var cm in model.EmployeeSale) + { + context.EmployeeSale.Add(new EmployeeSale + { + Employee = Employee, + Sale = context.Sale.First(x => x.Id == cm.Key), + }); + context.SaveChanges(); + } + _employeeSale = null; + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/EmployeeSale.cs b/CarCenter/CarCenterDataBaseImplement/Models/EmployeeSale.cs new file mode 100644 index 0000000..0fde578 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/EmployeeSale.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace CarCenterDataBaseImplement.Models +{ + public class EmployeeSale + { + public int Id { get; set; } + + [Required] + public int SaleId { get; set; } + + [Required] + public int EmployeeId { get; set; } + + public virtual Employee Employee { get; set; } = new(); + public virtual Sale Sale { get; set; } = new(); + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/Manager.cs b/CarCenter/CarCenterDataBaseImplement/Models/Manager.cs new file mode 100644 index 0000000..1705725 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/Manager.cs @@ -0,0 +1,81 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.ViewModels; +using CarCenterDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarCenterDataBaseImplement.Models +{ + public class Manager : IManagerModel + { + [Required] + public string ManagerFIO { get; set; } = string.Empty; + [Required] + public string ManagerPassword { get; set; } = string.Empty; + [Required] + public string ManagerLogin { get; set; } = string.Empty; + [Required] + public string ManagerEmail { get; set; } = string.Empty; + [Required] + public string ManagerNumber { get; set; } = string.Empty; + + public int Id { get; private set; } + + [ForeignKey("ManagerId")] + public virtual List Employees { get; set; } = new(); + [ForeignKey("ManagerId")] + public virtual List PreSaleWorks { get; set; } = new(); + [ForeignKey("ManagerId")] + public virtual List Sale { get; set; } = new(); + + public static Manager? Create(ManagerBindingModel model) + { + if (model == null) + { + return null; + } + return new Manager() + { + Id = model.Id, + ManagerFIO = model.ManagerFIO, + ManagerEmail = model.ManagerEmail, + ManagerPassword = model.ManagerPassword, + ManagerLogin = model.ManagerLogin, + ManagerNumber = model.ManagerNumber + }; + } + public static Manager Create(ManagerViewModel model) + { + return new Manager + { + Id = model.Id, + ManagerFIO = model.ManagerFIO, + ManagerEmail = model.ManagerEmail, + ManagerPassword = model.ManagerPassword, + ManagerLogin= model.ManagerLogin, + ManagerNumber= model.ManagerNumber + }; + } + public void Update(ManagerBindingModel model) + { + if (model == null) + { + return; + } + ManagerFIO = model.ManagerFIO; + ManagerEmail = model.ManagerEmail; + ManagerPassword = model.ManagerPassword; + ManagerLogin = model.ManagerLogin; + ManagerNumber = model.ManagerNumber; + } + public ManagerViewModel GetViewModel => new() + { + Id = Id, + ManagerFIO = ManagerFIO, + ManagerEmail = ManagerEmail, + ManagerPassword = ManagerPassword, + ManagerNumber = ManagerNumber, + ManagerLogin = ManagerLogin + }; + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWork.cs b/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWork.cs new file mode 100644 index 0000000..6ae9b40 --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWork.cs @@ -0,0 +1,109 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.ViewModels; +using CarCenterDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; + +namespace CarCenterDataBaseImplement.Models +{ + public class PreSaleWork : IPreSaleWorkModel + { + [Required] + public string PreSaleWorkType { get; set; } = string.Empty; + + [Required] + public double PreSaleWorkPrice { get; set; } + + public int ManagerId { get; private set; } + + public int Id { get; private set; } + + public virtual Manager Manager { get; set; } + + private Dictionary _PreSaleWorkSale = null; + + [NotMapped] + public Dictionary PreSaleWorkSale + { + get + { + if (_PreSaleWorkSale == null) + { + using var context = new CarCenterDataBase(); + _PreSaleWorkSale = Sale + .ToDictionary(x => x.SaleId, x => (context.Sale + .FirstOrDefault(y => y.Id == x.SaleId)! as ISaleModel)); + } + return _PreSaleWorkSale; + } + } + + [ForeignKey("PreSaleWorkId")] + public virtual List Cars { get; set; } = new(); + + [ForeignKey("PreSaleWorkId")] + public virtual List Sale { get; set; } = new(); + + public static PreSaleWork Create(CarCenterDataBase context, PreSaleWorkBindingModel model) + { + return new PreSaleWork() + { + Id = model.Id, + PreSaleWorkType = model.PreSaleWorkType, + PreSaleWorkPrice = model.PreSaleWorkPrice, + ManagerId = model.ManagerId, + Sale = model.PreSaleWorkSale.Select(x => new PreSaleWorkSale + { + Sale = context.Sale.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(PreSaleWorkBindingModel model) + { + PreSaleWorkType = model.PreSaleWorkType; + PreSaleWorkPrice = model.PreSaleWorkPrice; + ManagerId = model.ManagerId; + } + + public PreSaleWorkViewModel GetViewModel => new() + { + Id = Id, + PreSaleWorkType = PreSaleWorkType, + PreSaleWorkPrice = PreSaleWorkPrice, + ManagerId=ManagerId, + PreSaleWorkSale = PreSaleWorkSale + }; + + public void UpdateSale(CarCenterDataBase context, PreSaleWorkBindingModel model) + { + var PreSaleWorkSale = context.PreSaleWorkSale.Where(rec => rec.PreSaleWorkId == model.Id).ToList(); + + if (PreSaleWorkSale != null && PreSaleWorkSale.Any()) + { + context.PreSaleWorkSale.RemoveRange(PreSaleWorkSale.Where(rec => !model.PreSaleWorkSale.ContainsKey(rec.SaleId))); + context.SaveChanges(); + + foreach (var updateSale in PreSaleWorkSale) + { + model.PreSaleWorkSale.Remove(updateSale.SaleId); + } + context.SaveChanges(); + } + + var PreSaleWork = context.PreSaleWorks.First(x => x.Id == Id); + + foreach (var cm in model.PreSaleWorkSale) + { + context.PreSaleWorkSale.Add(new PreSaleWorkSale + { + PreSaleWork = PreSaleWork, + Sale = context.Sale.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _PreSaleWorkSale = null; + } + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWorkSale.cs b/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWorkSale.cs new file mode 100644 index 0000000..a0e87bb --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/PreSaleWorkSale.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace CarCenterDataBaseImplement.Models +{ + public class PreSaleWorkSale + { + public int Id { get; set; } + + [Required] + public int SaleId { get; set; } + + [Required] + public int PreSaleWorkId { get; set; } + + public virtual PreSaleWork PreSaleWork { get; set; } = new(); + public virtual Sale Sale { get; set; } = new(); + } +} diff --git a/CarCenter/CarCenterDataBaseImplement/Models/Sale.cs b/CarCenter/CarCenterDataBaseImplement/Models/Sale.cs new file mode 100644 index 0000000..6ef25df --- /dev/null +++ b/CarCenter/CarCenterDataBaseImplement/Models/Sale.cs @@ -0,0 +1,71 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.ViewModels; +using CarCenterDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarCenterDataBaseImplement.Models +{ + public class Sale : ISaleModel + { + [Required] + public DateTime? SaleDate { get; set; } = DateTime.Now; + [Required] + public string SalePrice { get; set; } = string.Empty; + + public int ManagerId { get; private set; } + + public int Id { get; private set; } + + public virtual Manager Manager { get; set; } + + [ForeignKey("SaleId")] + public virtual List PreSaleWorkSale { get; set; } = new(); + + + [ForeignKey("SaleId")] + public virtual List EmployeeSale { get; set; } = new(); + + public static Sale? Create(SaleBindingModel model) + { + if (model == null) + { + return null; + } + return new Sale() + { + Id = model.Id, + SaleDate = model.SaleDate, + SalePrice = model.SalePrice, + ManagerId = model.ManagerId, + }; + } + public static Sale Create(SaleViewModel model) + { + return new Sale + { + Id = model.Id, + SaleDate = model.SaleDate, + SalePrice = model.SalePrice, + ManagerId=model.ManagerId, + }; + } + public void Update(SaleBindingModel model) + { + if (model == null) + { + return; + } + SaleDate = model.SaleDate; + SalePrice = model.SalePrice; + ManagerId = model.ManagerId; + } + public SaleViewModel GetViewModel => new() + { + Id = Id, + SaleDate = SaleDate, + SalePrice = SalePrice, + ManagerId=ManagerId + }; + } +}