From 36721c9489f05d5ea24a37f13cc6db68e77a77df Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 14 Mar 2023 08:45:31 +0400 Subject: [PATCH] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pizzeria/Pizzeria.sln | 8 +- Pizzeria/Pizzeria/FormMain.cs | 2 +- Pizzeria/Pizzeria/Pizzeria.csproj | 6 +- Pizzeria/Pizzeria/Program.cs | 2 +- Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../Implements/ComponentStorage.cs | 83 +++++++++ .../Implements/OrderStorage.cs | 72 ++++++++ .../Implements/PizzaStorage.cs | 104 +++++++++++ .../20230228062848_Init.Designer.cs | 172 ++++++++++++++++++ .../Migrations/20230228062848_Init.cs | 126 +++++++++++++ ...230228065928_Change constraint.Designer.cs | 171 +++++++++++++++++ .../20230228065928_Change constraint.cs | 37 ++++ .../PizzeriaDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../PizzeriaDatabase.cs | 21 +++ .../PizzeriaDatabaseImplement.csproj | 24 +++ .../models/Component.cs | 57 ++++++ .../PizzeriaDatabaseImplement/models/Order.cs | 76 ++++++++ .../PizzeriaDatabaseImplement/models/Pizza.cs | 97 ++++++++++ .../models/PizzaComponent.cs | 17 ++ 20 files changed, 1241 insertions(+), 6 deletions(-) create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.Designer.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.Designer.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/Component.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/Order.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/Pizza.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/models/PizzaComponent.cs diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 1f6ef81..6b98332 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{138A775B-55CA-4120-918F-6093C0B2B241}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaShopFileImplement\PizzeriaFileImplement.csproj", "{44B31B6F-2852-454B-9E7D-E965C9A348ED}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaShopFileImplement\PizzeriaFileImplement.csproj", "{44B31B6F-2852-454B-9E7D-E965C9A348ED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{37411F99-74EC-476C-BE13-D20015ED52B2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Debug|Any CPU.Build.0 = Debug|Any CPU {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Release|Any CPU.ActiveCfg = Release|Any CPU {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Release|Any CPU.Build.0 = Release|Any CPU + {37411F99-74EC-476C-BE13-D20015ED52B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37411F99-74EC-476C-BE13-D20015ED52B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37411F99-74EC-476C-BE13-D20015ED52B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37411F99-74EC-476C-BE13-D20015ED52B2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/Pizzeria/FormMain.cs b/Pizzeria/Pizzeria/FormMain.cs index 1f13dba..cf5652d 100644 --- a/Pizzeria/Pizzeria/FormMain.cs +++ b/Pizzeria/Pizzeria/FormMain.cs @@ -73,7 +73,7 @@ namespace Pizzeria Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), PizzaId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["PizzaId"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + DateCreate = DateTime.SpecifyKind(DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), DateTimeKind.Utc), }; } private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) diff --git a/Pizzeria/Pizzeria/Pizzeria.csproj b/Pizzeria/Pizzeria/Pizzeria.csproj index ea801bb..50dc696 100644 --- a/Pizzeria/Pizzeria/Pizzeria.csproj +++ b/Pizzeria/Pizzeria/Pizzeria.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,7 +20,7 @@ - + diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index ec55503..c1b9b43 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -1,7 +1,7 @@ using PizzeriaBusinessLogic; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaFileImplement.Implements; +using PizzeriaDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs index f071a3f..7c2c9e5 100644 --- a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs @@ -63,7 +63,7 @@ namespace PizzeriaBusinessLogic if(newStatus == OrderStatus.Выдан) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); } if (_orderStorage.Update(model) == null) diff --git a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs index a90b43a..877567f 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs @@ -14,7 +14,7 @@ namespace PizzeriaContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..39be96d --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,83 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.models; +namespace PizzeriaDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + 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 PizzeriaDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..d5b4c63 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,72 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.models; +using Microsoft.EntityFrameworkCore; + + +namespace PizzeriaDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Orders + .Include(x => x.Pizza) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + using var context = new PizzeriaDatabase(); + return context.Orders + .Where(x => (model.Id.HasValue && x.Id == model.Id)) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + using var context = new PizzeriaDatabase(); + return context.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + Order newOrder = Order.Create(context, model); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new PizzeriaDatabase(); + 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 PizzeriaDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs new file mode 100644 index 0000000..d1e8f0a --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/PizzaStorage.cs @@ -0,0 +1,104 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.models; +using Microsoft.EntityFrameworkCore; +namespace PizzeriaDatabaseImplement.Implements +{ + public class PizzaStorage : IPizzaStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Pizzas + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Pizzas + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PizzaName.Contains(model.PizzaName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public PizzaViewModel? GetElement(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName) && + !model.Id.HasValue) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Pizzas + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PizzaName) && + x.PizzaName == model.PizzaName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PizzaViewModel? Insert(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var newPizza = Pizza.Create(context, model); + if (newPizza == null) + { + return null; + } + context.Pizzas.Add(newPizza); + context.SaveChanges(); + return newPizza.GetViewModel; + } + public PizzaViewModel? Update(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var pizza = context.Pizzas.FirstOrDefault(rec => + rec.Id == model.Id); + if (pizza == null) + { + return null; + } + pizza.Update(model); + context.SaveChanges(); + pizza.UpdateComponents(context, model); + transaction.Commit(); + return pizza.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PizzaViewModel? Delete(PizzaBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Pizzas + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pizzas.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.Designer.cs new file mode 100644 index 0000000..54ee90b --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.Designer.cs @@ -0,0 +1,172 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20230228062848_Init")] + partial class Init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .IsRequired() + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.cs new file mode 100644 index 0000000..2c337b9 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228062848_Init.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class Init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Pizzas", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PizzaName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pizzas", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PizzaId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PizzaComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PizzaId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PizzaComponents", x => x.Id); + table.ForeignKey( + name: "FK_PizzaComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PizzaComponents_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PizzaId", + table: "Orders", + column: "PizzaId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_ComponentId", + table: "PizzaComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PizzaComponents_PizzaId", + table: "PizzaComponents", + column: "PizzaId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PizzaComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pizzas"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.Designer.cs new file mode 100644 index 0000000..de9282c --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20230228065928_Change constraint")] + partial class Changeconstraint + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.cs new file mode 100644 index 0000000..1602208 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230228065928_Change constraint.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class Changeconstraint : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "DateImplement", + table: "Orders", + type: "timestamp with time zone", + nullable: true, + oldClrType: typeof(DateTime), + oldType: "timestamp with time zone"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "DateImplement", + table: "Orders", + type: "timestamp with time zone", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + oldClrType: typeof(DateTime), + oldType: "timestamp with time zone", + oldNullable: true); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs new file mode 100644 index 0000000..ae2eb4b --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + partial class PizzeriaDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PizzaId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs new file mode 100644 index 0000000..d7d0ce0 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -0,0 +1,21 @@ +using PizzeriaDatabaseImplement.models; +using Microsoft.EntityFrameworkCore; + +namespace PizzeriaDatabaseImplement +{ + public class PizzeriaDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=PizzeriaDatabase;Username=postgres;Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pizzas { set; get; } + public virtual DbSet PizzaComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj new file mode 100644 index 0000000..8d8372c --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/Component.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Component.cs new file mode 100644 index 0000000..74ce8a5 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Component.cs @@ -0,0 +1,57 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PizzeriaDatabaseImplement.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 PizzaComponents { 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/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs new file mode 100644 index 0000000..37c386f --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Order.cs @@ -0,0 +1,76 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.models +{ + public class Order : IOrderModel + { + [Required] + public int PizzaId { 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 Pizza Pizza { get; set; } = new(); + + public int Id { get; private set; } + + public static Order? Create(PizzeriaDatabase context, OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PizzaId = model.PizzaId, + Pizza = context.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId), + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + PizzaId = model.PizzaId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PizzaId = PizzaId, + PizzaName = Pizza.PizzaName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/models/Pizza.cs new file mode 100644 index 0000000..cc9137e --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/Pizza.cs @@ -0,0 +1,97 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + + +namespace PizzeriaDatabaseImplement.models +{ + public class Pizza : IPizzaModel + { + public int Id { get; set; } + [Required] + public string PizzaName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _pizzaComponents = + null; + [NotMapped] + public Dictionary PizzaComponents + { + get + { + if (_pizzaComponents == null) + { + _pizzaComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pizzaComponents; + } + } + [ForeignKey("PizzaId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PizzaId")] + public virtual List Orders { get; set; } = new(); + public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model) + { + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.Select(x => new + PizzaComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PizzaBindingModel model) + { + PizzaName = model.PizzaName; + Price = model.Price; + } + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + public void UpdateComponents(PizzeriaDatabase context, + PizzaBindingModel model) + { + var pizzaComponents = context.PizzaComponents.Where(rec => + rec.PizzaId == model.Id).ToList(); + if (pizzaComponents != null && pizzaComponents.Count > 0) + { // удалили те, которых нет в модели + context.PizzaComponents.RemoveRange(pizzaComponents.Where(rec + => !model.PizzaComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in pizzaComponents) + { + updateComponent.Count = + model.PizzaComponents[updateComponent.ComponentId].Item2; + model.PizzaComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var pizza = context.Pizzas.First(x => x.Id == Id); + foreach (var pc in model.PizzaComponents) + { + context.PizzaComponents.Add(new PizzaComponent + { + Pizza = pizza, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pizzaComponents = null; + } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/models/PizzaComponent.cs b/Pizzeria/PizzeriaDatabaseImplement/models/PizzaComponent.cs new file mode 100644 index 0000000..0059fe4 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/models/PizzaComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.models +{ + public class PizzaComponent + { + public int Id { get; set; } + [Required] + public int PizzaId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pizza Pizza { get; set; } = new(); + } +}