From 56faadb0adf74e85e5d041a3225a65dd75fe5e62 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 20 Mar 2024 10:14:29 +0400 Subject: [PATCH] Database/Implements --- .../Implements/ComponentStorage.cs | 81 +++++++++++++ .../Implements/OrderStorage.cs | 94 +++++++++++++++ .../Implements/RepairStorage.cs | 109 ++++++++++++++++++ .../Models/Repair.cs | 11 +- 4 files changed, 290 insertions(+), 5 deletions(-) create mode 100644 AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs diff --git a/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..3436c8a --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,81 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + return Context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ComponentName)) + return new(); + + using var Context = new AutoWorkshopDatabase(); + return Context.Components + .Where(x => x.ComponentName.Contains(Model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ComponentName) && !Model.Id.HasValue) + return null; + + using var Context = new AutoWorkshopDatabase(); + return Context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.ComponentName) && x.ComponentName == Model.ComponentName) || + (Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel Model) + { + var NewComponent = Component.Create(Model); + + if (NewComponent == null) + return null; + + using var Context = new AutoWorkshopDatabase(); + Context.Components.Add(NewComponent); + Context.SaveChanges(); + + return NewComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var Component = Context.Components.FirstOrDefault(x => x.Id == Model.Id); + + if (Component == null) + return null; + + Component.Update(Model); + Context.SaveChanges(); + + return Component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Component = Context.Components.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Component == null) + return null; + + Context.Components.Remove(Component); + Context.SaveChanges(); + return Component.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..fb14da0 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Where(x => x.Id == Model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => x.Id == Model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + if (Model == null) + return null; + + var NewOrder = Order.Create(Context, Model); + if (NewOrder == null) + return null; + + Context.Orders.Add(NewOrder); + Context.SaveChanges(); + + return NewOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id); + if (Order == null) + return null; + + Order.Update(Model); + Context.SaveChanges(); + + return Order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Order == null) + return null; + + Context.Orders.Remove(Order); + Context.SaveChanges(); + + return Order.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..205e538 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,109 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName)) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.RepairName.Contains(Model.RepairName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RepairViewModel? GetElement(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName) && !Model.Id.HasValue) + return null; + + using var Context = new AutoWorkshopDatabase(); + + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.RepairName) && x.RepairName == Model.RepairName) || + (Model.Id.HasValue && x.Id == Model.Id)) + ?.GetViewModel; + } + + public RepairViewModel? Insert(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var NewRepair = Repair.Create(Context, Model); + if (NewRepair == null) + return null; + + Context.Repairs.Add(NewRepair); + Context.SaveChanges(); + + return NewRepair.GetViewModel; + } + + public RepairViewModel? Update(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + using var Transaction = Context.Database.BeginTransaction(); + try + { + var Repair = Context.Repairs.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Repair == null) + return null; + + Repair.Update(Model); + Context.SaveChanges(); + Repair.UpdateComponents(Context, Model); + + Transaction.Commit(); + return Repair.GetViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + public RepairViewModel? Delete(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var Repair = Context.Repairs + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == Model.Id); + + if (Repair == null) + return null; + + Context.Repairs.Remove(Repair); + Context.SaveChanges(); + return Repair.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Repair.cs b/AutoWorkshopDatabaseImplement/Models/Repair.cs index 1d614a9..86fad65 100644 --- a/AutoWorkshopDatabaseImplement/Models/Repair.cs +++ b/AutoWorkshopDatabaseImplement/Models/Repair.cs @@ -16,18 +16,19 @@ namespace AutoWorkshopDatabaseImplement.Models [Required] public double Price { get; set; } - private Dictionary? _RepairComponents = null; + private Dictionary? _repairComponents = null; [NotMapped] public Dictionary RepairComponents { get { - if (_RepairComponents == null) + if (_repairComponents == null) { - _RepairComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + _repairComponents = Components.ToDictionary(RepComp => RepComp.ComponentId, RepComp => + (RepComp.Component as IComponentModel, RepComp.Count)); } - return _RepairComponents; + return _repairComponents; } } @@ -98,7 +99,7 @@ namespace AutoWorkshopDatabaseImplement.Models Context.SaveChanges(); } - _RepairComponents = null; + _repairComponents = null; } } }