diff --git a/.gitignore b/.gitignore index ca1c7a3..948f292 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,10 @@ # Mono auto generated files mono_crash.* +*.dll + +/SushiBar/ImplementationExtensions + # Build results [Dd]ebug/ [Dd]ebugPublic/ diff --git a/SushiBar/Program.cs b/SushiBar/Program.cs index bea7568..c0d5630 100644 --- a/SushiBar/Program.cs +++ b/SushiBar/Program.cs @@ -5,7 +5,7 @@ using SushiBarBusinessLogic; using SushiBarBusinessLogic.BusinessLogic; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarFileImplement.Implements; +using SushiBarDatabaseImplement.Implements; using SushiBarView; using SushiBarView.Shops; diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 8fb11cd..7188e03 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -3,18 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34525.116 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarView", "SushiBarView.csproj", "{E1025CC0-5650-4509-93C8-BF4FA2D506E0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarView", "SushiBarView.csproj", "{E1025CC0-5650-4509-93C8-BF4FA2D506E0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDataModels", "..\SushiBarDataModels\SushiBarDataModels.csproj", "{98946D8E-A8FC-40DC-9FC8-BCBBC6BD6688}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarDataModels", "..\SushiBarDataModels\SushiBarDataModels.csproj", "{98946D8E-A8FC-40DC-9FC8-BCBBC6BD6688}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "..\SushiBarContracts\SushiBarContracts.csproj", "{0C8B928F-ACD2-4196-8F2B-5190914911D8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarContracts", "..\SushiBarContracts\SushiBarContracts.csproj", "{0C8B928F-ACD2-4196-8F2B-5190914911D8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "..\SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{B4061C5A-A7C4-4F49-A1AC-083877203CD8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarBusinessLogic", "..\SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{B4061C5A-A7C4-4F49-A1AC-083877203CD8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarListImplements", "..\SushiBarListImplements\SushiBarListImplements.csproj", "{BAA05DBA-C77F-4CF6-9998-4C150FA5797C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplements", "..\SushiBarListImplements\SushiBarListImplements.csproj", "{BAA05DBA-C77F-4CF6-9998-4C150FA5797C}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "..\SushiBarFileImplement\SushiBarFileImplement.csproj", "{15B17431-20EB-4A87-9088-C19E5C2DD209}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDatabaseImplement", "..\SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{920F7D63-E1A0-4629-8322-AAD4A5F5ECAF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {15B17431-20EB-4A87-9088-C19E5C2DD209}.Debug|Any CPU.Build.0 = Debug|Any CPU {15B17431-20EB-4A87-9088-C19E5C2DD209}.Release|Any CPU.ActiveCfg = Release|Any CPU {15B17431-20EB-4A87-9088-C19E5C2DD209}.Release|Any CPU.Build.0 = Release|Any CPU + {920F7D63-E1A0-4629-8322-AAD4A5F5ECAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {920F7D63-E1A0-4629-8322-AAD4A5F5ECAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {920F7D63-E1A0-4629-8322-AAD4A5F5ECAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {920F7D63-E1A0-4629-8322-AAD4A5F5ECAF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBarView.csproj b/SushiBar/SushiBarView.csproj index 8bedfbf..fcd104a 100644 --- a/SushiBar/SushiBarView.csproj +++ b/SushiBar/SushiBarView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -19,6 +23,7 @@ + diff --git a/SushiBarDatabaseImplement/Implements/ComponentStorage.cs b/SushiBarDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..0417c41 --- /dev/null +++ b/SushiBarDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,73 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + 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 SushiBarDatabase(); + 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 SushiBarDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new SushiBarDatabase(); + 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 SushiBarDatabase(); + 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/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBarDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..ffcd06a --- /dev/null +++ b/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ + +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Orders.Select(x => AccessViewModel(x.GetViewModel)).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if(!model.Id.HasValue) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessViewModel(x.GetViewModel)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return AccessViewModel(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id).GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessViewModel(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessViewModel(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) return null; + context.Remove(order); + context.SaveChanges(); + return AccessViewModel(order.GetViewModel); + } + public static OrderViewModel? AccessViewModel(OrderViewModel viewModel) + { + using var context = new SushiBarDatabase(); + + var sushi = context.Sushis.FirstOrDefault(x => x.Id == viewModel.SushiId); + if (viewModel != null) + { + viewModel.SushiName = sushi!.SushiName; + return viewModel; + } + + return null; + } + } +} diff --git a/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBarDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..6808051 --- /dev/null +++ b/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,260 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Shops + .Include(x => x.Sushis) + .ThenInclude(x => x.Sushi) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new SushiBarDatabase(); + return context.Shops + .Include(x => x.Sushis) + .ThenInclude(x => x.Sushi) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + using var context = new SushiBarDatabase(); + return context.Shops + .Include(x => x.Sushis) + .ThenInclude(x => x.Sushi) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && + x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new SushiBarDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + + shop.Update(model); + context.SaveChanges(); + shop.UpdateSushis(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + /// + /// Удаление элемента + /// + /// + /// + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Shops + .Include(x => x.Sushis) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + /// + /// Продажа изделий + /// + /// + /// + /// + public bool SellSushis(ISushiModel model, int count) + { + using var context = new SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops + .Include(x => x.Sushis) + .ThenInclude(x => x.Sushi) + .ToList() + .Where(x => x.ShopSushis.ContainsKey(model.Id)); + + foreach (var shop in shops) + { + int countInCurrentShop = shop.ShopSushis[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopSushis + .Where(x => x.SushiId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopSushis.Remove(elem); + shop.ShopSushis.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ShopSushis[model.Id] = (shop.ShopSushis[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdateSushis(context, new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopSushis = shop.ShopSushis, + MaxCountSushis = shop.MaxCountSushis + }); + } + if (count <= 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + /// + /// Проверка наличия в нужном количестве + /// + /// + /// + /// + /// + public bool CheckCountSushi(ISushiModel model, int count) + { + throw new NotImplementedException(); + } + /*public bool CheckCountSushi(ISushiModel model, int count) + { + int store = _source.Shops.Select(x => x.ShopSushis.Select(y => + (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum(); + return store >= count; + } + + public bool SellSushis(ISushiModel model, int count) + { + var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (sushi == null || !CheckCountSushi(model, count)) + { + throw new ArgumentNullException("Такого количества суш нет, продать столько низя "); + } + + foreach (var shop in _source.Shops) + { + var sushis = shop.ShopSushis; + foreach (var elem in sushis.Where(x => x.Value.Item1.Id == sushi.Id)) + { + var selling = Math.Min(elem.Value.Item2, count); + sushis[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling); + count -= selling; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = model.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopSushis = sushis, + MaxCountSushis = shop.MaxCountSushis + }); + } + + _source.SaveShops(); + return true; + }*/ + } +} diff --git a/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBarDatabaseImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..e7d9130 --- /dev/null +++ b/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -0,0 +1,102 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Sushis + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Sushis + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.SushiName.Contains(model.SushiName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && + !model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return context.Sushis + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == + model.SushiName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public SushiViewModel? Insert(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + var newSushi = Sushi.Create(context, model); + if (newSushi == null) + { + return null; + } + context.Sushis.Add(newSushi); + context.SaveChanges(); + return newSushi.GetViewModel; + } + public SushiViewModel? Update(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Sushi = context.Sushis.FirstOrDefault(rec => rec.Id == model.Id); + if (Sushi == null) + { + return null; + } + Sushi.Update(model); + context.SaveChanges(); + Sushi.UpdateComponents(context, model); + transaction.Commit(); + return Sushi.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public SushiViewModel? Delete(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Sushis + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Sushis.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.Designer.cs b/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.Designer.cs new file mode 100644 index 0000000..08c09f4 --- /dev/null +++ b/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + [DbContext(typeof(SushiBarDatabase))] + [Migration("20240421182209_InitCreate")] + partial class InitCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("SushiId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountSushis") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SushiId"); + + b.ToTable("ShopSushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Sushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SushiId"); + + b.ToTable("SushiComponents"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", null) + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop") + .WithMany("Sushis") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany() + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component") + .WithMany("SushiComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany("Components") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b => + { + b.Navigation("SushiComponents"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("Sushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.cs b/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.cs new file mode 100644 index 0000000..278fc76 --- /dev/null +++ b/SushiBarDatabaseImplement/Migrations/20240421182209_InitCreate.cs @@ -0,0 +1,184 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class InitCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxCountSushis = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Sushis", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Sushis", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Sushis_SushiId", + column: x => x.SushiId, + principalTable: "Sushis", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShopSushis", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopSushis", x => x.Id); + table.ForeignKey( + name: "FK_ShopSushis_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopSushis_Sushis_SushiId", + column: x => x.SushiId, + principalTable: "Sushis", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SushiComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SushiId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SushiComponents", x => x.Id); + table.ForeignKey( + name: "FK_SushiComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SushiComponents_Sushis_SushiId", + column: x => x.SushiId, + principalTable: "Sushis", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SushiId", + table: "Orders", + column: "SushiId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSushis_ShopId", + table: "ShopSushis", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSushis_SushiId", + table: "ShopSushis", + column: "SushiId"); + + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_ComponentId", + table: "SushiComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_SushiId", + table: "SushiComponents", + column: "SushiId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ShopSushis"); + + migrationBuilder.DropTable( + name: "SushiComponents"); + + migrationBuilder.DropTable( + name: "Shops"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Sushis"); + } + } +} diff --git a/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs new file mode 100644 index 0000000..c0b8d48 --- /dev/null +++ b/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -0,0 +1,243 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SushiBarDatabaseImplement; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + [DbContext(typeof(SushiBarDatabase))] + partial class SushiBarDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("SushiId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountSushis") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SushiId"); + + b.ToTable("ShopSushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Sushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SushiId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SushiId"); + + b.ToTable("SushiComponents"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", null) + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop") + .WithMany("Sushis") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany() + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => + { + b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component") + .WithMany("SushiComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") + .WithMany("Components") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b => + { + b.Navigation("SushiComponents"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("Sushis"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBarDatabaseImplement/Models/Component.cs b/SushiBarDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..e394f9a --- /dev/null +++ b/SushiBarDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using SushiBarDataModels.Models; +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; + +namespace SushiBarDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List SushiComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/SushiBarDatabaseImplement/Models/Order.cs b/SushiBarDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..21d251d --- /dev/null +++ b/SushiBarDatabaseImplement/Models/Order.cs @@ -0,0 +1,62 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; +using SushiBarDataModels.Enums; +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int SushiId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/SushiBarDatabaseImplement/Models/Shop.cs b/SushiBarDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..7ab92a5 --- /dev/null +++ b/SushiBarDatabaseImplement/Models/Shop.cs @@ -0,0 +1,113 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SushiBarDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + [Required] + public string ShopName { get; private set; } + + [Required] + public string Address { get; private set; } + + [Required] + public DateTime DateOpening { get; private set; } + + [Required] + public int MaxCountSushis { get; private set; } + + [ForeignKey("ShopId")] + public List Sushis { get; private set; } = new(); + + private Dictionary? _shopSushis = null; + + [NotMapped] + public Dictionary ShopSushis + { + get + { + if (_shopSushis == null) + { + _shopSushis = Sushis.ToDictionary(recPC => recPC.SushiId, recPC => (recPC.Sushi as ISushiModel, recPC.Count)); + } + return _shopSushis; + } + } + + public static Shop Create(SushiBarDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountSushis = model.MaxCountSushis, + DateOpening = model.DateOpening, + Sushis = model.ShopSushis.Select(x => new ShopSushi + { + Sushi = context.Sushis.First(y => y.Id == x.Key), Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountSushis = model.MaxCountSushis; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCountSushis = MaxCountSushis, + ShopSushis = ShopSushis + }; + public void UpdateSushis(SushiBarDatabase context, ShopBindingModel model) + { + var ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopSushis != null && ShopSushis.Count > 0) + { + // удалили те, которых нет в модели + context.ShopSushis.RemoveRange(ShopSushis.Where(rec => !model.ShopSushis.ContainsKey(rec.SushiId))); + context.SaveChanges(); + ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateSushi in ShopSushis) + { + updateSushi.Count = model.ShopSushis[updateSushi.SushiId].Item2; + model.ShopSushis.Remove(updateSushi.SushiId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopSushis) + { + context.ShopSushis.Add(new ShopSushi + { + Shop = shop, + Sushi = context.Sushis.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _shopSushis = null; + } + } +} \ No newline at end of file diff --git a/SushiBarDatabaseImplement/Models/ShopSushi.cs b/SushiBarDatabaseImplement/Models/ShopSushi.cs new file mode 100644 index 0000000..5317784 --- /dev/null +++ b/SushiBarDatabaseImplement/Models/ShopSushi.cs @@ -0,0 +1,27 @@ +using SushiBarListImplements.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Models +{ + public class ShopSushi + { + public int Id { get; set; } + + [Required] + public int SushiId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Sushi Sushi { get; set; } = new(); + } +} diff --git a/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBarDatabaseImplement/Models/Sushi.cs new file mode 100644 index 0000000..25de728 --- /dev/null +++ b/SushiBarDatabaseImplement/Models/Sushi.cs @@ -0,0 +1,92 @@ +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; + + +namespace SushiBarDatabaseImplement.Models +{ + public class Sushi : ISushiModel + { + public int Id { get; set; } + + [Required] + public string SushiName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _sushiComponents = null; + + [NotMapped] + public Dictionary SushiComponents + { + get + { + if (_sushiComponents == null) + { + _sushiComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _sushiComponents; + } + } + [ForeignKey("SushiId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("SushiId")] + public virtual List Orders { get; set; } = new(); + public static Sushi Create(SushiBarDatabase context, SushiBindingModel model) + { + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Components = model.SushiComponents.Select(x => new SushiComponent + { + Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SushiBindingModel model) + { + SushiName = model.SushiName; + Price = model.Price; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model) + { + var SushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList(); + if (SushiComponents != null && SushiComponents.Count > 0) + { // удалили те, которых нет в модели + context.SushiComponents.RemoveRange(SushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in SushiComponents) + { + updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2; + model.SushiComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Sushi = context.Sushis.First(x => x.Id == Id); + foreach (var pc in model.SushiComponents) + { + context.SushiComponents.Add(new SushiComponent + { + Sushi = Sushi, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _sushiComponents = null; + } + } +} diff --git a/SushiBarDatabaseImplement/Models/SushiComponent.cs b/SushiBarDatabaseImplement/Models/SushiComponent.cs new file mode 100644 index 0000000..60c93fa --- /dev/null +++ b/SushiBarDatabaseImplement/Models/SushiComponent.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + + +namespace SushiBarDatabaseImplement.Models +{ + public class SushiComponent + { + public int Id { get; set; } + [Required] + public int SushiId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Sushi Sushi { get; set; } = new(); + + } +} diff --git a/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBarDatabaseImplement/SushiBarDatabase.cs new file mode 100644 index 0000000..8f7cc96 --- /dev/null +++ b/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement +{ + public class SushiBarDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS; + Initial Catalog=SushiBarDatabaseLC3; + Integrated Security=True; + MultipleActiveResultSets=True;; + TrustServerCertificate=True" + ); + + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Sushis { set; get; } + public virtual DbSet SushiComponents { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopSushis { set; get; } + } +} diff --git a/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj new file mode 100644 index 0000000..4a4eae2 --- /dev/null +++ b/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + +