From 1a8bfc323e557330ef75d6ed9091bd3074b46e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 20 Feb 2023 01:35:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B?= =?UTF-8?q?=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=89=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=81=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Program.cs | 4 +- .../ComponentStorage.cs | 80 +++++++++++++++++ .../ConfectioneryDatabase.cs | 1 + .../OrderStorage.cs | 74 +++++++++++++++ .../PastryStorage.cs | 89 +++++++++++++++++++ 5 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 ConfectioneryDatabaseImplement/ComponentStorage.cs create mode 100644 ConfectioneryDatabaseImplement/OrderStorage.cs create mode 100644 ConfectioneryDatabaseImplement/PastryStorage.cs diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index bd6722d..0ccb4c2 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,8 +1,8 @@ -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; +using ConfectioneryDatabaseImplement; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; -using ConfectioneryFileImplement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/ConfectioneryDatabaseImplement/ComponentStorage.cs b/ConfectioneryDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..495ecf7 --- /dev/null +++ b/ConfectioneryDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,80 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 66c820b..08095db 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -8,6 +8,7 @@ namespace ConfectioneryDatabaseImplement { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + // Перед работой необходимо установить SQL Express if (optionsBuilder.IsConfigured == false) { optionsBuilder.UseSqlServer(@" diff --git a/ConfectioneryDatabaseImplement/OrderStorage.cs b/ConfectioneryDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..4e87161 --- /dev/null +++ b/ConfectioneryDatabaseImplement/OrderStorage.cs @@ -0,0 +1,74 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + using var context = new ConfectioneryDatabase(); + if (!model.Id.HasValue) + { + return null; + } + return context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs new file mode 100644 index 0000000..84af032 --- /dev/null +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -0,0 +1,89 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries.FirstOrDefault + (x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Select(x => x.GetViewModel) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Select(x => x.GetViewModel) + .ToList(); + } + + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + newPastry = context.Pastries.Add(newPastry).Entity; + context.SaveChanges(); + model.Id = newPastry.Id; + newPastry.UpdateComponents(context, model); + return newPastry.GetViewModel; + } + + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + pastry.UpdateComponents(context, model); + context.Update(pastry); + context.SaveChanges(); + return pastry.GetViewModel; + } + } +}