From 65b5f5e0fb32e1dae44f764df03d8012a94506b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Mon, 25 Mar 2024 22:59:49 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= =?UTF-8?q?=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Confectionery.sln | 6 + .../ConfectioneryDatabase.cs | 26 +++ .../ConfectioneryDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 92 ++++++++++ .../Implements/OrderStorage.cs | 97 ++++++++++ .../Implements/PastryStorage.cs | 111 ++++++++++++ .../20240325181336_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240325181336_InitialCreate.cs | 125 +++++++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Order.cs | 64 +++++++ .../Models/Pastry.cs | 98 ++++++++++ .../Models/PastryComponent.cs | 23 +++ .../Implements/PastryStorage.cs | 8 +- .../ConfectioneryView.csproj | 5 + Confectionery/ConfectioneryView/Program.cs | 2 +- 16 files changed, 1075 insertions(+), 5 deletions(-) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index b3a2288..97044be 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Release|Any CPU.Build.0 = Release|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..9682e7f --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.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 ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pastries { set; get; } + public virtual DbSet PastryComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj new file mode 100644 index 0000000..854a6f5 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..159cd99 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Components.Web; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..51b041e --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs new file mode 100644 index 0000000..9783cb9 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList() + .Select(x => x.GetViewModel).ToList(); + } + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && + x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + context.Pastries.Add(newPastry); + context.SaveChanges(); + return newPastry.GetViewModel; + } + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Pastries.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs new file mode 100644 index 0000000..82ef147 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20240325181336_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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs new file mode 100644 index 0000000..d633f68 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.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: "Pastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastries", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PastryComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_PastryComponents", x => x.Id); + table.ForeignKey( + name: "FK_PastryComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PastryComponents_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PastryId", + table: "Orders", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_ComponentId", + table: "PastryComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_PastryId", + table: "PastryComponents", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PastryComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pastries"); + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..e7c13d9 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + partial class ConfectioneryDatabaseModelSnapshot : 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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..1e36015 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using ConfectioneryContracts.BindingModels; +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 ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryDatabaseImplement.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 ProductComponents { 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/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..619f7b1 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -0,0 +1,64 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public virtual Pastry Pastry { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PastryId = model.PastryId, + 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, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + PastryName = Pastry.PastryName + }; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs new file mode 100644 index 0000000..386f279 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs @@ -0,0 +1,98 @@ +using ConfectioneryDataModels.Models; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; set; } + [Required] + public string PastryName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _pastryComponents = null; + [NotMapped] + public Dictionary PastryComponents + { + get + { + if (_pastryComponents == null) + { + _pastryComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pastryComponents; + } + } + [ForeignKey("PastryId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Orders { get; set; } = new(); + public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) + { + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.Select(x => new + PastryComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PastryBindingModel model) + { + PastryName = model.PastryName; + Price = model.Price; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + + public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model) + { + var pastryComponents = context.PastryComponents.Where(rec => rec.PastryId == model.Id).ToList(); + if (pastryComponents != null && pastryComponents.Count > 0) + { // удалили те, которых нет в модели + context.PastryComponents.RemoveRange(pastryComponents.Where(rec => !model.PastryComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in pastryComponents) + { + updateComponent.Count = + model.PastryComponents[updateComponent.ComponentId].Item2; + model.PastryComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var pastry = context.Pastries.First(x => x.Id == Id); + foreach (var pc in model.PastryComponents) + { + context.PastryComponents.Add(new PastryComponent + { + Pastry = pastry, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pastryComponents = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs new file mode 100644 index 0000000..52d782a --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class PastryComponent + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + + } +} diff --git a/Confectionery/ConfectioneryFileImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/PastryStorage.cs index 582638e..94657af 100644 --- a/Confectionery/ConfectioneryFileImplement/Implements/PastryStorage.cs +++ b/Confectionery/ConfectioneryFileImplement/Implements/PastryStorage.cs @@ -66,14 +66,14 @@ namespace ConfectioneryFileImplement.Implements public PastryViewModel? Update(PastryBindingModel model) { - var iceCream = source.Pastries.FirstOrDefault(x => x.Id == model.Id); - if (iceCream == null) + var pastry = source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) { return null; } - iceCream.Update(model); + pastry.Update(model); source.SavePastries(); - return iceCream.GetViewModel; + return pastry.GetViewModel; } public PastryViewModel? Delete(PastryBindingModel model) diff --git a/Confectionery/ConfectioneryView/ConfectioneryView.csproj b/Confectionery/ConfectioneryView/ConfectioneryView.csproj index c9f4ff9..2330b6d 100644 --- a/Confectionery/ConfectioneryView/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView/ConfectioneryView.csproj @@ -15,6 +15,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -26,6 +30,7 @@ + diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 22cffa1..3653f8d 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From f14a4ae2e17b15e290dbfa9943021f21346164a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 27 Mar 2024 09:27:41 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=BE=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/ConfectioneryView/FormCreateOrder.cs | 4 ++-- Confectionery/ConfectioneryView/FormPastries.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Confectionery/ConfectioneryView/FormCreateOrder.cs b/Confectionery/ConfectioneryView/FormCreateOrder.cs index a448d9b..b903b1b 100644 --- a/Confectionery/ConfectioneryView/FormCreateOrder.cs +++ b/Confectionery/ConfectioneryView/FormCreateOrder.cs @@ -29,7 +29,7 @@ namespace ConfectioneryView private void FormCreateOrder_Load(object sender, EventArgs e) { - _logger.LogInformation("Loading ice cream for order"); + _logger.LogInformation("Loading pastry for order"); try { var pastryList = _logicP.ReadList(null); @@ -43,7 +43,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Error during loading ice cream for order"); + _logger.LogError(ex, "Error during loading pastry for order"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/Confectionery/ConfectioneryView/FormPastries.cs b/Confectionery/ConfectioneryView/FormPastries.cs index 7932f5b..508f6cc 100644 --- a/Confectionery/ConfectioneryView/FormPastries.cs +++ b/Confectionery/ConfectioneryView/FormPastries.cs @@ -86,7 +86,7 @@ namespace ConfectioneryView if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Deletion of ice cream"); + _logger.LogInformation("Deletion of pastry"); try { if (!_logic.Delete(new PastryBindingModel { Id = id })) @@ -97,7 +97,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Ice cream deletion error"); + _logger.LogError(ex, "Pastry deletion error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } -- 2.25.1 From 8ec0755e6f4d34b7c77cd0a4c94672f247f5f52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Mon, 25 Mar 2024 22:59:49 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= =?UTF-8?q?=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Confectionery.sln | 6 + .../ConfectioneryDatabase.cs | 26 +++ .../ConfectioneryDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 92 ++++++++++ .../Implements/OrderStorage.cs | 97 ++++++++++ .../Implements/PastryStorage.cs | 111 ++++++++++++ .../20240325181336_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240325181336_InitialCreate.cs | 125 +++++++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Order.cs | 64 +++++++ .../Models/Pastry.cs | 98 ++++++++++ .../Models/PastryComponent.cs | 23 +++ .../ConfectioneryView.csproj | 5 + Confectionery/ConfectioneryView/Program.cs | 2 +- 15 files changed, 1071 insertions(+), 1 deletion(-) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index b3a2288..97044be 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {0607BCF8-D685-49A9-9C16-76BD0E8BAAA7}.Release|Any CPU.Build.0 = Release|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03D1ACA7-5F94-4B79-B558-7D5DADF11E1E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..9682e7f --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.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 ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pastries { set; get; } + public virtual DbSet PastryComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj new file mode 100644 index 0000000..854a6f5 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..159cd99 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Components.Web; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..51b041e --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Pastry) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs new file mode 100644 index 0000000..9783cb9 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList() + .Select(x => x.GetViewModel).ToList(); + } + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && + x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + context.Pastries.Add(newPastry); + context.SaveChanges(); + return newPastry.GetViewModel; + } + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Pastries.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs new file mode 100644 index 0000000..82ef147 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20240325181336_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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs new file mode 100644 index 0000000..d633f68 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.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: "Pastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastries", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PastryComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_PastryComponents", x => x.Id); + table.ForeignKey( + name: "FK_PastryComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PastryComponents_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PastryId", + table: "Orders", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_ComponentId", + table: "PastryComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_PastryId", + table: "PastryComponents", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PastryComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pastries"); + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..e7c13d9 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + partial class ConfectioneryDatabaseModelSnapshot : 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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..1e36015 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using ConfectioneryContracts.BindingModels; +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 ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryDatabaseImplement.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 ProductComponents { 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/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..619f7b1 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -0,0 +1,64 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public virtual Pastry Pastry { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PastryId = model.PastryId, + 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, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + PastryName = Pastry.PastryName + }; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs new file mode 100644 index 0000000..386f279 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs @@ -0,0 +1,98 @@ +using ConfectioneryDataModels.Models; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; set; } + [Required] + public string PastryName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _pastryComponents = null; + [NotMapped] + public Dictionary PastryComponents + { + get + { + if (_pastryComponents == null) + { + _pastryComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pastryComponents; + } + } + [ForeignKey("PastryId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Orders { get; set; } = new(); + public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) + { + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.Select(x => new + PastryComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PastryBindingModel model) + { + PastryName = model.PastryName; + Price = model.Price; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + + public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model) + { + var pastryComponents = context.PastryComponents.Where(rec => rec.PastryId == model.Id).ToList(); + if (pastryComponents != null && pastryComponents.Count > 0) + { // удалили те, которых нет в модели + context.PastryComponents.RemoveRange(pastryComponents.Where(rec => !model.PastryComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in pastryComponents) + { + updateComponent.Count = + model.PastryComponents[updateComponent.ComponentId].Item2; + model.PastryComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var pastry = context.Pastries.First(x => x.Id == Id); + foreach (var pc in model.PastryComponents) + { + context.PastryComponents.Add(new PastryComponent + { + Pastry = pastry, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pastryComponents = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs new file mode 100644 index 0000000..52d782a --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/PastryComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class PastryComponent + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + + } +} diff --git a/Confectionery/ConfectioneryView/ConfectioneryView.csproj b/Confectionery/ConfectioneryView/ConfectioneryView.csproj index c9f4ff9..2330b6d 100644 --- a/Confectionery/ConfectioneryView/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView/ConfectioneryView.csproj @@ -15,6 +15,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -26,6 +30,7 @@ + diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 22cffa1..3653f8d 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From cd826c1b51ab1cb20a84579aa3393aacf054f88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Fri, 21 Jun 2024 12:12:48 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B4=D0=B0=D0=B9=20=D0=B1=D0=BE=D0=B3=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryDatabase.cs | 4 +- .../Implements/ShopStorage.cs | 151 ++++++++++++++++++ ... 20240621080649_InitialCreate.Designer.cs} | 79 ++++++++- ...ate.cs => 20240621080649_InitialCreate.cs} | 59 +++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 77 +++++++++ .../Models/Shop.cs | 115 +++++++++++++ .../Models/ShopPastry.cs | 27 ++++ 7 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs rename Confectionery/ConfectioneryDatabaseImplement/Migrations/{20240325181336_InitialCreate.Designer.cs => 20240621080649_InitialCreate.Designer.cs} (68%) rename Confectionery/ConfectioneryDatabaseImplement/Migrations/{20240325181336_InitialCreate.cs => 20240621080649_InitialCreate.cs} (66%) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 9682e7f..aedd294 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -14,7 +14,7 @@ namespace ConfectioneryDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase3hard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -22,5 +22,7 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastries { set; get; } public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPastries { set; get; } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..71363af --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdatePastries(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Shops + .Include(x => x.Pastries) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool Sale(IPastryModel model, int count) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry) + .Where(x => x.Pastries.Any(x => x.PastryId == model.Id)) + .ToList()) + { + var pastry = shop.ShopPastries[model.Id]; + int min = Math.Min(pastry.Item2, count); + if (min == pastry.Item2) + { + shop.ShopPastries.Remove(model.Id); + } + else + { + shop.ShopPastries[model.Id] = (pastry.Item1, pastry.Item2 - min); + } + shop.UpdatePastries(context, new() { Id = shop.Id, ShopPastries = shop.ShopPastries }); + count -= min; + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs similarity index 68% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs index 82ef147..668be46 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ConfectioneryDatabaseImplement.Migrations { [DbContext(typeof(ConfectioneryDatabase))] - [Migration("20240325181336_InitialCreate")] + [Migration("20240621080649_InitialCreate")] partial class InitialCreate { /// @@ -124,6 +124,59 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("PastriesMaximum") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") @@ -154,6 +207,25 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Pastry"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => { b.Navigation("ProductComponents"); @@ -165,6 +237,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); #pragma warning restore 612, 618 } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs similarity index 66% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs index d633f68..df49625 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs @@ -39,6 +39,22 @@ namespace ConfectioneryDatabaseImplement.Migrations table.PrimaryKey("PK_Pastries", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + PastriesMaximum = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -90,6 +106,33 @@ namespace ConfectioneryDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopPastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + PastryId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPastries", x => x.Id); + table.ForeignKey( + name: "FK_ShopPastries_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_Orders_PastryId", table: "Orders", @@ -104,6 +147,16 @@ namespace ConfectioneryDatabaseImplement.Migrations name: "IX_PastryComponents_PastryId", table: "PastryComponents", column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastries", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastries", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace ConfectioneryDatabaseImplement.Migrations migrationBuilder.DropTable( name: "PastryComponents"); + migrationBuilder.DropTable( + name: "ShopPastries"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Pastries"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index e7c13d9..08ba109 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("PastriesMaximum") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") @@ -151,6 +204,25 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Pastry"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => { b.Navigation("ProductComponents"); @@ -162,6 +234,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); #pragma warning restore 612, 618 } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..04f35fc --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.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; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + [Required] + public int PastriesMaximum { get; set; } + + private Dictionary? _shopPastries = null; + + [NotMapped] + public Dictionary ShopPastries + { + get + { + if (_shopPastries == null) + { + _shopPastries = Pastries + .ToDictionary(x => x.PastryId, x => (x.Pastry as IPastryModel, x.Count)); + } + return _shopPastries; + } + } + + [ForeignKey("ShopId")] + public virtual List Pastries { get; set; } = new(); + + public static Shop Create(ConfectioneryDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + PastriesMaximum = model.PastriesMaximum, + Pastries = model.ShopPastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + PastriesMaximum = model.PastriesMaximum; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + PastriesMaximum = PastriesMaximum, + ShopPastries = ShopPastries + }; + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPastries != null && shopPastries.Count > 0) + { + context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId))); + context.SaveChanges(); + foreach (var updatePastry in shopPastries) + { + if (model.ShopPastries.ContainsKey(updatePastry.PastryId)) + { + updatePastry.Count = model.ShopPastries[updatePastry.PastryId].Item2; + model.ShopPastries.Remove(updatePastry.PastryId); + } + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ic in model.ShopPastries) + { + context.ShopPastries.Add(new ShopPastry + { + Shop = shop, + Pastry = context.Pastries.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _shopPastries = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs new file mode 100644 index 0000000..fda1998 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class ShopPastry + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int PastryId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Pastry Pastry { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} -- 2.25.1 From 44b6dfdcb73e7afbad05606601bff41730ef65b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Fri, 21 Jun 2024 20:06:15 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D1=87=D1=83=D1=82=D1=8C=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs index 386f279..f5c61fa 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Pastry.cs @@ -37,6 +37,8 @@ namespace ConfectioneryDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("PastryId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List ShopPastries { get; set; } = new(); public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) { return new Pastry() -- 2.25.1