+ DataBaseImplements

This commit is contained in:
spacyboy 2024-04-28 21:13:12 +04:00
parent b1161502fa
commit 8b3f6b839e
17 changed files with 1010 additions and 18 deletions

View File

@ -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<PreSaleWorkModel>? ReadList(PreSaleWorkSearchModel? model)
public List<PreSaleWorkViewModel>? ReadList(PreSaleWorkSearchModel? model)
{
_logger.LogInformation("ReadList. PreSaleWorkType:{PreSaleWorkType}.Id:{ Id}", model?.PreSaleWorkType, model?.Id);

View File

@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\CarCenterContracts\CarCenterContracts.csproj" />
<ProjectReference Include="..\CarCenterDataBaseImplement\CarCenterDataBaseImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -7,8 +7,8 @@ namespace CarCenterContracts.BusinessLogicsContracts
{
public interface IPreSaleWorkLogic
{
List<PreSaleWorkModel>? ReadList(PreSaleWorkSearchModel? model);
PreSaleWorkModel? ReadElement(PreSaleWorkSearchModel model);
List<PreSaleWorkViewModel>? ReadList(PreSaleWorkSearchModel? model);
PreSaleWorkViewModel? ReadElement(PreSaleWorkSearchModel model);
bool AddSaleToPreSaleWork(PreSaleWorkSearchModel model, ISaleModel Sale);
bool Create(PreSaleWorkBindingModel model);
bool Update(PreSaleWorkBindingModel model);

View File

@ -6,16 +6,16 @@ namespace CarCenterContracts.StoragesContracts
{
public interface IPreSaleWorkStorage
{
List<PreSaleWorkModel> GetFullList();
List<PreSaleWorkViewModel> GetFullList();
List<PreSaleWorkModel> GetFilteredList(PreSaleWorkSearchModel model);
List<PreSaleWorkViewModel> 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);
}
}

View File

@ -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<int, ISaleModel> PreSaleWorkSale { get; set; } = new();
public Dictionary<int, ICarModel> PreSaleWorkCars { get; set; } = new();
public PreSaleWorkModel() { }
public PreSaleWorkViewModel() { }
[JsonConstructor]
public PreSaleWorkModel(Dictionary<int, SaleViewModel> PreSaleWorkSale, Dictionary<int, CarViewModel> PreSaleWorkCars)
public PreSaleWorkViewModel(Dictionary<int, SaleViewModel> PreSaleWorkSale, Dictionary<int, CarViewModel> 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);

View File

@ -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<Employee> Employees { set; get; }
public virtual DbSet<Equipment> Equipments { set; get; }
public virtual DbSet<PreSaleWork> PreSaleWorks { set; get; }
public virtual DbSet<Inspection> Inspections { set; get; }
public virtual DbSet<Sale> Sales { set; get; }
public virtual DbSet<Car> Cars { set; get; }
public virtual DbSet<Manager> Managers { set; get; }
public virtual DbSet<Administrator> Administrators { set; get; }
public virtual DbSet<EmployeeSale> EmployeeSales { set; get; }
public virtual DbSet<EquipmentCar> EquipmentCars { set; get; }
public virtual DbSet<PreSaleWorkSale> PreSaleWorkSales { set; get; }
public virtual DbSet<InspectionCar> InspectionCars { set; get; }
}
}

View File

@ -6,14 +6,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CarCenterContracts\CarCenterContracts.csproj" />
<ProjectReference Include="..\CarCenterDataModels\CarCenterDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
<Folder Include="Models\" />
</ItemGroup>
</Project>

View File

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

View File

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

View File

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

View File

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

View File

@ -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<int, ISaleModel> _employeeSale = null;
[NotMapped]
public Dictionary<int, ISaleModel> 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<Inspection> Inspections { get; set; } = new();
[ForeignKey("EmployeeId")]
public virtual List<EmployeeSale> 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;
}
}
}

View File

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

View File

@ -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<Employee> Employees { get; set; } = new();
[ForeignKey("ManagerId")]
public virtual List<PreSaleWork> PreSaleWorks { get; set; } = new();
[ForeignKey("ManagerId")]
public virtual List<Sale> 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
};
}
}

View File

@ -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<int, ISaleModel> _PreSaleWorkSale = null;
[NotMapped]
public Dictionary<int, ISaleModel> 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<Car> Cars { get; set; } = new();
[ForeignKey("PreSaleWorkId")]
public virtual List<PreSaleWorkSale> 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;
}
}
}

View File

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

View File

@ -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> PreSaleWorkSale { get; set; } = new();
[ForeignKey("SaleId")]
public virtual List<EmployeeSale> 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
};
}
}