From 45235c7a829f4924c593208382fb468181462b3e Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Mon, 27 Feb 2023 17:07:02 +0400 Subject: [PATCH 1/2] add database --- .../.idea/.idea.SushiBar/.idea/.gitignore | 3 + .../.idea/.idea.SushiBar/.idea/encodings.xml | 4 + .../.idea.SushiBar/.idea/indexLayout.xml | 8 + .../.idea/projectSettingsUpdater.xml | 6 + SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml | 6 + SushiBar/SushiBar.sln | 8 +- SushiBar/SushiBar/SushiBar.csproj | 5 + .../Implements/ComponentStorage.cs | 102 ++++++++++ .../Implements/OrderStorage.cs | 100 ++++++++++ .../Implements/SushiStorage.cs | 121 ++++++++++++ .../20230227124505_InitialCreate.Designer.cs | 175 ++++++++++++++++++ .../20230227124505_InitialCreate.cs | 126 +++++++++++++ .../Migrations/SushiDatabaseModelSnapshot.cs | 172 +++++++++++++++++ .../Models/Component.cs | 57 ++++++ .../SushiBarDatabaseImplement/Models/Order.cs | 81 ++++++++ .../SushiBarDatabaseImplement/Models/Sushi.cs | 88 +++++++++ .../Models/SushiComponents.cs | 17 ++ .../SushiBarDatabase.cs | 21 +++ .../SushiBarDatabaseImplement.csproj | 22 +++ 19 files changed, 1121 insertions(+), 1 deletion(-) create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/.gitignore create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/encodings.xml create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml create mode 100644 SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml 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/20230227124505_InitialCreate.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.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/SushiComponents.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/.gitignore b/SushiBar/.idea/.idea.SushiBar/.idea/.gitignore new file mode 100644 index 0000000..d34719a --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/encodings.xml b/SushiBar/.idea/.idea.SushiBar/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml b/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml b/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml b/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/SushiBar/.idea/.idea.SushiBar/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index f8e68e4..5daa8e8 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarBusinessLogic", "Su EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushibarListImplement", "SushibarListImplement\SushibarListImplement.csproj", "{1BA388CC-72A1-47FE-A1DC-56C6A95D2F09}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{E55FF400-876B-4E35-8536-E099241195B8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{E55FF400-876B-4E35-8536-E099241195B8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{4E59EEFE-0146-4AFB-BA8C-18F2D8F8D22D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {E55FF400-876B-4E35-8536-E099241195B8}.Debug|Any CPU.Build.0 = Debug|Any CPU {E55FF400-876B-4E35-8536-E099241195B8}.Release|Any CPU.ActiveCfg = Release|Any CPU {E55FF400-876B-4E35-8536-E099241195B8}.Release|Any CPU.Build.0 = Release|Any CPU + {4E59EEFE-0146-4AFB-BA8C-18F2D8F8D22D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4E59EEFE-0146-4AFB-BA8C-18F2D8F8D22D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4E59EEFE-0146-4AFB-BA8C-18F2D8F8D22D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4E59EEFE-0146-4AFB-BA8C-18F2D8F8D22D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBar/SushiBar.csproj b/SushiBar/SushiBar/SushiBar.csproj index 97cb249..cec725f 100644 --- a/SushiBar/SushiBar/SushiBar.csproj +++ b/SushiBar/SushiBar/SushiBar.csproj @@ -9,12 +9,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..2035a6f --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,102 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + 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; + } + + 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 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 List GetFullList() + { + using var context = new SushiBarDatabase(); + + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + 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; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7f2101b --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,100 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new SushiBarDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new SushiBarDatabase(); + + return context.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + 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 => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new SushiBarDatabase(); + + return context.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new SushiBarDatabase(); + + 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; + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..4423273 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Implements/SushiStorage.cs @@ -0,0 +1,121 @@ +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 SushiViewModel? Delete(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + + var element = context.Sushi + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Sushi.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + + using var context = new SushiBarDatabase(); + + return context.Sushi + .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 List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + + using var context = new SushiBarDatabase(); + + return context.Sushi + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.SushiName.Contains(model.SushiName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new SushiBarDatabase(); + + return context.Sushi + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList().Select(x => x.GetViewModel) + .ToList(); + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + using var context = new SushiBarDatabase(); + + var newSushi = Sushi.Create(context, model); + + if (newSushi == null) + { + return null; + } + + context.Sushi.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.Sushi.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; + } + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.Designer.cs new file mode 100644 index 0000000..d16cf1e --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.Designer.cs @@ -0,0 +1,175 @@ +// +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("20230227124505_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .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.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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("Sushi"); + }); + + 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("SushiComponent") + .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("SushiComponent"); + }); + + modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_InitialCreate.cs new file mode 100644 index 0000000..aa77eb0 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20230227124505_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: "Sushi", + 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_Sushi", 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), + SushiName = table.Column(type: "nvarchar(max)", 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_Sushi_SushiId", + column: x => x.SushiId, + principalTable: "Sushi", + 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_Sushi_SushiId", + column: x => x.SushiId, + principalTable: "Sushi", + 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: "Sushi"); + } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs new file mode 100644 index 0000000..6583fcb --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiDatabaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +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 SushiDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .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.Property("SushiName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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("Sushi"); + }); + + 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("SushiComponent") + .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("SushiComponent"); + }); + + 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..881d6db --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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 SushiComponent { 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..564399b --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -0,0 +1,81 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int SushiId { get; private set; } + + public string SushiName { get; private set; } = string.Empty; + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Unknown; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public virtual Sushi Sushi { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + SushiName = model.SushiName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + SushiId = model.SushiId; + SushiName = model.SushiName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + SushiId = SushiId, + SushiName = 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..d97988c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/Sushi.cs @@ -0,0 +1,88 @@ +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +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 + { + _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.Sushi.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/SushiComponents.cs b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs new file mode 100644 index 0000000..dca7318 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Models/SushiComponents.cs @@ -0,0 +1,17 @@ +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/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs new file mode 100644 index 0000000..f4c7b93 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -0,0 +1,21 @@ +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=LAPTOP-QKSH4DCA\SQLEXPRESS;Initial Catalog=SushiBarDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Sushi { set; get; } + public virtual DbSet SushiComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj new file mode 100644 index 0000000..af97e9f --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + -- 2.25.1 From 9b712685baa840b917a5b446d0fc99ca857d2312 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Mon, 27 Feb 2023 17:15:26 +0400 Subject: [PATCH 2/2] Edit main form Complete lab 3 --- SushiBar/SushiBar/FormMain.Designer.cs | 48 +++++++++++++------------- SushiBar/SushiBar/Program.cs | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/SushiBar/SushiBar/FormMain.Designer.cs b/SushiBar/SushiBar/FormMain.Designer.cs index 709f9ea..d372812 100644 --- a/SushiBar/SushiBar/FormMain.Designer.cs +++ b/SushiBar/SushiBar/FormMain.Designer.cs @@ -31,13 +31,13 @@ this.dataGridView = new System.Windows.Forms.DataGridView(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.directoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.componentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.sushiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonSubmit = new System.Windows.Forms.Button(); this.buttonReady = new System.Windows.Forms.Button(); this.buttonIssue = new System.Windows.Forms.Button(); this.buttonReload = new System.Windows.Forms.Button(); - this.componentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.sushiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -48,7 +48,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(656, 411); + this.dataGridView.Size = new System.Drawing.Size(796, 411); this.dataGridView.TabIndex = 0; // // menuStrip1 @@ -57,7 +57,7 @@ this.directoryToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(800, 24); + this.menuStrip1.Size = new System.Drawing.Size(940, 24); this.menuStrip1.TabIndex = 1; this.menuStrip1.Text = "menuStrip1"; // @@ -70,9 +70,23 @@ this.directoryToolStripMenuItem.Size = new System.Drawing.Size(67, 20); this.directoryToolStripMenuItem.Text = "Directory"; // + // componentsToolStripMenuItem + // + this.componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; + this.componentsToolStripMenuItem.Size = new System.Drawing.Size(143, 22); + this.componentsToolStripMenuItem.Text = "Components"; + this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); + // + // sushiToolStripMenuItem + // + this.sushiToolStripMenuItem.Name = "sushiToolStripMenuItem"; + this.sushiToolStripMenuItem.Size = new System.Drawing.Size(143, 22); + this.sushiToolStripMenuItem.Text = "Sushi"; + this.sushiToolStripMenuItem.Click += new System.EventHandler(this.SushiToolStripMenuItem_Click); + // // buttonCreateOrder // - this.buttonCreateOrder.Location = new System.Drawing.Point(674, 27); + this.buttonCreateOrder.Location = new System.Drawing.Point(814, 27); this.buttonCreateOrder.Name = "buttonCreateOrder"; this.buttonCreateOrder.Size = new System.Drawing.Size(114, 23); this.buttonCreateOrder.TabIndex = 2; @@ -82,7 +96,7 @@ // // buttonSubmit // - this.buttonSubmit.Location = new System.Drawing.Point(674, 56); + this.buttonSubmit.Location = new System.Drawing.Point(814, 56); this.buttonSubmit.Name = "buttonSubmit"; this.buttonSubmit.Size = new System.Drawing.Size(114, 23); this.buttonSubmit.TabIndex = 3; @@ -92,7 +106,7 @@ // // buttonReady // - this.buttonReady.Location = new System.Drawing.Point(674, 85); + this.buttonReady.Location = new System.Drawing.Point(814, 85); this.buttonReady.Name = "buttonReady"; this.buttonReady.Size = new System.Drawing.Size(114, 23); this.buttonReady.TabIndex = 4; @@ -102,7 +116,7 @@ // // buttonIssue // - this.buttonIssue.Location = new System.Drawing.Point(674, 114); + this.buttonIssue.Location = new System.Drawing.Point(814, 114); this.buttonIssue.Name = "buttonIssue"; this.buttonIssue.Size = new System.Drawing.Size(114, 23); this.buttonIssue.TabIndex = 5; @@ -112,7 +126,7 @@ // // buttonReload // - this.buttonReload.Location = new System.Drawing.Point(674, 143); + this.buttonReload.Location = new System.Drawing.Point(814, 143); this.buttonReload.Name = "buttonReload"; this.buttonReload.Size = new System.Drawing.Size(114, 23); this.buttonReload.TabIndex = 6; @@ -120,25 +134,11 @@ this.buttonReload.UseVisualStyleBackColor = true; this.buttonReload.Click += new System.EventHandler(this.ButtonReload_Click); // - // componentsToolStripMenuItem - // - this.componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - this.componentsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.componentsToolStripMenuItem.Text = "Components"; - this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); - // - // sushiToolStripMenuItem - // - this.sushiToolStripMenuItem.Name = "sushiToolStripMenuItem"; - this.sushiToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.sushiToolStripMenuItem.Text = "Sushi"; - this.sushiToolStripMenuItem.Click += new System.EventHandler(this.SushiToolStripMenuItem_Click); - // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(940, 450); this.Controls.Add(this.buttonReload); this.Controls.Add(this.buttonIssue); this.Controls.Add(this.buttonReady); diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index adeec3e..af6a63d 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogics; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarFileImplement.Implements; +using SushiBarDatabaseImplement.Implements; namespace SushiBar { -- 2.25.1