From 95973b888b54c164c4c7bf5cfb258a821b8e007b Mon Sep 17 00:00:00 2001 From: Yourdax Date: Tue, 30 Apr 2024 20:39:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB=20databaseimplements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/ComponentSearchModel.cs | 1 + .../Implements/ComponentStorage.cs | 85 ++++++++++++++ .../Implements/DrinkStorage.cs | 106 ++++++++++++++++++ .../Implements/OrderStorage.cs | 1 - .../Implements/ProductStorage.cs | 106 ++++++++++++++++++ .../Models/Component.cs | 2 +- 6 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs create mode 100644 DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs diff --git a/DiningRoom/DiningRoomContracts/SearchModels/ComponentSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/ComponentSearchModel.cs index 2404884..0701459 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/ComponentSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/ComponentSearchModel.cs @@ -5,5 +5,6 @@ public int? Id { get; set; } public int? UserId { get; set; } + public string ComponentName { get; set; } } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..573c2b0 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; + +namespace DiningRoomDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + // + public List GetFullList() + { + using var context = new DiningRoomDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + // + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new DiningRoomDatabase(); + 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 DiningRoomDatabase(); + 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 DiningRoomDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new DiningRoomDatabase(); + 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 DiningRoomDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs new file mode 100644 index 0000000..b8cc9b0 --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/DrinkStorage.cs @@ -0,0 +1,106 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace DiningRoomDatabaseImplement.Implements +{ + public class DrinkStorage : IDrinkStorage + { + public List GetFullList() + { + using var context = new DiningRoomDatabase(); + return context.Drinks + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(DrinkSearchModel model) + { + if (string.IsNullOrEmpty(model.DrinkName)) + { + return new(); + } + using var context = new DiningRoomDatabase(); + return context.Drinks + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.DrinkName.Contains(model.DrinkName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public DrinkViewModel? GetElement(DrinkSearchModel model) + { + if (string.IsNullOrEmpty(model.DrinkName) && !model.Id.HasValue) + { + return null; + } + using var context = new DiningRoomDatabase(); + return context.Drinks + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrinkName) && x.DrinkName == model.DrinkName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public DrinkViewModel? Insert(DrinkBindingModel model) + { + using var context = new DiningRoomDatabase(); + var newDrink = Drink.Create(context, model); + if (newDrink == null) + { + return null; + } + context.Drinks.Add(newDrink); + context.SaveChanges(); + return newDrink.GetViewModel; + } + + public DrinkViewModel? Update(DrinkBindingModel model) + { + using var context = new DiningRoomDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Drink = context.Drinks.FirstOrDefault(rec => rec.Id == model.Id); + if (Drink == null) + { + return null; + } + Drink.Update(model); + context.SaveChanges(); + Drink.UpdateComponents(context, model); + transaction.Commit(); + return Drink.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public DrinkViewModel? Delete(DrinkBindingModel model) + { + using var context = new DiningRoomDatabase(); + var element = context.Drinks + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Drinks.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs index 74ad27c..763985d 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs @@ -13,7 +13,6 @@ using System.Threading.Tasks; namespace DiningRoomDatabaseImplement.Implements { - //!!!ПОДОБИЕ component public class OrderStorage : IOrderStorage { public OrderViewModel? Delete(OrderBindingModel model) diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..fda76ec --- /dev/null +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,106 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using DiningRoomDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace DiningRoomDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new DiningRoomDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName)) + { + return new(); + } + using var context = new DiningRoomDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ProductName.Contains(model.ProductName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue) + { + return null; + } + using var context = new DiningRoomDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new DiningRoomDatabase(); + 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 DiningRoomDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Product = context.Products.FirstOrDefault(rec => rec.Id == model.Id); + if (Product == null) + { + return null; + } + Product.Update(model); + context.SaveChanges(); + Product.UpdateComponents(context, model); + transaction.Commit(); + return Product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new DiningRoomDatabase(); + var element = context.Products + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs index df3fcb8..b1ddf71 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs @@ -44,7 +44,7 @@ namespace DiningRoomDatabaseImplement.Models Unit = Model.Unit; } - public ComponentViewModel ViewModel => new() + public ComponentViewModel GetViewModel => new() { Id = Id, UserId = UserId,