From 19d31583425c4b143161fc9ae38a9f663919467c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:27:11 +0400 Subject: [PATCH] componentStorage --- .../Implements/ComponentStorage.cs | 85 +++++++++++++++++++ .../Models/Build.cs | 10 +++ .../Models/StoreKeeper.cs | 2 - 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5041064 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new (); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) + && x.Name == model.Name) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComponent = Component.Create(context, model); + if (newComponent == null) + { + return null; + } + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + 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 ComputerHardwareStoreDBContext(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index db68236..1675e6a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -67,6 +67,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models }; } + public void Update(BuildBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + } + public BuildViewModel GetViewModel() => new() { Id = Id, diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs index 2143e5b..1fd691b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -15,14 +15,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Login { get; private set; } = string.Empty; [Required] - public string Password { get; private set; } = string.Empty; [ForeignKey("StoreKeeperId")] public virtual List Products { get; set; } = new(); [ForeignKey("StoreKeeperId")] public virtual List Components { get; set; } = new(); - public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) { if (model == null)