From f837f6d12e23423901735c3c4a438514544ec78b Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 13 Mar 2024 08:36:24 +0400 Subject: [PATCH 1/6] install packs --- SushiBar/SushiBar/SushiBar.csproj | 9 +++++++++ .../SushiBarBusinessLogic.csproj | 6 ++++++ SushiBar/SushiBarBusinessLogic_/SushiLogic.cs | 1 - SushiBar/SushiBarContracts/SushiBarContracts.csproj | 9 +++++++++ SushiBar/SushiBarDataModels/SushiBarDataModels.csproj | 9 +++++++++ .../SushiBarFileImplement/SushiBarFileImplement.csproj | 9 +++++++++ .../SushiBarListImplement.csproj | 9 +++++++++ SushiBar/SushiBarView/SushiBarView.csproj | 10 ++++++++++ 8 files changed, 61 insertions(+), 1 deletion(-) diff --git a/SushiBar/SushiBar/SushiBar.csproj b/SushiBar/SushiBar/SushiBar.csproj index b57c89e..1f7a282 100644 --- a/SushiBar/SushiBar/SushiBar.csproj +++ b/SushiBar/SushiBar/SushiBar.csproj @@ -8,4 +8,13 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + \ No newline at end of file diff --git a/SushiBar/SushiBarBusinessLogic_/SushiBarBusinessLogic.csproj b/SushiBar/SushiBarBusinessLogic_/SushiBarBusinessLogic.csproj index 8d13c8d..e674f0a 100644 --- a/SushiBar/SushiBarBusinessLogic_/SushiBarBusinessLogic.csproj +++ b/SushiBar/SushiBarBusinessLogic_/SushiBarBusinessLogic.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/SushiBar/SushiBarBusinessLogic_/SushiLogic.cs b/SushiBar/SushiBarBusinessLogic_/SushiLogic.cs index fefe4a4..339a27a 100644 --- a/SushiBar/SushiBarBusinessLogic_/SushiLogic.cs +++ b/SushiBar/SushiBarBusinessLogic_/SushiLogic.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; diff --git a/SushiBar/SushiBarContracts/SushiBarContracts.csproj b/SushiBar/SushiBarContracts/SushiBarContracts.csproj index a25f2c8..e101f33 100644 --- a/SushiBar/SushiBarContracts/SushiBarContracts.csproj +++ b/SushiBar/SushiBarContracts/SushiBarContracts.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj b/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj index 132c02c..0dfd04d 100644 --- a/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj +++ b/SushiBar/SushiBarDataModels/SushiBarDataModels.csproj @@ -6,4 +6,13 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj index ec020c5..3e8ca0c 100644 --- a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj +++ b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SushiBar/SushiBarListImplement_/SushiBarListImplement.csproj b/SushiBar/SushiBarListImplement_/SushiBarListImplement.csproj index ec020c5..3e8ca0c 100644 --- a/SushiBar/SushiBarListImplement_/SushiBarListImplement.csproj +++ b/SushiBar/SushiBarListImplement_/SushiBarListImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/SushiBar/SushiBarView/SushiBarView.csproj b/SushiBar/SushiBarView/SushiBarView.csproj index 574ede4..8fa9389 100644 --- a/SushiBar/SushiBarView/SushiBarView.csproj +++ b/SushiBar/SushiBarView/SushiBarView.csproj @@ -19,6 +19,16 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + -- 2.25.1 From 3becbcf0bfbafad7145cbcf847b0999cee336eac Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 13 Mar 2024 15:25:35 +0400 Subject: [PATCH 2/6] done --- SushiBar/SushiBar.sln | 16 +- .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 84 +++++++++ .../Implements/SushiStorage.cs | 99 ++++++++++ .../20240313082356_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240313082356_InitialCreate.cs | 125 +++++++++++++ .../SushiBarDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 65 +++++++ .../SushiBarDatabaseImplement/Models/Order.cs | 74 ++++++++ .../SushiBarDatabaseImplement/Models/Sushi.cs | 101 +++++++++++ .../Models/SushiComponent.cs | 22 +++ .../SushiBarDatabaseImplement.csproj | 23 +++ .../SushiBarDatatbase.cs | 26 +++ SushiBar/SushiBarView/Program.cs | 2 +- SushiBar/SushiBarView/SushiBarView.csproj | 1 + 15 files changed, 1059 insertions(+), 2 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Component.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Order.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/SushiComponent.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index cdba406..1630fe1 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "Su EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic_\SushiBarBusinessLogic.csproj", "{D7936C41-351B-43FD-B90B-5477375EEC1E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{3FDA8B7A-9E62-4A1B-B926-DC71C6C5812F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{3FDA8B7A-9E62-4A1B-B926-DC71C6C5812F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{B8DE57BC-14B3-47A9-93E9-F668D3F10A21}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -111,6 +113,18 @@ Global {3FDA8B7A-9E62-4A1B-B926-DC71C6C5812F}.Release|x64.Build.0 = Release|Any CPU {3FDA8B7A-9E62-4A1B-B926-DC71C6C5812F}.Release|x86.ActiveCfg = Release|Any CPU {3FDA8B7A-9E62-4A1B-B926-DC71C6C5812F}.Release|x86.Build.0 = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|x64.ActiveCfg = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|x64.Build.0 = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|x86.ActiveCfg = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Debug|x86.Build.0 = Debug|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|Any CPU.Build.0 = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|x64.ActiveCfg = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|x64.Build.0 = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|x86.ActiveCfg = Release|Any CPU + {B8DE57BC-14B3-47A9-93E9-F668D3F10A21}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..701a49f --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..c4af243 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Orders.Include(x => x.Sushi).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Orders.Include(x => x.Sushi).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Orders.Include(x => x.Sushi).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return 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 order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..abf8fea --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +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/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs new file mode 100644 index 0000000..7809e8c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240313082356_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .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.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", "Sushi") + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + 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.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs new file mode 100644 index 0000000..4367175 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : 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: "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: "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_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: "SushiComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Sushis"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs new file mode 100644 index 0000000..43e1c41 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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", "7.0.17") + .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.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", "Sushi") + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + 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.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Component.cs b/SushiBar/SushiBarDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..125d8d4 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Component.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; + +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/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..07c4247 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -0,0 +1,74 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int SushiId { get; private set; } + + public virtual Sushi Sushi { get; set; } = new(); + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order Create(SushiBarDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + Sushi = context.Sushis.First(x => x.Id == 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, + SushiName = Sushi.SushiName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs new file mode 100644 index 0000000..71b776c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -0,0 +1,101 @@ +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SushiBarContracts.BindingModels; +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/SushiBar/SushiBarDatabaseImplement/Models/SushiComponent.cs b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponent.cs new file mode 100644 index 0000000..56cf503 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj new file mode 100644 index 0000000..7adb075 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs new file mode 100644 index 0000000..715a501 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement +{ + public class SushiBarDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SushiBarDatabase;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; } + } +} diff --git a/SushiBar/SushiBarView/Program.cs b/SushiBar/SushiBarView/Program.cs index 0d12b88..836538a 100644 --- a/SushiBar/SushiBarView/Program.cs +++ b/SushiBar/SushiBarView/Program.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging; using SushiBarContracts.BusinessLogicsContracts; using System; using SushiBarBusinessLogic.BusinessLogics; -using SushiBarFileImplement.Implements; +using SushiBarDatabaseImplement.Implements; using SushiBarContracts.StoragesContracts; using SushiBarView; using NLog.Extensions.Logging; diff --git a/SushiBar/SushiBarView/SushiBarView.csproj b/SushiBar/SushiBarView/SushiBarView.csproj index 8fa9389..4e301ad 100644 --- a/SushiBar/SushiBarView/SushiBarView.csproj +++ b/SushiBar/SushiBarView/SushiBarView.csproj @@ -38,6 +38,7 @@ + -- 2.25.1 From bcab5d31c20ab6042cbd37bdf3b0121f8fe269a3 Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 13 Mar 2024 15:53:26 +0400 Subject: [PATCH 3/6] start --- SushiBar/SushiBarView/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SushiBar/SushiBarView/Program.cs b/SushiBar/SushiBarView/Program.cs index 836538a..47ac190 100644 --- a/SushiBar/SushiBarView/Program.cs +++ b/SushiBar/SushiBarView/Program.cs @@ -6,7 +6,7 @@ using SushiBarBusinessLogic.BusinessLogics; using SushiBarDatabaseImplement.Implements; using SushiBarContracts.StoragesContracts; using SushiBarView; -using NLog.Extensions.Logging; +using NLog.Extensions.Logging; namespace SushiBarView -- 2.25.1 From 1fd29b894847884ea55610ddbf963783e0af37d4 Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 27 Mar 2024 08:19:43 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=88=D0=B8=D1=80=D0=B8=D0=BD=D1=8B=20=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=BB=D0=B1=D1=86=D0=B0=20=D0=B8=D0=B7=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarView/FormMain.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SushiBar/SushiBarView/FormMain.cs b/SushiBar/SushiBarView/FormMain.cs index 0ca85b9..2688f16 100644 --- a/SushiBar/SushiBarView/FormMain.cs +++ b/SushiBar/SushiBarView/FormMain.cs @@ -53,8 +53,7 @@ namespace SushiBarView { dataGridView.DataSource = list; dataGridView.Columns["SushiId"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill; + } _logger.LogInformation("Загрузка заказов"); } -- 2.25.1 From 1203148896cdefb98221c66642ef57c257d2e59c Mon Sep 17 00:00:00 2001 From: ValAnn Date: Wed, 27 Mar 2024 14:25:07 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ShopLogic.cs | 10 + .../Implements/ShopStorage.cs | 185 ++++++++++++++++ .../20240313082356_InitialCreate.cs | 206 +++++++++--------- 3 files changed, 298 insertions(+), 103 deletions(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs diff --git a/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs b/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs index 47cfd25..f14617f 100644 --- a/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs +++ b/SushiBar/SushiBarBusinessLogic_/BusinessLogics/ShopLogic.cs @@ -125,6 +125,16 @@ namespace SushiBarBusinessLogic.BusinessLogics } shop.ShopSushis.Add(model.SushiId, (sushi, model.Count)); } + + _shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + ShopSushis = shop.ShopSushis, + SushiMaxCount = shop.SushiMaxCount, + }); return true; } diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f0f1fd1 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,185 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +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 new(); + } + 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(x => x.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 shop = context.Shops.Include(x => x.Sushis).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + + public bool RestockingShops(SupplyBindingModel model) + { + using var context = new SushiBarDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList(). + Where(x => x.SushiMaxCount > x.ShopSushis.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.SushiMaxCount - shop.ShopSushis.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopSushis.ContainsKey(model.SushiId)) + { + var datePair = shop.ShopSushis[model.SushiId]; + datePair.Item2 += refill; + shop.ShopSushis[model.SushiId] = datePair; + } + else + { + var sushi = context.Sushis.First(x => x.Id == model.SushiId); + shop.ShopSushis.Add(model.SushiId, (sushi, refill)); + } + shop.SushisDictionatyUpdate(context); + if (model.Count == 0) + { + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public bool Sale(SupplySearchModel model) + { + using var context = new SushiBarDatabase(); + 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.SushiId.Value)).OrderByDescending(x => x.ShopSushis[model.SushiId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopSushis[model.SushiId.Value].Item2; + if (residue > 0) + { + shop.ShopSushis.Remove(model.SushiId.Value); + shop.SushisDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopSushis.Remove(model.SushiId.Value); + else + { + var dataPair = shop.ShopSushis[model.SushiId.Value]; + dataPair.Item2 = -residue; + shop.ShopSushis[model.SushiId.Value] = dataPair; + } + + shop.SushisDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs index 4367175..3ffad67 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs @@ -8,118 +8,118 @@ namespace SushiBarDatabaseImplement.Migrations /// public partial class InitialCreate : 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); - }); + /// + 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: "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: "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: "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: "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.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_Orders_SushiId", + table: "Orders", + column: "SushiId"); - migrationBuilder.CreateIndex( - name: "IX_SushiComponents_ComponentId", - table: "SushiComponents", - column: "ComponentId"); + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_ComponentId", + table: "SushiComponents", + column: "ComponentId"); - migrationBuilder.CreateIndex( - name: "IX_SushiComponents_SushiId", - table: "SushiComponents", - column: "SushiId"); - } + migrationBuilder.CreateIndex( + name: "IX_SushiComponents_SushiId", + table: "SushiComponents", + column: "SushiId"); + } - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Orders"); + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); - migrationBuilder.DropTable( - name: "SushiComponents"); + migrationBuilder.DropTable( + name: "SushiComponents"); - migrationBuilder.DropTable( - name: "Components"); + migrationBuilder.DropTable( + name: "Components"); - migrationBuilder.DropTable( - name: "Sushis"); + migrationBuilder.DropTable( + name: "Sushis"); + } } } -} -- 2.25.1 From 84b0ae0b43e92393a6f03e38aa3259273e76ab04 Mon Sep 17 00:00:00 2001 From: ValAnn Date: Tue, 9 Apr 2024 19:43:29 +0400 Subject: [PATCH 6/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 1 + .../20240313082356_InitialCreate.cs | 125 --------- ... 20240409135559_InitialCreate.Designer.cs} | 79 +++++- .../20240409135559_InitialCreate.cs | 126 +++++++++ ...40409140637_InitialCreate_Hard.Designer.cs | 248 ++++++++++++++++++ .../20240409140637_InitialCreate_Hard.cs | 78 ++++++ .../SushiBarDatabaseModelSnapshot.cs | 77 ++++++ .../SushiBarDatabaseImplement/Models/Shop.cs | 119 +++++++++ .../Models/ShopSushis.cs | 28 ++ .../SushiBarDatabaseImplement.csproj | 1 + .../SushiBarDatatbase.cs | 2 + 11 files changed, 758 insertions(+), 126 deletions(-) delete mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs rename SushiBar/SushiBarDatabaseImplement/Migrations/{20240313082356_InitialCreate.Designer.cs => 20240409135559_InitialCreate.Designer.cs} (68%) create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/Shop.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Models/ShopSushis.cs diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs index f0f1fd1..c5d472c 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs @@ -5,6 +5,7 @@ using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; using SushiBarDatabaseImplement.Models; + namespace SushiBarDatabaseImplement.Implements { public class ShopStorage : IShopStorage diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs deleted file mode 100644 index 3ffad67..0000000 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace SushiBarDatabaseImplement.Migrations -{ - /// - public partial class InitialCreate : 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: "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: "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_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: "SushiComponents"); - - migrationBuilder.DropTable( - name: "Components"); - - migrationBuilder.DropTable( - name: "Sushis"); - } - } - } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.Designer.cs similarity index 68% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.Designer.cs index 7809e8c..4dc8011 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313082356_InitialCreate.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using SushiBarDatabaseImplement; namespace SushiBarDatabaseImplement.Migrations { [DbContext(typeof(SushiBarDatabase))] - [Migration("20240313082356_InitialCreate")] + [Migration("20240409135559_InitialCreate")] partial class InitialCreate { /// @@ -78,6 +78,59 @@ namespace SushiBarDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SushiMaxCount") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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") @@ -135,6 +188,25 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("Sushi"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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") @@ -159,6 +231,11 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("SushiComponents"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("Sushis"); + }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => { b.Navigation("Components"); diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.cs new file mode 100644 index 0000000..dd648b1 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409135559_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : 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: "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: "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_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: "SushiComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Sushis"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.Designer.cs new file mode 100644 index 0000000..e1a1efe --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.Designer.cs @@ -0,0 +1,248 @@ +// +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("20240409140637_InitialCreate_Hard")] + partial class InitialCreate_Hard + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .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("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SushiMaxCount") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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", "Sushi") + .WithMany("Orders") + .HasForeignKey("SushiId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Sushi"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.cs new file mode 100644 index 0000000..4a5a1d0 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240409140637_InitialCreate_Hard.cs @@ -0,0 +1,78 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SushiBarDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate_Hard : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + 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), + Adress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + SushiMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + 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.CreateIndex( + name: "IX_ShopSushis_ShopId", + table: "ShopSushis", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSushis_SushiId", + table: "ShopSushis", + column: "SushiId"); + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopSushis"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index 43e1c41..b69c52f 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -75,6 +75,59 @@ namespace SushiBarDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SushiMaxCount") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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") @@ -132,6 +185,25 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("Sushi"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushis", 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") @@ -156,6 +228,11 @@ namespace SushiBarDatabaseImplement.Migrations b.Navigation("SushiComponents"); }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b => + { + b.Navigation("Sushis"); + }); + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => { b.Navigation("Components"); diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..8455db5 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Shop.cs @@ -0,0 +1,119 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + + public string Adress { get; set; } = string.Empty; + + public DateTime OpeningDate { get; set; } + + public int SushiMaxCount { get; set; } + + private Dictionary? _shopSushis = null; + + public Dictionary ShopSushis + { + get + { + if (_shopSushis == null) + { + if (_shopSushis == null) + { + _shopSushis = Sushis + .ToDictionary(recSP => recSP.SushiId, recSP => (recSP.Sushi as ISushiModel, recSP.Count)); + } + return _shopSushis; + } + return _shopSushis; + } + } + + [ForeignKey("ShopId")] + public List Sushis { get; set; } = new(); + + public static Shop Create(SushiBarDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Sushis = model.ShopSushis.Select(x => new ShopSushis + { + Sushi = context.Sushis.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + SushiMaxCount = model.SushiMaxCount + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + SushiMaxCount = model.SushiMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopSushis = ShopSushis, + SushiMaxCount = SushiMaxCount + }; + + 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 ar in model.ShopSushis) + { + context.ShopSushis.Add(new ShopSushis + { + Shop = shop, + Sushi = context.Sushis.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 + }); + context.SaveChanges(); + } + _shopSushis = null; + } + + public void SushisDictionatyUpdate(SushiBarDatabase context) + { + UpdateSushis(context, new ShopBindingModel + { + Id = Id, + ShopSushis = ShopSushis, + }); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Models/ShopSushis.cs b/SushiBar/SushiBarDatabaseImplement/Models/ShopSushis.cs new file mode 100644 index 0000000..1d54689 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/ShopSushis.cs @@ -0,0 +1,28 @@ +using SushiBarDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Models +{ + public class ShopSushis + { + 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/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj index 7adb075..14e4efd 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -18,6 +18,7 @@ + diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs index 715a501..e1d490f 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs @@ -22,5 +22,7 @@ namespace SushiBarDatabaseImplement public virtual DbSet Sushis { set; get; } public virtual DbSet SushiComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopSushis { get; set; } } } -- 2.25.1