From ae15e998293a1e27ceab2d8946fbaf1668b68984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=9F=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Mon, 11 Mar 2024 10:36:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=203=20(=D0=B1=D0=B0=D0=B7=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FoodOrders/FoodOrders.sln | 6 + .../FoodOrdersDatabase.cs | 27 +++ .../FoodOrdersDatabaseImplement.csproj | 27 +++ .../Implemets/ComponentStorage.cs | 84 +++++++++ .../Implemets/DishStorage.cs | 132 ++++++++++++++ .../Implemets/OrderStorage.cs | 85 +++++++++ .../20240311061233_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240311061233_InitialCreate.cs | 125 +++++++++++++ .../FoodOrdersDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 69 +++++++ .../Models/Dish.cs | 100 ++++++++++ .../Models/DishComponent.cs | 22 +++ .../Models/Order.cs | 73 ++++++++ .../FoodOrdersView/FoodOrdersView.csproj | 5 + FoodOrders/FoodOrdersView/Program.cs | 2 +- 15 files changed, 1095 insertions(+), 1 deletion(-) create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabaseImplement.csproj create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Implemets/ComponentStorage.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Implemets/DishStorage.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Implemets/OrderStorage.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.Designer.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Migrations/FoodOrdersDatabaseModelSnapshot.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/Component.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/DishComponent.cs create mode 100644 FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs diff --git a/FoodOrders/FoodOrders.sln b/FoodOrders/FoodOrders.sln index aa16ffc..274c2d0 100644 --- a/FoodOrders/FoodOrders.sln +++ b/FoodOrders/FoodOrders.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersListImplement", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersFileImplement", "FoodOrdersFileImplement\FoodOrdersFileImplement.csproj", "{92F623B7-C680-4A3A-9749-26E87628A02E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersDatabaseImplement", "FoodOrdersDatabaseImplement\FoodOrdersDatabaseImplement.csproj", "{2190363C-BB1A-4955-B88F-C5F516D7A758}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {92F623B7-C680-4A3A-9749-26E87628A02E}.Debug|Any CPU.Build.0 = Debug|Any CPU {92F623B7-C680-4A3A-9749-26E87628A02E}.Release|Any CPU.ActiveCfg = Release|Any CPU {92F623B7-C680-4A3A-9749-26E87628A02E}.Release|Any CPU.Build.0 = Release|Any CPU + {2190363C-BB1A-4955-B88F-C5F516D7A758}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2190363C-BB1A-4955-B88F-C5F516D7A758}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2190363C-BB1A-4955-B88F-C5F516D7A758}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2190363C-BB1A-4955-B88F-C5F516D7A758}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs new file mode 100644 index 0000000..6d47ff1 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabase.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FoodOrdersDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FoodOrdersDatabaseImplement +{ + public class FoodOrdersDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=FoodOrdersDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Dishs { set; get; } + public virtual DbSet DishComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabaseImplement.csproj b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabaseImplement.csproj new file mode 100644 index 0000000..68fc1c5 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/FoodOrdersDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + ..\FoodOrdersContracts\obj\Debug\net6.0\ref\FoodOrdersContracts.dll + + + ..\FoodOrdersDataModels\obj\Debug\net6.0\ref\FoodOrdersDataModels.dll + + + + diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Implemets/ComponentStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/ComponentStorage.cs new file mode 100644 index 0000000..f623455 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/ComponentStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDatabaseImplement.Models; + +namespace FoodOrdersDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FoodOrdersDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FoodOrdersDatabase(); + 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 FoodOrdersDatabase(); + 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 FoodOrdersDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FoodOrdersDatabase(); + 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 FoodOrdersDatabase(); + 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/FoodOrders/FoodOrdersDatabaseImplement/Implemets/DishStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/DishStorage.cs new file mode 100644 index 0000000..82cd344 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/DishStorage.cs @@ -0,0 +1,132 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using System.Xml.Linq; +using FoodOrdersContracts.StoragesContracts; +using System.Runtime.ConstrainedExecution; + +namespace FoodOrdersDatabaseImplement.Implements +{ + public class DishStorage : IDishStorage + { + public List GetFullList() + { + using var context = new FoodOrdersDatabase(); + return context.Dishs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(DishSearchModel model) + { + if (string.IsNullOrEmpty(model.DishName)) + { + return new(); + } + using var context = new FoodOrdersDatabase(); + return context.Dishs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.DishName.Contains(model.DishName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public DishViewModel? GetElement(DishSearchModel model) + { + if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue) + { + return null; + } + using var context = new FoodOrdersDatabase(); + return context.Dishs.Include( + x => x.Components + ).ThenInclude(x => x.Component).FirstOrDefault(x => ( + !string.IsNullOrEmpty(model.DishName) && + x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public DishViewModel? Insert(DishBindingModel model) + { + using var context = new FoodOrdersDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newDish = Dish.Create(context, model); + if (newDish == null) + { + transaction.Rollback(); + return null; + } + context.Dishs.Add(newDish); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newDish.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public DishViewModel? Update(DishBindingModel model) + { + using var context = new FoodOrdersDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var Dish = context.Dishs.FirstOrDefault(x => x.Id == model.Id); + if (Dish == null) + { + transaction.Rollback(); + return null; + } + + Dish.Update(model); + Dish.UpdateComponents(context, model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return Dish.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public DishViewModel? Delete(DishBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var element = context.Dishs.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Dishs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Implemets/OrderStorage.cs b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/OrderStorage.cs new file mode 100644 index 0000000..a87eac7 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Implemets/OrderStorage.cs @@ -0,0 +1,85 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FoodOrdersDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FoodOrdersDatabase(); + return context.Orders.Include(x => x.Dish).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 FoodOrdersDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Dish) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new FoodOrdersDatabase(); + return context.Orders.Include(x => x.Dish).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new FoodOrdersDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Dish).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FoodOrdersDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.Designer.cs b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.Designer.cs new file mode 100644 index 0000000..f857eae --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using FoodOrdersDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FoodOrdersDatabaseImplement.Migrations +{ + [DbContext(typeof(FoodOrdersDatabase))] + [Migration("20240311061233_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.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("FoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DishName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dishs"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DishId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DishId"); + + b.ToTable("DishComponents"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.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("DishId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DishId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.HasOne("FoodOrdersDatabaseImplement.Models.Component", "Component") + .WithMany("DishComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Components") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Order", b => + { + b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Orders") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Component", b => + { + b.Navigation("DishComponents"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.cs b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.cs new file mode 100644 index 0000000..1697e97 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/20240311061233_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FoodOrdersDatabaseImplement.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: "Dishs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Dishs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DishComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishId = 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_DishComponents", x => x.Id); + table.ForeignKey( + name: "FK_DishComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DishComponents_Dishs_DishId", + column: x => x.DishId, + principalTable: "Dishs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DishId = 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_Dishs_DishId", + column: x => x.DishId, + principalTable: "Dishs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DishComponents_ComponentId", + table: "DishComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_DishComponents_DishId", + table: "DishComponents", + column: "DishId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DishId", + table: "Orders", + column: "DishId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DishComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Dishs"); + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Migrations/FoodOrdersDatabaseModelSnapshot.cs b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/FoodOrdersDatabaseModelSnapshot.cs new file mode 100644 index 0000000..ef235de --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Migrations/FoodOrdersDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using FoodOrdersDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FoodOrdersDatabaseImplement.Migrations +{ + [DbContext(typeof(FoodOrdersDatabase))] + partial class FoodOrdersDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.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("FoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DishName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dishs"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DishId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DishId"); + + b.ToTable("DishComponents"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.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("DishId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DishId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.HasOne("FoodOrdersDatabaseImplement.Models.Component", "Component") + .WithMany("DishComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Components") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Order", b => + { + b.HasOne("FoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Orders") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Component", b => + { + b.Navigation("DishComponents"); + }); + + modelBuilder.Entity("FoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/Component.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..95148a6 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Component.cs @@ -0,0 +1,69 @@ +using FoodOrdersDataModels.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 FoodOrdersContracts.ViewModels; +using FoodOrdersContracts.BindingModels; + +namespace FoodOrdersDatabaseImplement.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 DishComponents { 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/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs new file mode 100644 index 0000000..f889698 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using FoodOrdersDataModels.Models; +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; + +namespace FoodOrdersDatabaseImplement.Models +{ + public class Dish : IDishModel + { + public int Id { get; private set; } + [Required] + public string DishName { get; private set; } = string.Empty; + [Required] + public double Price { get; private set; } + + private Dictionary? _dishComponents = null; + + [NotMapped] + public Dictionary DishComponents + { + get + { + if (_dishComponents == null) + { + _dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _dishComponents; + } + } + + [ForeignKey("DishId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("DishId")] + public virtual List Orders { get; set; } = new(); + + public static Dish? Create(FoodOrdersDatabase context, DishBindingModel model) + { + var components = context.Components; + return new Dish() + { + Id = model.Id, + DishName = model.DishName, + Price = model.Price, + Components = model.DishComponents.Select(x => new DishComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(DishBindingModel model) + { + DishName = model.DishName; + Price = model.Price; + } + public DishViewModel GetViewModel => new() + { + Id = Id, + DishName = DishName, + Price = Price, + DishComponents = DishComponents + }; + + public void UpdateComponents(FoodOrdersDatabase context, DishBindingModel model) + { + var dishComponents = context.DishComponents.Where(rec => rec.DishId == model.Id).ToList(); + if (dishComponents != null && DishComponents.Count > 0) + { // удалили те, которых нет в модели + context.DishComponents.RemoveRange(dishComponents.Where(rec => !model.DishComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in dishComponents) + { + updateComponent.Count = model.DishComponents[updateComponent.ComponentId].Item2; + model.DishComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var dish = context.Dishs.First(x => x.Id == Id); + foreach (var pc in model.DishComponents) + { + context.DishComponents.Add(new DishComponent + { + Dish = dish, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _dishComponents = null; + } + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/DishComponent.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/DishComponent.cs new file mode 100644 index 0000000..d9701c1 --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/DishComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + +namespace FoodOrdersDatabaseImplement.Models +{ + public class DishComponent + { + public int Id { get; set; } + [Required] + public int DishId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Dish Dish { get; set; } = new(); + } +} diff --git a/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs b/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..a950d3e --- /dev/null +++ b/FoodOrders/FoodOrdersDatabaseImplement/Models/Order.cs @@ -0,0 +1,73 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDataModels.Enums; +using FoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace FoodOrdersDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DishId { get; private set; } + + [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 virtual Dish Dish { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + DishId = model.DishId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + DishId = DishId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + DishName = Dish.DishName + }; + } +} diff --git a/FoodOrders/FoodOrdersView/FoodOrdersView.csproj b/FoodOrders/FoodOrdersView/FoodOrdersView.csproj index eccd4be..2cf25b8 100644 --- a/FoodOrders/FoodOrdersView/FoodOrdersView.csproj +++ b/FoodOrders/FoodOrdersView/FoodOrdersView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/FoodOrders/FoodOrdersView/Program.cs b/FoodOrders/FoodOrdersView/Program.cs index 3eb69c5..731ce95 100644 --- a/FoodOrders/FoodOrdersView/Program.cs +++ b/FoodOrders/FoodOrdersView/Program.cs @@ -1,7 +1,7 @@ using FoodOrdersBusinessLogic.BusinessLogics; using FoodOrdersContracts.BusinessLogicsContracts; using FoodOrdersContracts.StoragesContracts; -using FoodOrdersFileImplement.Implements; +using FoodOrdersDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;