From 60c17db71a60c98247e30f315710d81b91598a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B8=D0=BD=D0=B0?= <Алина@DESKTOP-PH8VQJA> Date: Tue, 19 Mar 2024 23:37:07 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BD=D0=B8=D1=88=20=D1=82=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=B1=D0=BB=D0=B8=D0=B7=D0=BE=D0=BA...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Typography/Typography.sln | 8 +- .../Implements/ComponentStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 91 ++++++++++ .../Implements/PrintedStorage.cs | 111 ++++++++++++ .../20240319173132_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240319173132_InitialCreate.cs | 125 +++++++++++++ .../TypographyDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 68 +++++++ .../Models/Order.cs | 78 ++++++++ .../Models/Printed.cs | 104 +++++++++++ .../Models/PrintedComponent.cs | 27 +++ .../TypographyDatabase.cs | 30 +++ .../TypographyDatabaseImplement.csproj | 23 +++ Typography/TypographyView/Program.cs | 2 +- .../TypographyView/TypographyView.csproj | 5 + 15 files changed, 1093 insertions(+), 2 deletions(-) create mode 100644 Typography/TypographyDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Typography/TypographyDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Typography/TypographyDatabaseImplement/Implements/PrintedStorage.cs create mode 100644 Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.Designer.cs create mode 100644 Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.cs create mode 100644 Typography/TypographyDatabaseImplement/Migrations/TypographyDatabaseModelSnapshot.cs create mode 100644 Typography/TypographyDatabaseImplement/Models/Component.cs create mode 100644 Typography/TypographyDatabaseImplement/Models/Order.cs create mode 100644 Typography/TypographyDatabaseImplement/Models/Printed.cs create mode 100644 Typography/TypographyDatabaseImplement/Models/PrintedComponent.cs create mode 100644 Typography/TypographyDatabaseImplement/TypographyDatabase.cs create mode 100644 Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj diff --git a/Typography/Typography.sln b/Typography/Typography.sln index 28bc28e..70be705 100644 --- a/Typography/Typography.sln +++ b/Typography/Typography.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypographyBusinessLogic", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypographyDataModels", "TypographyDataModels\TypographyDataModels.csproj", "{75576C57-02E9-4D1D-9A02-D39C1EBE992D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypographyFileImplement", "TypographyFileImplement\TypographyFileImplement.csproj", "{C5D1C1C8-9221-40E9-8F1E-0DD6BB46FC4B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypographyFileImplement", "TypographyFileImplement\TypographyFileImplement.csproj", "{C5D1C1C8-9221-40E9-8F1E-0DD6BB46FC4B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypographyDatabaseImplement", "TypographyDatabaseImplement\TypographyDatabaseImplement.csproj", "{F182BCA6-A1D0-4E0C-8444-4BBF1CD480B2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {C5D1C1C8-9221-40E9-8F1E-0DD6BB46FC4B}.Debug|Any CPU.Build.0 = Debug|Any CPU {C5D1C1C8-9221-40E9-8F1E-0DD6BB46FC4B}.Release|Any CPU.ActiveCfg = Release|Any CPU {C5D1C1C8-9221-40E9-8F1E-0DD6BB46FC4B}.Release|Any CPU.Build.0 = Release|Any CPU + {F182BCA6-A1D0-4E0C-8444-4BBF1CD480B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F182BCA6-A1D0-4E0C-8444-4BBF1CD480B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F182BCA6-A1D0-4E0C-8444-4BBF1CD480B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F182BCA6-A1D0-4E0C-8444-4BBF1CD480B2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Typography/TypographyDatabaseImplement/Implements/ComponentStorage.cs b/Typography/TypographyDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..d394296 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyDatabaseImplement.Models; + +namespace TypographyDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new TypographyDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new TypographyDatabase(); + 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 TypographyDatabase(); + 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 TypographyDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new TypographyDatabase(); + 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 TypographyDatabase(); + 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/Typography/TypographyDatabaseImplement/Implements/OrderStorage.cs b/Typography/TypographyDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7b7d149 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyDatabaseImplement.Models; + +namespace TypographyDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new TypographyDatabase(); + return context.Orders + .Include(x => x.Printed) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (model.Id == null) + { + return new(); + } + using var context = new TypographyDatabase(); + return context.Orders + .Include(x => x.Printed) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new TypographyDatabase(); + return context.Orders + .Include(x => x.Printed) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new TypographyDatabase(); + var newOrder = Order.Create(model, context); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new TypographyDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new TypographyDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Implements/PrintedStorage.cs b/Typography/TypographyDatabaseImplement/Implements/PrintedStorage.cs new file mode 100644 index 0000000..894479b --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Implements/PrintedStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyDatabaseImplement.Models; + +namespace TypographyDatabaseImplement.Implements +{ + public class PrintedStorage : IPrintedStorage + { + public List GetFullList() + { + using var context = new TypographyDatabase(); + return context.Printeds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PrintedSearchModel model) + { + if (string.IsNullOrEmpty(model.PrintedName)) + { + return new(); + } + using var context = new TypographyDatabase(); + return context.Printeds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PrintedName.Contains(model.PrintedName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public PrintedViewModel? GetElement(PrintedSearchModel model) + { + if (string.IsNullOrEmpty(model.PrintedName) && !model.Id.HasValue) + { + return null; + } + using var context = new TypographyDatabase(); + return context.Printeds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PrintedName) && x.PrintedName == model.PrintedName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public PrintedViewModel? Insert(PrintedBindingModel model) + { + using var context = new TypographyDatabase(); + var newPrinted = Printed.Create(context, model); + if (newPrinted == null) + { + return null; + } + context.Printeds.Add(newPrinted); + context.SaveChanges(); + return newPrinted.GetViewModel; + } + + public PrintedViewModel? Update(PrintedBindingModel model) + { + using var context = new TypographyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var printed = context.Printeds.FirstOrDefault(rec => rec.Id == model.Id); + if (printed == null) + { + return null; + } + printed.Update(model); + context.SaveChanges(); + printed.UpdateComponents(context, model); + transaction.Commit(); + return printed.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PrintedViewModel? Delete(PrintedBindingModel model) + { + using var context = new TypographyDatabase(); + var element = context.Printeds + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Printeds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.Designer.cs b/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.Designer.cs new file mode 100644 index 0000000..cd15f0c --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TypographyDatabaseImplement; + +#nullable disable + +namespace TypographyDatabaseImplement.Migrations +{ + [DbContext(typeof(TypographyDatabase))] + [Migration("20240319173132_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TypographyDatabaseImplement.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("TypographyDatabaseImplement.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("PrintedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PrintedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Printed", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("PrintedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Printeds"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.PrintedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PrintedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PrintedId"); + + b.ToTable("PrintedComponents"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Order", b => + { + b.HasOne("TypographyDatabaseImplement.Models.Printed", "Printed") + .WithMany("Orders") + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Printed"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.PrintedComponent", b => + { + b.HasOne("TypographyDatabaseImplement.Models.Component", "Component") + .WithMany("PrintedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TypographyDatabaseImplement.Models.Printed", "Printed") + .WithMany("Components") + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Printed"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Component", b => + { + b.Navigation("PrintedComponents"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Printed", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.cs b/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.cs new file mode 100644 index 0000000..8e8bf1b --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Migrations/20240319173132_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TypographyDatabaseImplement.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: "Printeds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PrintedName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Printeds", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PrintedId = 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_Printeds_PrintedId", + column: x => x.PrintedId, + principalTable: "Printeds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PrintedComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PrintedId = 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_PrintedComponents", x => x.Id); + table.ForeignKey( + name: "FK_PrintedComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PrintedComponents_Printeds_PrintedId", + column: x => x.PrintedId, + principalTable: "Printeds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PrintedId", + table: "Orders", + column: "PrintedId"); + + migrationBuilder.CreateIndex( + name: "IX_PrintedComponents_ComponentId", + table: "PrintedComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PrintedComponents_PrintedId", + table: "PrintedComponents", + column: "PrintedId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PrintedComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Printeds"); + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Migrations/TypographyDatabaseModelSnapshot.cs b/Typography/TypographyDatabaseImplement/Migrations/TypographyDatabaseModelSnapshot.cs new file mode 100644 index 0000000..ee989e3 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Migrations/TypographyDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TypographyDatabaseImplement; + +#nullable disable + +namespace TypographyDatabaseImplement.Migrations +{ + [DbContext(typeof(TypographyDatabase))] + partial class TypographyDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("TypographyDatabaseImplement.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("TypographyDatabaseImplement.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("PrintedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PrintedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Printed", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("PrintedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Printeds"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.PrintedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PrintedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PrintedId"); + + b.ToTable("PrintedComponents"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Order", b => + { + b.HasOne("TypographyDatabaseImplement.Models.Printed", "Printed") + .WithMany("Orders") + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Printed"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.PrintedComponent", b => + { + b.HasOne("TypographyDatabaseImplement.Models.Component", "Component") + .WithMany("PrintedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TypographyDatabaseImplement.Models.Printed", "Printed") + .WithMany("Components") + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Printed"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Component", b => + { + b.Navigation("PrintedComponents"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.Printed", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Models/Component.cs b/Typography/TypographyDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..0b0359d --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Models/Component.cs @@ -0,0 +1,68 @@ +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 TypographyDataModels.Models; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; + +namespace TypographyDatabaseImplement.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 PrintedComponents { 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/Typography/TypographyDatabaseImplement/Models/Order.cs b/Typography/TypographyDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..a524845 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Models/Order.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.Enums; +using TypographyDataModels.Models; + +namespace TypographyDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int PrintedId { get; private set; } + + public virtual Printed Printed { get; set; } = new(); + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model, TypographyDatabase context) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PrintedId = model.PrintedId, + Printed = context.Printeds.First(x => model.PrintedId == x.Id), + 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, + PrintedId = PrintedId, + PrintedName = Printed.PrintedName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/Typography/TypographyDatabaseImplement/Models/Printed.cs b/Typography/TypographyDatabaseImplement/Models/Printed.cs new file mode 100644 index 0000000..63d1a17 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Models/Printed.cs @@ -0,0 +1,104 @@ +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; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.Models; + +namespace TypographyDatabaseImplement.Models +{ + public class Printed : IPrintedModel + { + public int Id { get; set; } + + [Required] + public string PrintedName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _printedComponents = null; + + [NotMapped] + public Dictionary PrintedComponents + { + get + { + if (_printedComponents == null) + { + _printedComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _printedComponents; + } + } + + [ForeignKey("PrintedId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("PrintedId")] + public virtual List Orders { get; set; } = new(); + + public static Printed Create(TypographyDatabase context, PrintedBindingModel model) + { + return new Printed() + { + Id = model.Id, + PrintedName = model.PrintedName, + Price = model.Price, + Components = model.PrintedComponents.Select(x => new PrintedComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(PrintedBindingModel model) + { + PrintedName = model.PrintedName; + Price = model.Price; + } + + public PrintedViewModel GetViewModel => new() + { + Id = Id, + PrintedName = PrintedName, + Price = Price, + PrintedComponents = PrintedComponents + }; + + public void UpdateComponents(TypographyDatabase context, PrintedBindingModel model) + { + var printedComponents = context.PrintedComponents.Where(rec => rec.PrintedId == model.Id).ToList(); + if (printedComponents != null && printedComponents.Count > 0) + { + context.PrintedComponents.RemoveRange(printedComponents.Where(rec => !model.PrintedComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + + foreach (var updateComponent in printedComponents) + { + updateComponent.Count = model.PrintedComponents[updateComponent.ComponentId].Item2; + model.PrintedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var printed = context.Printeds.First(x => x.Id == Id); + foreach (var pc in model.PrintedComponents) + { + context.PrintedComponents.Add(new PrintedComponent + { + Printed = printed, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _printedComponents = null; + } + } +} diff --git a/Typography/TypographyDatabaseImplement/Models/PrintedComponent.cs b/Typography/TypographyDatabaseImplement/Models/PrintedComponent.cs new file mode 100644 index 0000000..3230de1 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Models/PrintedComponent.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 TypographyDatabaseImplement.Models +{ + public class PrintedComponent + { + public int Id { get; set; } + + [Required] + public int PrintedId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Printed Printed { get; set; } = new(); + } +} diff --git a/Typography/TypographyDatabaseImplement/TypographyDatabase.cs b/Typography/TypographyDatabaseImplement/TypographyDatabase.cs new file mode 100644 index 0000000..fcb923d --- /dev/null +++ b/Typography/TypographyDatabaseImplement/TypographyDatabase.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using TypographyDatabaseImplement.Models; + +namespace TypographyDatabaseImplement +{ + public class TypographyDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-VCUNP5I6\SQLEXPRESS;Initial Catalog=TypographyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet Printeds { set; get; } + + public virtual DbSet PrintedComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj b/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj new file mode 100644 index 0000000..1ce9597 --- /dev/null +++ b/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Typography/TypographyView/Program.cs b/Typography/TypographyView/Program.cs index aea75eb..8416c5a 100644 --- a/Typography/TypographyView/Program.cs +++ b/Typography/TypographyView/Program.cs @@ -1,7 +1,7 @@ using TypographyBusinessLogic.BusinessLogics; using TypographyContracts.BusinessLogicsContracts; using TypographyContracts.StoragesContracts; -using TypographyFileImplement.Implements; +using TypographyDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Typography/TypographyView/TypographyView.csproj b/Typography/TypographyView/TypographyView.csproj index e6c718b..9f08fc1 100644 --- a/Typography/TypographyView/TypographyView.csproj +++ b/Typography/TypographyView/TypographyView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -26,6 +30,7 @@ +