From 1b5fad8c375281b073a8d2ec506b2d0de6124034 Mon Sep 17 00:00:00 2001 From: tellsense Date: Thu, 28 Mar 2024 21:55:29 +0400 Subject: [PATCH 1/4] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=B0.=20=D0=A1?= =?UTF-8?q?=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= =?UTF-8?q?.=20=D0=A1=D0=BA=D0=B0=D1=87=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D0=BE=D0=B2.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBar.sln | 8 ++++++- SushiBar/SushiBarDatabaseImplement/Class1.cs | 7 ++++++ .../SushiBarDatabaseImplement.csproj | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 SushiBar/SushiBarDatabaseImplement/Class1.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 6bb25d6..b4d5341 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "Su EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarView", "SushiBarView\SushiBarView.csproj", "{149793FF-D4B1-4627-BBCB-ADFC6769C8FE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{FAF3D975-5D08-4515-A51A-94FF76BB985A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{FAF3D975-5D08-4515-A51A-94FF76BB985A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{DDA8F283-045D-4556-80DA-85CB448B6263}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {FAF3D975-5D08-4515-A51A-94FF76BB985A}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAF3D975-5D08-4515-A51A-94FF76BB985A}.Release|Any CPU.ActiveCfg = Release|Any CPU {FAF3D975-5D08-4515-A51A-94FF76BB985A}.Release|Any CPU.Build.0 = Release|Any CPU + {DDA8F283-045D-4556-80DA-85CB448B6263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DDA8F283-045D-4556-80DA-85CB448B6263}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DDA8F283-045D-4556-80DA-85CB448B6263}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DDA8F283-045D-4556-80DA-85CB448B6263}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBarDatabaseImplement/Class1.cs b/SushiBar/SushiBarDatabaseImplement/Class1.cs new file mode 100644 index 0000000..2f98d5c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Class1.cs @@ -0,0 +1,7 @@ +namespace SushiBarDatabaseImplement +{ + public class Class1 + { + + } +} 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 + + + + + + + + + -- 2.25.1 From 0dc32849e6990f78f8014277f97950cb7e286353 Mon Sep 17 00:00:00 2001 From: tellsense Date: Thu, 28 Mar 2024 23:00:05 +0400 Subject: [PATCH 2/4] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarDatabaseImplement/Class1.cs | 7 - .../SushiBarDatabaseImplement/Component.cs | 55 ++++++ .../ComponentStorage.cs | 81 +++++++++ .../20240328181724_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240328181724_InitialCreate.cs | 125 +++++++++++++ .../SushiBarDatabaseModelSnapshot.cs | 168 +++++++++++++++++ SushiBar/SushiBarDatabaseImplement/Order.cs | 69 +++++++ .../SushiBarDatabaseImplement/OrderStorage.cs | 79 ++++++++ SushiBar/SushiBarDatabaseImplement/Sushi.cs | 101 +++++++++++ .../SushiBarDatatbase.cs | 26 +++ .../SushiComponent.cs | 16 ++ .../SushiBarDatabaseImplement/SushiStorage.cs | 94 ++++++++++ SushiBar/SushiBarView/SushiBarView.csproj | 4 + 13 files changed, 989 insertions(+), 7 deletions(-) delete mode 100644 SushiBar/SushiBarDatabaseImplement/Class1.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Component.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/ComponentStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_InitialCreate.Designer.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_InitialCreate.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Order.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/OrderStorage.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/Sushi.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiComponent.cs create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiStorage.cs diff --git a/SushiBar/SushiBarDatabaseImplement/Class1.cs b/SushiBar/SushiBarDatabaseImplement/Class1.cs deleted file mode 100644 index 2f98d5c..0000000 --- a/SushiBar/SushiBarDatabaseImplement/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SushiBarDatabaseImplement -{ - public class Class1 - { - - } -} diff --git a/SushiBar/SushiBarDatabaseImplement/Component.cs b/SushiBar/SushiBarDatabaseImplement/Component.cs new file mode 100644 index 0000000..d555489 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Component.cs @@ -0,0 +1,55 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +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 + }; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/ComponentStorage.cs b/SushiBar/SushiBarDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..2175f30 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,81 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + + +namespace SushiBarDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new SushiBarDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new SushiBarDatabase(); + return context.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new SushiBarDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new SushiBarDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new SushiBarDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} + diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_InitialCreate.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_InitialCreate.Designer.cs new file mode 100644 index 0000000..4062a93 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_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("20240328181724_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/20240328181724_InitialCreate.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_InitialCreate.cs new file mode 100644 index 0000000..4367175 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240328181724_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/Order.cs b/SushiBar/SushiBarDatabaseImplement/Order.cs new file mode 100644 index 0000000..caec16c --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Order.cs @@ -0,0 +1,69 @@ +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; } + + [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, + }; + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..7e572a6 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/OrderStorage.cs @@ -0,0 +1,79 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new SushiBarDatabase(); + return context.Orders.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; + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/Sushi.cs b/SushiBar/SushiBarDatabaseImplement/Sushi.cs new file mode 100644 index 0000000..85050d5 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/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/SushiBarDatatbase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs new file mode 100644 index 0000000..7f96273 --- /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=DESKTOP-VPL3HCO\SQLEXPRESS;Initial Catalog=SushiBarDatabaseFull;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; } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/SushiComponent.cs b/SushiBar/SushiBarDatabaseImplement/SushiComponent.cs new file mode 100644 index 0000000..965f511 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiComponent.cs @@ -0,0 +1,16 @@ +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(); + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarDatabaseImplement/SushiStorage.cs b/SushiBar/SushiBarDatabaseImplement/SushiStorage.cs new file mode 100644 index 0000000..7c99bf7 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiStorage.cs @@ -0,0 +1,94 @@ +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; + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarView/SushiBarView.csproj b/SushiBar/SushiBarView/SushiBarView.csproj index eab506f..6f09278 100644 --- a/SushiBar/SushiBarView/SushiBarView.csproj +++ b/SushiBar/SushiBarView/SushiBarView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + -- 2.25.1 From d402b224edfde7842d8025899e4d5b26b9a8ddb0 Mon Sep 17 00:00:00 2001 From: tellsense Date: Thu, 28 Mar 2024 23:13:24 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs index 7f96273..3a040ab 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs @@ -14,7 +14,7 @@ namespace SushiBarDatabaseImplement { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-VPL3HCO\SQLEXPRESS;Initial Catalog=SushiBarDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SushiBarDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } -- 2.25.1 From 9df304f90e295145aef6b5c01d75302a7416473e Mon Sep 17 00:00:00 2001 From: tellsense Date: Thu, 11 Apr 2024 19:48:02 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{SushiBarDatatbase.cs => SushiBarDatabase.cs} | 0 SushiBar/SushiBarView/FormCreateOrder.cs | 5 +---- 2 files changed, 1 insertion(+), 4 deletions(-) rename SushiBar/SushiBarDatabaseImplement/{SushiBarDatatbase.cs => SushiBarDatabase.cs} (100%) diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs similarity index 100% rename from SushiBar/SushiBarDatabaseImplement/SushiBarDatatbase.cs rename to SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs diff --git a/SushiBar/SushiBarView/FormCreateOrder.cs b/SushiBar/SushiBarView/FormCreateOrder.cs index 0704e32..5c33ec2 100644 --- a/SushiBar/SushiBarView/FormCreateOrder.cs +++ b/SushiBar/SushiBarView/FormCreateOrder.cs @@ -22,10 +22,7 @@ namespace SushiBarView private readonly ISushiLogic _logicP; private readonly IOrderLogic _logicO; private List? _list; - public FormCreateOrder() - { - InitializeComponent(); - } + public FormCreateOrder(ILogger logger, ISushiLogic logicP, IOrderLogic logicO) { InitializeComponent(); -- 2.25.1