From 2f7003d3b7d3f6eb7a93d394bb0a34ac7bc43605 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:42:42 +0400 Subject: [PATCH] productStorage --- .../Implements/ComponentStorage.cs | 29 +++---- .../Implements/ProductStorage.cs | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5041064..5bb8224 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Select(x => x.GetViewModel) + .Select(c => c.GetViewModel) .ToList(); } @@ -20,12 +20,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { if (string.IsNullOrEmpty(model.Name)) { - return new (); + return new(); } using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Where(x => x.Name.Contains(model.Name)) - .Select(x => x.GetViewModel) + .Where(c => c.Name.Contains(model.Name)) + .Select(c => c.GetViewModel) .ToList(); } @@ -37,10 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } 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)) + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) ?.GetViewModel; } @@ -59,7 +58,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + var component = context.Components.FirstOrDefault(c => c.Id == model.Id); if (component == null) { return null; @@ -72,14 +71,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Delete(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + var element = context.Components.FirstOrDefault(c => c.Id == model.Id); + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..a5face0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.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 ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Where(p => p.Name.Contains(model.Name)) + .Select(p => p.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) + ?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return product.GetViewModel; + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Products.Remove(product); + context.SaveChanges(); + return product.GetViewModel; + } + } +}