From 82487590ad82bde3ea5b3523df159a63f3dae0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Sun, 26 Mar 2023 18:33:41 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=203=20=D0=BF=D0=BE=D1=81?= =?UTF-8?q?=D0=BB=D0=B5=20=D0=BF=D0=B0=D0=B4=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlacksmithWorkshop/BlacksmithWorkshop.sln | 8 +- .../BlacksmithWorkshopView.csproj | 5 + .../BlacksmithWorkshop/Program.cs | 3 +- .../BlacksmithWorkshopDatabase.cs | 28 +++ ...BlacksmithWorkshopDatebaseImplement.csproj | 22 +++ .../Implements/ComponentStorage.cs | 77 ++++++++ .../Implements/ManufactureStorage.cs | 102 +++++++++++ .../Implements/OrderStorage.cs | 91 ++++++++++ .../20230326142752_InitMigration.Designer.cs | 169 ++++++++++++++++++ .../20230326142752_InitMigration.cs | 125 +++++++++++++ ...BlacksmithWorkshopDatabaseModelSnapshot.cs | 166 +++++++++++++++++ .../Models/Component.cs | 56 ++++++ .../Models/Manufacture.cs | 88 +++++++++ .../Models/ManufactureComponent.cs | 17 ++ .../Models/Order.cs | 76 ++++++++ 15 files changed, 1030 insertions(+), 3 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ComponentStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ManufactureStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/OrderStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Component.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Manufacture.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureComponent.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Order.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop.sln b/BlacksmithWorkshop/BlacksmithWorkshop.sln index 2037fc1..3e3c374 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop.sln +++ b/BlacksmithWorkshop/BlacksmithWorkshop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopBusinessL EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopListImplement", "BlacksmithWorkshopListImplement\BlacksmithWorkshopListImplement.csproj", "{D57FFF62-0A87-4995-9693-317A94689469}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{0FFB9D8A-1B02-4622-AD31-DB88F4201350}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{0FFB9D8A-1B02-4622-AD31-DB88F4201350}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopDatebaseImplement", "BlacksmithWorkshopDatebaseImplement\BlacksmithWorkshopDatebaseImplement.csproj", "{2DB15039-1244-4882-BF37-C3FD8C0B7CBB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Debug|Any CPU.Build.0 = Debug|Any CPU {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Release|Any CPU.ActiveCfg = Release|Any CPU {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Release|Any CPU.Build.0 = Release|Any CPU + {2DB15039-1244-4882-BF37-C3FD8C0B7CBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2DB15039-1244-4882-BF37-C3FD8C0B7CBB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2DB15039-1244-4882-BF37-C3FD8C0B7CBB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2DB15039-1244-4882-BF37-C3FD8C0B7CBB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj index 55b8a2d..ae37a5d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -26,6 +30,7 @@ + diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index 49a2371..643d0f9 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -1,8 +1,7 @@ using BlacksmithWorkShopBusinessLogic.BusinessLogics; using BlacksmithWorkshopContracts.BusinessLogicContracts; using BlacksmithWorkshopContracts.StoragesContracts; -using BlacksmithWorkshopFileImplement.Implements; -using BlacksmithWorkshopView; +using BlacksmithWorkshopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs new file mode 100644 index 0000000..05d278c --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs @@ -0,0 +1,28 @@ +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BlacksmithWorkshopDatabaseImplement +{ + public class BlacksmithWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer + ( + @"Data Source=IGORS_2011; + Initial Catalog=BlacksmithWorkshopDatabaseFull; + Integrated Security=True; + MultipleActiveResultSets=True; + TrustServerCertificate=True" + ); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Manufactures { set; get; } + public virtual DbSet ManufactureComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj new file mode 100644 index 0000000..a5d5c4a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatebaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ComponentStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..94167ca --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,77 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new BlacksmithWorkshopDatabase(); + 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 BlacksmithWorkshopDatabase(); + 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 BlacksmithWorkshopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + 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 BlacksmithWorkshopDatabase(); + 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/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ManufactureStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ManufactureStorage.cs new file mode 100644 index 0000000..0c674fd --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/ManufactureStorage.cs @@ -0,0 +1,102 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class ManufactureStorage : IManufactureStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + return context.Manufactures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName)) + { + return new(); + } + using var context = new BlacksmithWorkshopDatabase(); + return context.Manufactures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ManufactureName.Contains(model.ManufactureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ManufactureViewModel? GetElement(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName) && + !model.Id.HasValue) + { + return null; + } + using var context = new BlacksmithWorkshopDatabase(); + return context.Manufactures + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ManufactureViewModel? Insert(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var newManufacture = Manufacture.Create(context, model); + if (newManufacture == null) + { + return null; + } + context.Manufactures.Add(newManufacture); + context.SaveChanges(); + return newManufacture.GetViewModel; + } + public ManufactureViewModel? Update(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var manufacture = context.Manufactures.FirstOrDefault(rec => + rec.Id == model.Id); + if (manufacture == null) + { + return null; + } + manufacture.Update(model); + context.SaveChanges(); + manufacture.UpdateComponents(context, model); + transaction.Commit(); + return manufacture.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ManufactureViewModel? Delete(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Manufactures + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Manufactures.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..59a416a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,91 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + List orderList = context.Orders + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string manufactureName = context.Manufactures + .SingleOrDefault(x => x.Id == order.ManufactureId)?.ManufactureName ?? string.Empty; + order.ManufactureName = manufactureName; + } + return orderList; + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new BlacksmithWorkshopDatabase(); + List orderList = context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string manufactureName = context.Manufactures + .SingleOrDefault(x => x.Id == order.ManufactureId)?.ManufactureName ?? string.Empty; + order.ManufactureName = manufactureName; + } + return orderList; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new BlacksmithWorkshopDatabase(); + return context.Orders + .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 BlacksmithWorkshopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + 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 BlacksmithWorkshopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs new file mode 100644 index 0000000..acbbe31 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatebaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230326142752_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("ManufactureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Components") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs new file mode 100644 index 0000000..61c4b95 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatebaseImplement.Migrations +{ + /// + public partial class InitMigration : 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: "Manufactures", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Manufactures", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ManufactureComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureId = 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_ManufactureComponents", x => x.Id); + table.ForeignKey( + name: "FK_ManufactureComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ManufactureComponents_Manufactures_ManufactureId", + column: x => x.ManufactureId, + principalTable: "Manufactures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureId = 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_Manufactures_ManufactureId", + column: x => x.ManufactureId, + principalTable: "Manufactures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ManufactureComponents_ComponentId", + table: "ManufactureComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ManufactureComponents_ManufactureId", + table: "ManufactureComponents", + column: "ManufactureId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ManufactureId", + table: "Orders", + column: "ManufactureId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ManufactureComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Manufactures"); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..ddfbfaa --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatebaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + partial class BlacksmithWorkshopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("ManufactureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Components") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Component.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Component.cs new file mode 100644 index 0000000..b89c52d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BlacksmithWorkshopDatabaseImplement.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 ManufactureComponents { 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/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Manufacture.cs new file mode 100644 index 0000000..e9aaf67 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Manufacture.cs @@ -0,0 +1,88 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Manufacture : IManufactureModel + { + public int Id { get; set; } + [Required] + public string ManufactureName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _manufactureComponents = null; + [NotMapped] + public Dictionary ManufactureComponents + { + get + { + if (_manufactureComponents == null) + { + _manufactureComponents = Components.ToDictionary(recMC => recMC.ComponentId, recMC => (recMC.Component as IComponentModel, recMC.Count)); + } + return _manufactureComponents; + } + } + [ForeignKey("ManufactureId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ManufactureId")] + public virtual List Orders { get; set; } = new(); + public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + return new Manufacture() + { + Id = model.Id, + ManufactureName = model.ManufactureName, + Price = model.Price, + Components = model.ManufactureComponents.Select(x => new ManufactureComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ManufactureBindingModel model) + { + ManufactureName = model.ManufactureName; + Price = model.Price; + } + public ManufactureViewModel GetViewModel => new() + { + Id = Id, + ManufactureName = ManufactureName, + Price = Price, + ManufactureComponents = ManufactureComponents + }; + public void UpdateComponents(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + List manufactureComponents = context.ManufactureComponents.Where(rec => rec.ManufactureId == model.Id).ToList(); + if (manufactureComponents != null && manufactureComponents.Count > 0) + { // удалили те, которых нет в модели + context.ManufactureComponents.RemoveRange(manufactureComponents.Where(rec => !model.ManufactureComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in manufactureComponents) + { + updateComponent.Count = model.ManufactureComponents[updateComponent.ComponentId].Item2; + model.ManufactureComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var manufacture = context.Manufactures.First(x => x.Id == Id); + foreach (var pc in model.ManufactureComponents) + { + context.ManufactureComponents.Add(new ManufactureComponent + { + Manufacture = manufacture, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _manufactureComponents = null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureComponent.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureComponent.cs new file mode 100644 index 0000000..ec51192 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class ManufactureComponent + { + public int Id { get; set; } + [Required] + public int ManufactureId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Manufacture Manufacture { get; set; } = new(); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Order.cs new file mode 100644 index 0000000..9747f16 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Order.cs @@ -0,0 +1,76 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Enums; +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int ManufactureId { get; set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ManufactureId = model.ManufactureId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order Create(OrderViewModel model) + { + return new Order() + { + Id = model.Id, + ManufactureId = model.ManufactureId, + 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; + if (model.DateImplement != null) + { + DateImplement = model.DateImplement; + } + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ManufactureId = ManufactureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} -- 2.25.1 From aeaa0b8ee887658587451724d428a0fecc107ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Tue, 9 May 2023 17:28:55 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D0=BE=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7?= =?UTF-8?q?=D0=B0=D0=BA=D1=86=D0=B8=D1=8F=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 18 +- .../BusinessLogics/StoreLogic.cs | 1 + .../BlacksmithWorkshopDatabase.cs | 6 +- .../Implements/StoreStorage.cs | 153 +++++++++++ .../20230326142752_InitMigration.Designer.cs | 2 +- .../20230326142752_InitMigration.cs | 2 +- ...0230509130456_Lab3AdvMigration.Designer.cs | 246 ++++++++++++++++++ .../20230509130456_Lab3AdvMigration.cs | 78 ++++++ ...BlacksmithWorkshopDatabaseModelSnapshot.cs | 79 +++++- .../Models/ManufactureInStore.cs | 17 ++ .../Models/Store.cs | 108 ++++++++ 11 files changed, 696 insertions(+), 14 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/StoreStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureInStore.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Store.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index d0ff0fd..264e53c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -44,15 +44,6 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics model.Status = vm?.Status ?? OrderStatus.Неизвестен; if ((int)model.Status == (int)newstatus - 1) { - model.Status = newstatus; - if (newstatus == OrderStatus.Готов) - { - model.DateImplement = DateTime.Now; - } - if (_orderStorage.Update(model) != null) - { - return true; - } if (newstatus == OrderStatus.Выдан) { if (vm == null) @@ -68,6 +59,15 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics throw new Exception("Не удалось заполнить магазины"); } } + model.Status = newstatus; + if (newstatus == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + } + if (_orderStorage.Update(model) != null) + { + return true; + } } _logger.LogWarning($"Changing order status of order {model.Id} to {newstatus} failed"); return false; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs index bc06417..96fb8fc 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs @@ -86,6 +86,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics } _storeStorage.Update(new StoreBindingModel() { + Id = model.Id, StoreName = model.StoreName, Address = model.Address, OpeningDate = model.OpeningDate, diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs index 05d278c..aaa3544 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/BlacksmithWorkshopDatabase.cs @@ -12,7 +12,7 @@ namespace BlacksmithWorkshopDatabaseImplement optionsBuilder.UseSqlServer ( @"Data Source=IGORS_2011; - Initial Catalog=BlacksmithWorkshopDatabaseFull; + Initial Catalog=BlacksmithWorkshopDatabaseFullAdv; Integrated Security=True; MultipleActiveResultSets=True; TrustServerCertificate=True" @@ -24,5 +24,7 @@ namespace BlacksmithWorkshopDatabaseImplement public virtual DbSet Manufactures { set; get; } public virtual DbSet ManufactureComponents { set; get; } public virtual DbSet Orders { set; get; } - } + public virtual DbSet Stores { set; get; } + public virtual DbSet ManufacturesInStore { set; get; } + } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/StoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/StoreStorage.cs new file mode 100644 index 0000000..ede6823 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/StoreStorage.cs @@ -0,0 +1,153 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement; +using BlacksmithWorkshopDatabaseImplement.Models; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class StoreStorage : IStoreStorage + { + public StoreViewModel? Delete(StoreBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Stores.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Stores.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public StoreViewModel? GetElement(StoreSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new BlacksmithWorkshopDatabase(); + return context.Stores + .Include(x => x.ManufacturesInStore) + .ThenInclude(x => x.Manufacture) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(StoreSearchModel model) + { + if (string.IsNullOrEmpty(model.StoreName)) + { + return new(); + } + using var context = new BlacksmithWorkshopDatabase(); + return context.Stores + .Include(x => x.ManufacturesInStore) + .ThenInclude(x => x.Manufacture) + .Select(x => x.GetViewModel) + .Where(x => x.StoreName.Contains(model.StoreName ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + return context.Stores + .Include(x => x.ManufacturesInStore) + .ThenInclude(x => x.Manufacture) + .Select(shop => shop.GetViewModel) + .ToList(); + } + + public StoreViewModel? Insert(StoreBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Store.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Stores.Any(x => x.StoreName == newShop.StoreName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Stores.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public StoreViewModel? Update(StoreBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var store = context.Stores.FirstOrDefault(x => x.Id == model.Id); + if (store == null) + { + return null; + } + store.Update(model); + store.UpdateManufactures(context, model); + context.SaveChanges(); + return store.GetViewModel; + } + + public bool HasManufactures(IManufactureModel manufacture, int count) + { + throw new NotImplementedException(); + } + + public bool SellManufacture(IManufactureModel manufacture, int count) + { + using var context = new BlacksmithWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + foreach (var MIS in context.ManufacturesInStore.Where(x => x.ManufactureId == manufacture.Id)) + { + var res = Math.Min(count, MIS.Count); + MIS.Count -= res; + count -= res; + if (MIS.Count == 0) + { + context.ManufacturesInStore.Remove(MIS); + } + if (count == 0) + { + break; + } + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + return count == 0; + } + + public int GetPlaceLeft(IStoreModel store) + { + using var context = new BlacksmithWorkshopDatabase(); + var selectedStore = context.Stores.FirstOrDefault(x => x.Id == store.Id); + if (selectedStore == null) + { + return 0; + } + return selectedStore.MaxManufactures - selectedStore.Manufactures.Select(x => x.Value.Item2).Sum(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs index acbbe31..ca3c403 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable -namespace BlacksmithWorkshopDatebaseImplement.Migrations +namespace BlacksmithWorkshopDatabaseImplement.Migrations { [DbContext(typeof(BlacksmithWorkshopDatabase))] [Migration("20230326142752_InitMigration")] diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs index 61c4b95..b69af54 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230326142752_InitMigration.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; #nullable disable -namespace BlacksmithWorkshopDatebaseImplement.Migrations +namespace BlacksmithWorkshopDatabaseImplement.Migrations { /// public partial class InitMigration : Migration diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.Designer.cs new file mode 100644 index 0000000..dd4acd3 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230509130456_Lab3AdvMigration")] + partial class Lab3AdvMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("StoreId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.HasIndex("StoreId"); + + b.ToTable("ManufacturesInStore"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("ManufactureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxManufactures") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("StoreName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Stores"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("ManufactureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Components") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany() + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Store", "Store") + .WithMany("ManufacturesInStore") + .HasForeignKey("StoreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + + b.Navigation("Store"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b => + { + b.Navigation("ManufacturesInStore"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.cs new file mode 100644 index 0000000..a3f52ae --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/20230509130456_Lab3AdvMigration.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + /// + public partial class Lab3AdvMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Stores", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + StoreName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + MaxManufactures = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Stores", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ManufacturesInStore", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ManufactureId = table.Column(type: "int", nullable: false), + StoreId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ManufacturesInStore", x => x.Id); + table.ForeignKey( + name: "FK_ManufacturesInStore_Manufactures_ManufactureId", + column: x => x.ManufactureId, + principalTable: "Manufactures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ManufacturesInStore_Stores_StoreId", + column: x => x.StoreId, + principalTable: "Stores", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ManufacturesInStore_ManufactureId", + table: "ManufacturesInStore", + column: "ManufactureId"); + + migrationBuilder.CreateIndex( + name: "IX_ManufacturesInStore_StoreId", + table: "ManufacturesInStore", + column: "StoreId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ManufacturesInStore"); + + migrationBuilder.DropTable( + name: "Stores"); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs index ddfbfaa..6f14c16 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable -namespace BlacksmithWorkshopDatebaseImplement.Migrations +namespace BlacksmithWorkshopDatabaseImplement.Migrations { [DbContext(typeof(BlacksmithWorkshopDatabase))] partial class BlacksmithWorkshopDatabaseModelSnapshot : ModelSnapshot @@ -88,6 +88,32 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations b.ToTable("ManufactureComponents"); }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("StoreId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ManufactureId"); + + b.HasIndex("StoreId"); + + b.ToTable("ManufacturesInStore"); + }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -121,6 +147,33 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxManufactures") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("StoreName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Stores"); + }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => { b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") @@ -140,6 +193,25 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations b.Navigation("Manufacture"); }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany() + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Store", "Store") + .WithMany("ManufacturesInStore") + .HasForeignKey("StoreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Manufacture"); + + b.Navigation("Store"); + }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => { b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null) @@ -160,6 +232,11 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b => + { + b.Navigation("ManufacturesInStore"); + }); #pragma warning restore 612, 618 } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureInStore.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureInStore.cs new file mode 100644 index 0000000..306a433 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/ManufactureInStore.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class ManufactureInStore + { + public int Id { get; set; } + [Required] + public int ManufactureId { get; set; } + [Required] + public int StoreId { get; set; } + [Required] + public int Count { get; set; } + public virtual Store Store { get; set; } = new(); + public virtual Manufacture Manufacture { get; set; } = new(); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Store.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Store.cs new file mode 100644 index 0000000..bb7eeb1 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Models/Store.cs @@ -0,0 +1,108 @@ +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Store : IStoreModel + { + public int Id { get; private set; } + [Required] + public string StoreName { get; private set; } = string.Empty; + [Required] + public string Address { get; private set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; private set; } + [Required] + public int MaxManufactures { get; private set; } + private Dictionary? _manufactures = null; + [NotMapped] + public Dictionary Manufactures + { + get + { + if (_manufactures == null) + { + using var context = new BlacksmithWorkshopDatabase(); + _manufactures = ManufacturesInStore + .ToDictionary(x => x.ManufactureId, x => (context.Manufactures + .FirstOrDefault(y => y.Id == x.ManufactureId)! as IManufactureModel, x.Count)); + } + return _manufactures; + } + } + [ForeignKey("StoreId")] + public virtual List ManufacturesInStore { get; set; } = new(); + + public static Store? Create(BlacksmithWorkshopDatabase context, StoreBindingModel? model) + { + if (model == null) + { + return null; + } + return new Store() + { + Id = model.Id, + StoreName = model.StoreName, + Address = model.Address, + OpeningDate = model.OpeningDate, + MaxManufactures = model.MaxManufactures, + ManufacturesInStore = model.Manufactures.Select(x => new ManufactureInStore + { + Manufacture = context.Manufactures.FirstOrDefault(y => y.Id == x.Key)!, + Count = x.Value.Item2, + }).ToList() + }; + } + public void Update(StoreBindingModel? model) + { + if (model == null) + { + return; + } + StoreName = model.StoreName; + Address = model.Address; + OpeningDate = model.OpeningDate; + } + public StoreViewModel GetViewModel => new() + { + Id = Id, + StoreName = StoreName, + Address = Address, + Manufactures = Manufactures, + OpeningDate = OpeningDate, + MaxManufactures = MaxManufactures, + }; + + public void UpdateManufactures(BlacksmithWorkshopDatabase context, StoreBindingModel model) + { + var manufacturesInStore = context.ManufacturesInStore + .Where(rec => rec.StoreId == model.Id) + .ToList(); + // удалили те, которых нет в модели + if (manufacturesInStore != null && manufacturesInStore.Count > 0) + { + context.ManufacturesInStore + .RemoveRange(manufacturesInStore + .Where(rec => !model.Manufactures + .ContainsKey(rec.ManufactureId))); + // обновили количество у существующих записей + foreach (var updateManufacture in manufacturesInStore.Where(x => model.Manufactures.ContainsKey(x.ManufactureId))) + { + updateManufacture.Count = model.Manufactures[updateManufacture.ManufactureId].Item2; + model.Manufactures.Remove(updateManufacture.ManufactureId); + } + } + var store = context.Stores.First(x => x.Id == model.Id); + store.ManufacturesInStore.AddRange(model.Manufactures.Select(x => new ManufactureInStore + { + Manufacture = context.Manufactures.First(y => y.Id == x.Key), + Count = x.Value.Item2, + }).Except(manufacturesInStore ?? new())); + context.SaveChanges(); + _manufactures = null; + } + } +} -- 2.25.1