From a821a68770bfd213c0e28ef3322ba2c8ef3be55c Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Tue, 26 Mar 2024 18:50:50 +0400 Subject: [PATCH 01/10] =?UTF-8?q?=D1=87=D1=83=D1=82=D1=8C=20=D1=87=D1=83?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BD=D0=B0=D0=B4=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AircraftPlant/AircraftPlant.sln | 8 +- .../AircraftPlantDatabase.cs | 27 +++ .../AircraftPlantDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 77 ++++++++ .../Implements/OrderStorage.cs | 94 ++++++++++ .../Implements/PlaneStorage.cs | 110 ++++++++++++ .../20240326134051_InitialCreate.Designer.cs | 169 ++++++++++++++++++ .../20240326134051_InitialCreate.cs | 125 +++++++++++++ .../AircraftPlantDatabaseModelSnapshot.cs | 166 +++++++++++++++++ .../Models/Component.cs | 56 ++++++ .../Models/Order.cs | 66 +++++++ .../Models/Plane.cs | 94 ++++++++++ .../Models/PlaneComponent.cs | 23 +++ .../AircraftPlantView.csproj | 5 + AircraftPlant/AircraftPlantView/Program.cs | 2 +- 15 files changed, 1043 insertions(+), 2 deletions(-) create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabaseImplement.csproj create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/PlaneComponent.cs diff --git a/AircraftPlant/AircraftPlant.sln b/AircraftPlant/AircraftPlant.sln index 87d8379..fe263a2 100644 --- a/AircraftPlant/AircraftPlant.sln +++ b/AircraftPlant/AircraftPlant.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AircraftPlantBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AircraftPlantListImplement", "AbstractShopListImplement\AircraftPlantListImplement.csproj", "{69BB512A-F548-45B7-B983-C3E9600CFC5D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantFileImplement", "AircraftPlantFileImplement\AircraftPlantFileImplement.csproj", "{5DA728EE-42D1-45EC-A62D-67EBB0DF316C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AircraftPlantFileImplement", "AircraftPlantFileImplement\AircraftPlantFileImplement.csproj", "{5DA728EE-42D1-45EC-A62D-67EBB0DF316C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantDatabaseImplement", "AircraftPlantDatabaseImplement\AircraftPlantDatabaseImplement.csproj", "{F63D1E94-5F0A-42CC-8BC9-058CBCFE7EDA}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {5DA728EE-42D1-45EC-A62D-67EBB0DF316C}.Debug|Any CPU.Build.0 = Debug|Any CPU {5DA728EE-42D1-45EC-A62D-67EBB0DF316C}.Release|Any CPU.ActiveCfg = Release|Any CPU {5DA728EE-42D1-45EC-A62D-67EBB0DF316C}.Release|Any CPU.Build.0 = Release|Any CPU + {F63D1E94-5F0A-42CC-8BC9-058CBCFE7EDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F63D1E94-5F0A-42CC-8BC9-058CBCFE7EDA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F63D1E94-5F0A-42CC-8BC9-058CBCFE7EDA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F63D1E94-5F0A-42CC-8BC9-058CBCFE7EDA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs new file mode 100644 index 0000000..ef80783 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace AircraftPlantDatabaseImplement +{ + public class AircraftPlantDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6QDRI0N\SQLEXPRESS;Initial Catalog=AircraftPlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Planes { set; get; } + public virtual DbSet PlaneComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabaseImplement.csproj b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabaseImplement.csproj new file mode 100644 index 0000000..d3f5744 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..c36a155 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,77 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new AircraftPlantDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AircraftPlantDatabase(); + 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 AircraftPlantDatabase(); + 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 AircraftPlantDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AircraftPlantDatabase(); + 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 AircraftPlantDatabase(); + 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/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1449885 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new AircraftPlantDatabase(); + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new AircraftPlantDatabase(); + return context.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new AircraftPlantDatabase(); + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new AircraftPlantDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + return GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return GetViewModel(element); + } + return null; + } + private static OrderViewModel GetViewModel(Order order) + { + using var context = new AircraftPlantDatabase(); + var viewModel = order.GetViewModel; + var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId); + if (plane != null) + { + viewModel.PlaneName = plane.PlaneName; + } + return viewModel; + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs new file mode 100644 index 0000000..8befae3 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs @@ -0,0 +1,110 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Implements +{ + public class PlaneStorage : IPlaneStorage + { + public List GetFullList() + { + using var context = new AircraftPlantDatabase(); + return context.Planes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PlaneSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaneName)) + { + return new(); + } + using var context = new AircraftPlantDatabase(); + return context.Planes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PlaneName.Contains(model.PlaneName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public PlaneViewModel? GetElement(PlaneSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaneName) && + !model.Id.HasValue) + { + return null; + } + using var context = new AircraftPlantDatabase(); + return context.Planes + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && + x.PlaneName == model.PlaneName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PlaneViewModel? Insert(PlaneBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var newPlane = Plane.Create(context, model); + if (newPlane == null) + { + return null; + } + context.Planes.Add(newPlane); + context.SaveChanges(); + return newPlane.GetViewModel; + } + public PlaneViewModel? Update(PlaneBindingModel model) + { + using var context = new AircraftPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var plane = context.Planes.FirstOrDefault(rec => + rec.Id == model.Id); + if (plane == null) + { + return null; + } + plane.Update(model); + context.SaveChanges(); + plane.UpdateComponents(context, model); + transaction.Commit(); + return plane.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PlaneViewModel? Delete(PlaneBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var element = context.Planes + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Planes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs new file mode 100644 index 0000000..d45b967 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using AircraftPlantDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AircraftPlantDatabaseImplement.Migrations +{ + [DbContext(typeof(AircraftPlantDatabase))] + [Migration("20240326134051_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.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("AircraftPlantDatabaseImplement.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("PlaneId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PlaneId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PlaneName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Planes"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PlaneId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PlaneId"); + + b.ToTable("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) + .WithMany("Orders") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Component", "Component") + .WithMany("PlaneComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany("Components") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Plane"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b => + { + b.Navigation("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs new file mode 100644 index 0000000..7d0e945 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AircraftPlantDatabaseImplement.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: "Planes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PlaneName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Planes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PlaneId = 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_Planes_PlaneId", + column: x => x.PlaneId, + principalTable: "Planes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PlaneComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PlaneId = 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_PlaneComponents", x => x.Id); + table.ForeignKey( + name: "FK_PlaneComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PlaneComponents_Planes_PlaneId", + column: x => x.PlaneId, + principalTable: "Planes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PlaneId", + table: "Orders", + column: "PlaneId"); + + migrationBuilder.CreateIndex( + name: "IX_PlaneComponents_ComponentId", + table: "PlaneComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PlaneComponents_PlaneId", + table: "PlaneComponents", + column: "PlaneId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PlaneComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Planes"); + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..0de3086 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using AircraftPlantDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AircraftPlantDatabaseImplement.Migrations +{ + [DbContext(typeof(AircraftPlantDatabase))] + partial class AircraftPlantDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.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("AircraftPlantDatabaseImplement.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("PlaneId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PlaneId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PlaneName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Planes"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PlaneId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PlaneId"); + + b.ToTable("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) + .WithMany("Orders") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Component", "Component") + .WithMany("PlaneComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany("Components") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Plane"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b => + { + b.Navigation("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..9508c7c --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AircraftPlantDatabaseImplement.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 PlaneComponents { 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/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..49ba435 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,66 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Enums; +using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int PlaneId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + Id = model.Id, + PlaneId = model.PlaneId, + 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, + PlaneId = PlaneId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs new file mode 100644 index 0000000..3108c85 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AircraftPlantDatabaseImplement.Models +{ + public class Plane : IPlaneModel + { + public int Id { get; set; } + [Required] + public string PlaneName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _planeComponents = null; + [NotMapped] + public Dictionary PlaneComponents + { + get + { + if (_planeComponents == null) + { + _planeComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _planeComponents; + } + } + [ForeignKey("PlaneId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PlaneId")] + public virtual List Orders { get; set; } = new(); + public static Plane Create(AircraftPlantDatabase context, PlaneBindingModel model) + { + return new Plane() + { + Id = model.Id, + PlaneName = model.PlaneName, + Price = model.Price, + Components = model.PlaneComponents.Select(x => new PlaneComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PlaneBindingModel model) + { + PlaneName = model.PlaneName; + Price = model.Price; + } + public PlaneViewModel GetViewModel => new() + { + Id = Id, + PlaneName = PlaneName, + Price = Price, + PlaneComponents = PlaneComponents + }; + public void UpdateComponents(AircraftPlantDatabase context, PlaneBindingModel model) + { + var planeComponents = context.PlaneComponents.Where(rec => rec.PlaneId == model.Id).ToList(); + if (planeComponents != null && planeComponents.Count > 0) + { // удалили те, которых нет в модели + context.PlaneComponents.RemoveRange(planeComponents.Where(rec => !model.PlaneComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in planeComponents) + { + updateComponent.Count = model.PlaneComponents[updateComponent.ComponentId].Item2; + model.PlaneComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var plane = context.Planes.First(x => x.Id == Id); + foreach (var pc in model.PlaneComponents) + { + context.PlaneComponents.Add(new PlaneComponent + { + Plane = plane, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _planeComponents = null; + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/PlaneComponent.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/PlaneComponent.cs new file mode 100644 index 0000000..c690b21 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/PlaneComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + + +namespace AircraftPlantDatabaseImplement.Models +{ + public class PlaneComponent + { + public int Id { get; set; } + [Required] + public int PlaneId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Plane Plane { get; set; } = new(); + } +} diff --git a/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj b/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj index 25d874a..7f4cabc 100644 --- a/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj +++ b/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/AircraftPlant/AircraftPlantView/Program.cs b/AircraftPlant/AircraftPlantView/Program.cs index fbdda4e..33775ef 100644 --- a/AircraftPlant/AircraftPlantView/Program.cs +++ b/AircraftPlant/AircraftPlantView/Program.cs @@ -1,7 +1,7 @@ using AircraftPlantBusinessLogic.BusinessLogics; using AircraftPlantContracts.BusinessLogicsContracts; using AircraftPlantContracts.StoragesContracts; -using AircraftPlantFileImplement.Implements; +using AircraftPlantDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 5d694d2f125863272c0087a673b2a6c8c20eff47 Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Tue, 26 Mar 2024 18:52:05 +0400 Subject: [PATCH 02/10] =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=BC=D0=B0=D0=B5=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftPlantDatabaseImplement/Implements/OrderStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs index 1449885..acc8553 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -39,7 +39,7 @@ namespace AircraftPlantDatabaseImplement.Implements } using var context = new AircraftPlantDatabase(); - return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + return GetViewModel(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)); } public OrderViewModel? Insert(OrderBindingModel model) { -- 2.25.1 From 9354462a00dc98f2c270504f76f678bac08366b0 Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Tue, 26 Mar 2024 19:43:48 +0400 Subject: [PATCH 03/10] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=BF=D0=B5=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 63 ++++++++++--------- .../AircraftPlantDatabaseModelSnapshot.cs | 4 +- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs index acc8553..6daf99a 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -3,11 +3,7 @@ using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; using AircraftPlantDatabaseImplement.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace AircraftPlantDatabaseImplement.Implements { @@ -16,31 +12,39 @@ namespace AircraftPlantDatabaseImplement.Implements public List GetFullList() { using var context = new AircraftPlantDatabase(); - return context.Orders.Select(x => GetViewModel(x)).ToList(); + return context.Orders + .Include(x => x.Plant) + .Select(x => x.GetViewModel) + .ToList(); } + public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) { return new(); } - using var context = new AircraftPlantDatabase(); return context.Orders - .Where(x => x.Id.Equals(model.Id)) - .Select(x => GetViewModel(x)) - .ToList(); + .Include(x => x.Plant) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } + public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } - using var context = new AircraftPlantDatabase(); - return GetViewModel(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)); + return context.Orders + .Include(x => x.Plant) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } + public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -48,12 +52,15 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } - using var context = new AircraftPlantDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return GetViewModel(newOrder); + return context.Orders + .Include(x => x.Plant) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } + public OrderViewModel? Update(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); @@ -62,33 +69,29 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } - order.Update(model); context.SaveChanges(); - return GetViewModel(order); + return context.Orders + .Include(x => x.Plant) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } + public OrderViewModel? Delete(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); - var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { + var deletedElement = context.Orders + .Include(x => x.Plant) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return GetViewModel(element); + return deletedElement; } return null; } - private static OrderViewModel GetViewModel(Order order) - { - using var context = new AircraftPlantDatabase(); - var viewModel = order.GetViewModel; - var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId); - if (plane != null) - { - viewModel.PlaneName = plane.PlaneName; - } - return viewModel; - } } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs index 0de3086..a65fd13 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs @@ -123,11 +123,13 @@ namespace AircraftPlantDatabaseImplement.Migrations modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => { - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") .WithMany("Orders") .HasForeignKey("PlaneId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Plane"); }); modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => -- 2.25.1 From 72202d2d74c1c951eedc7ccc249289e09dd07235 Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Tue, 26 Mar 2024 21:23:10 +0400 Subject: [PATCH 04/10] =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=BC=D0=B0=D0=BB?= =?UTF-8?q?=D0=BE=D1=81=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftPlantDatabase.cs | 2 +- .../Implements/OrderStorage.cs | 12 +++++----- ... 20240326170504_InitialCreate.Designer.cs} | 6 +++-- ...ate.cs => 20240326170504_InitialCreate.cs} | 0 .../Models/Component.cs | 11 ++++++++- .../Models/Order.cs | 24 ++++++++++++------- AircraftPlant/AircraftPlantView/FormMain.cs | 5 ++-- 7 files changed, 39 insertions(+), 21 deletions(-) rename AircraftPlant/AircraftPlantDatabaseImplement/Migrations/{20240326134051_InitialCreate.Designer.cs => 20240326170504_InitialCreate.Designer.cs} (98%) rename AircraftPlant/AircraftPlantDatabaseImplement/Migrations/{20240326134051_InitialCreate.cs => 20240326170504_InitialCreate.cs} (100%) diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs index ef80783..5294c0a 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs @@ -15,7 +15,7 @@ namespace AircraftPlantDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6QDRI0N\SQLEXPRESS;Initial Catalog=AircraftPlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6QDRI0N\SQLEXPRESS;Initial Catalog=AircraftPlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs index 6daf99a..cad61b5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -13,7 +13,7 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .Select(x => x.GetViewModel) .ToList(); } @@ -26,7 +26,7 @@ namespace AircraftPlantDatabaseImplement.Implements } using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); @@ -40,7 +40,7 @@ namespace AircraftPlantDatabaseImplement.Implements } using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -56,7 +56,7 @@ namespace AircraftPlantDatabaseImplement.Implements context.Orders.Add(newOrder); context.SaveChanges(); return context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .FirstOrDefault(x => x.Id == newOrder.Id) ?.GetViewModel; } @@ -72,7 +72,7 @@ namespace AircraftPlantDatabaseImplement.Implements order.Update(model); context.SaveChanges(); return context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -84,7 +84,7 @@ namespace AircraftPlantDatabaseImplement.Implements if (element != null) { var deletedElement = context.Orders - .Include(x => x.Plant) + .Include(x => x.Plane) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; context.Orders.Remove(element); diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs similarity index 98% rename from AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs rename to AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs index d45b967..eac223c 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.Designer.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace AircraftPlantDatabaseImplement.Migrations { [DbContext(typeof(AircraftPlantDatabase))] - [Migration("20240326134051_InitialCreate")] + [Migration("20240326170504_InitialCreate")] partial class InitialCreate { /// @@ -126,11 +126,13 @@ namespace AircraftPlantDatabaseImplement.Migrations modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => { - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") .WithMany("Orders") .HasForeignKey("PlaneId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Plane"); }); modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.cs similarity index 100% rename from AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326134051_InitialCreate.cs rename to AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.cs diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs index 9508c7c..7ffeebf 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs @@ -1,6 +1,8 @@ using AircraftPlantContracts.BindingModels; using AircraftPlantContracts.ViewModels; using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; @@ -9,12 +11,16 @@ namespace AircraftPlantDatabaseImplement.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 PlaneComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) { if (model == null) @@ -28,6 +34,7 @@ namespace AircraftPlantDatabaseImplement.Models Cost = model.Cost }; } + public static Component Create(ComponentViewModel model) { return new Component @@ -37,6 +44,7 @@ namespace AircraftPlantDatabaseImplement.Models Cost = model.Cost }; } + public void Update(ComponentBindingModel model) { if (model == null) @@ -46,6 +54,7 @@ namespace AircraftPlantDatabaseImplement.Models ComponentName = model.ComponentName; Cost = model.Cost; } + public ComponentViewModel GetViewModel => new() { Id = Id, @@ -53,4 +62,4 @@ namespace AircraftPlantDatabaseImplement.Models Cost = Cost }; } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs index 49ba435..64e7110 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs @@ -2,36 +2,40 @@ using AircraftPlantContracts.ViewModels; using AircraftPlantDataModels.Enums; using AircraftPlantDataModels.Models; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Models { public class Order : IOrderModel { public int Id { get; set; } + [Required] public int PlaneId { get; set; } + [Required] public int Count { get; set; } + [Required] public double Sum { get; set; } + [Required] public OrderStatus Status { get; set; } + [Required] public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + + public virtual Plane Plane { get; set; } + public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } - return new Order + return new Order() { Id = model.Id, PlaneId = model.PlaneId, @@ -42,16 +46,17 @@ namespace AircraftPlantDatabaseImplement.Models DateImplement = model.DateImplement }; } + public void Update(OrderBindingModel? model) { if (model == null) { return; } - Status = model.Status; DateImplement = model.DateImplement; } + public OrderViewModel GetViewModel => new() { Id = Id, @@ -60,7 +65,8 @@ namespace AircraftPlantDatabaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + PlaneName = Plane.PlaneName }; } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantView/FormMain.cs b/AircraftPlant/AircraftPlantView/FormMain.cs index ce50e8d..1260c36 100644 --- a/AircraftPlant/AircraftPlantView/FormMain.cs +++ b/AircraftPlant/AircraftPlantView/FormMain.cs @@ -119,7 +119,7 @@ namespace AircraftPlantView /// /// /// - private void ButtonOrderReady_Click(object sender, EventArgs e) + private void buttonOrderReady_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { @@ -127,7 +127,7 @@ namespace AircraftPlantView _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); try { - var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); + var operationResult = _logic.DeliveryOrder(new OrderBindingModel { Id = id }); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -141,6 +141,7 @@ namespace AircraftPlantView } } } + /// /// Кнопка "Заказ выдан" /// -- 2.25.1 From 4fd0b3c72c3630b4631d0dc108f7cdb3e73971f9 Mon Sep 17 00:00:00 2001 From: "safiulova.k" Date: Tue, 26 Mar 2024 21:57:02 +0400 Subject: [PATCH 05/10] =?UTF-8?q?=D1=87=D1=83=D1=82=D1=8C=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=8F=20=D0=BD=D0=B5=20=D1=80=D0=BE=D0=B4=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=81=203=20=D0=BB=D0=B0=D0=B1=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AircraftPlant/AircraftPlantView/FormMain.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AircraftPlant/AircraftPlantView/FormMain.cs b/AircraftPlant/AircraftPlantView/FormMain.cs index 1260c36..3670437 100644 --- a/AircraftPlant/AircraftPlantView/FormMain.cs +++ b/AircraftPlant/AircraftPlantView/FormMain.cs @@ -119,7 +119,7 @@ namespace AircraftPlantView /// /// /// - private void buttonOrderReady_Click(object sender, EventArgs e) + private void ButtonOrderReady_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { @@ -127,7 +127,7 @@ namespace AircraftPlantView _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); try { - var operationResult = _logic.DeliveryOrder(new OrderBindingModel { Id = id }); + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); -- 2.25.1 From 9be26628db36004fc6989eec69b25654ce5c7145 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 13:48:15 +0400 Subject: [PATCH 06/10] =?UTF-8?q?=D1=87=D1=91=D1=82=D0=BE=20=D0=BD=D0=B5?= =?UTF-8?q?=20=D0=BF=D0=B0=D1=88=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftPlantDatabase.cs | 2 + .../Implements/ShopStorage.cs | 170 ++++++++++++ .../20240621094116_NewMigration.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20240621094116_NewMigration.cs | 78 ++++++ .../AircraftPlantDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 111 ++++++++ .../Models/ShopPlane.cs | 22 ++ AircraftPlant/AircraftPlantView/Program.cs | 3 +- 8 files changed, 709 insertions(+), 2 deletions(-) create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Implements/ShopStorage.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs create mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Models/ShopPlane.cs diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs index 5294c0a..2150817 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs @@ -23,5 +23,7 @@ namespace AircraftPlantDatabaseImplement public virtual DbSet Planes { set; get; } public virtual DbSet PlaneComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPlanes { set; get; } } } diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ShopStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..7307bbd --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,170 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDatabaseImplement.Models; +using AircraftPlantDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new AircraftPlantDatabase(); + return context.Shops + .Include(x => x.Planes) + .ThenInclude(x => x.Plane) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new AircraftPlantDatabase(); + return context.Shops + .Include(x => x.Planes) + .ThenInclude(x => x.Plane) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + using var context = new AircraftPlantDatabase(); + return context.Shops + .Include(x => x.Planes) + .ThenInclude(x => x.Plane) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && + x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new AircraftPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + + shop.Update(model); + context.SaveChanges(); + shop.UpdatePlanes(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new AircraftPlantDatabase(); + var element = context.Shops + .Include(x => x.Planes) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellPlanes(IPlaneModel model, int count) + { + using var context = new AircraftPlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops + .Include(x => x.Planes) + .ThenInclude(x => x.Plane) + .ToList() + .Where(x => x.ShopPlanes.ContainsKey(model.Id)); + + foreach (var shop in shops) + { + int countInCurrentShop = shop.ShopPlanes[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopPlanes + .Where(x => x.PlaneId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopPlanes.Remove(elem); + shop.ShopPlanes.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ShopPlanes[model.Id] = (shop.ShopPlanes[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdatePlanes(context, new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopPlanes = shop.ShopPlanes, + MaxPlanes = shop.MaxPlanes + }); + } + if (count <= 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + public bool CheckCount(IPlaneModel model, int count) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs new file mode 100644 index 0000000..1b9363f --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using AircraftPlantDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AircraftPlantDatabaseImplement.Migrations +{ + [DbContext(typeof(AircraftPlantDatabase))] + [Migration("20240621094116_NewMigration")] + partial class NewMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.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("AircraftPlantDatabaseImplement.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("PlaneId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PlaneId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PlaneName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Planes"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PlaneId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PlaneId"); + + b.ToTable("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxPlanes") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PlaneId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PlaneId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPlanes"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany("Orders") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Plane"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Component", "Component") + .WithMany("PlaneComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany("Components") + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Plane"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany() + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AircraftPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Planes") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Plane"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b => + { + b.Navigation("PlaneComponents"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Planes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs new file mode 100644 index 0000000..2f08a64 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AircraftPlantDatabaseImplement.Migrations +{ + /// + public partial class NewMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxPlanes = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopPlanes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + PlaneId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPlanes", x => x.Id); + table.ForeignKey( + name: "FK_ShopPlanes_Planes_PlaneId", + column: x => x.PlaneId, + principalTable: "Planes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPlanes_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPlanes_PlaneId", + table: "ShopPlanes", + column: "PlaneId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPlanes_ShopId", + table: "ShopPlanes", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPlanes"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs index a65fd13..535ac0e 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace AircraftPlantDatabaseImplement.Migrations b.ToTable("PlaneComponents"); }); + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxPlanes") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PlaneId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PlaneId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPlanes"); + }); + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => { b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") @@ -151,6 +204,25 @@ namespace AircraftPlantDatabaseImplement.Migrations b.Navigation("Plane"); }); + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b => + { + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + .WithMany() + .HasForeignKey("PlaneId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AircraftPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Planes") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Plane"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b => { b.Navigation("PlaneComponents"); @@ -162,6 +234,11 @@ namespace AircraftPlantDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Planes"); + }); #pragma warning restore 612, 618 } } diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..86f99b5 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs @@ -0,0 +1,111 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + private Dictionary? _shopPlanes = null; + + [NotMapped] + public Dictionary ShopPlanes + { + get + { + if (_shopPlanes == null) + { + _shopPlanes = Planes + .ToDictionary(recSP => recSP.PlaneId, recSP => (recSP.Plane as IPlaneModel, recSP.Count)); + } + return _shopPlanes; + } + } + public int MaxPlanes { get; set; } + + [ForeignKey("ShopId")] + public List Planes { get; set; } = new(); + + public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model) + { + return new Shop + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxPlanes = model.MaxPlanes, + Planes = model.ShopPlanes.Select(x => new ShopPlane + { + Plane = context.Planes.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxPlanes = model.MaxPlanes; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopPlanes = ShopPlanes, + MaxPlanes = MaxPlanes + }; + public void UpdatePlanes(AircraftPlantDatabase context, ShopBindingModel model) + { + var shopPlanes = context.ShopPlanes.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPlanes != null && shopPlanes.Count > 0) + { + // Удаление изделий, которых нет в магазине + context.ShopPlanes.RemoveRange(shopPlanes.Where(rec => !model.ShopPlanes.ContainsKey(rec.PlaneId))); + context.SaveChanges(); + // Обновление количества у существующих записей + foreach (var updatePlanes in shopPlanes) + { + updatePlanes.Count = model.ShopPlanes[updatePlanes.PlaneId].Item2; + model.ShopPlanes.Remove(updatePlanes.PlaneId); + } + context.SaveChanges(); + } + + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopPlanes) + { + context.ShopPlanes.Add(new ShopPlane + { + Shop = shop, + Plane = context.Planes.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopPlanes = null; + } + } +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/ShopPlane.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/ShopPlane.cs new file mode 100644 index 0000000..9af624b --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/ShopPlane.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement.Models +{ + public class ShopPlane + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int PlaneId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Plane Plane { get; set; } = new(); + } +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantView/Program.cs b/AircraftPlant/AircraftPlantView/Program.cs index aa71485..216dece 100644 --- a/AircraftPlant/AircraftPlantView/Program.cs +++ b/AircraftPlant/AircraftPlantView/Program.cs @@ -1,8 +1,7 @@ using AircraftPlantBusinessLogic.BusinessLogics; using AircraftPlantContracts.BusinessLogicsContracts; using AircraftPlantContracts.StoragesContracts; -using AircraftPlantFileImplement; -using AircraftPlantFileImplement.Implements; +using AircraftPlantDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From e56dbb7f4b8c5c55b776eed1cff1e42307f7d328 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 14:01:04 +0400 Subject: [PATCH 07/10] =?UTF-8?q?=D0=BF=D0=B0=D1=88=D0=B5=D1=82=20=D0=BF?= =?UTF-8?q?=D0=B0=D1=88=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractShopListImplement/OrderStorage.cs | 15 +++-- .../Implements/ComponentStorage.cs | 33 +++++++--- .../Implements/OrderStorage.cs | 63 +++++++++---------- .../Implements/PlaneStorage.cs | 49 ++++++++------- .../Models/Component.cs | 17 +++-- .../Models/Order.cs | 26 ++++---- .../Models/Plane.cs | 23 ++++--- .../Models/Shop.cs | 9 +-- 8 files changed, 126 insertions(+), 109 deletions(-) diff --git a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs index 2dd4cfc..cdd1b93 100644 --- a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs +++ b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs @@ -28,6 +28,7 @@ namespace AircraftPlantListImplement.Implements { _source = DataListSingleton.GetInstance(); } + /// /// Получение полного списка /// @@ -41,6 +42,7 @@ namespace AircraftPlantListImplement.Implements } return result; } + /// /// Получение фильтрованного списка /// @@ -63,6 +65,7 @@ namespace AircraftPlantListImplement.Implements } return result; } + /// /// Получение элемента /// @@ -84,6 +87,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Добавление элемента /// @@ -109,6 +113,7 @@ namespace AircraftPlantListImplement.Implements _source.Orders.Add(newOrder); return GetViewModel(newOrder); } + /// /// Редактирование элемента /// @@ -126,6 +131,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Удаление элемента /// @@ -144,6 +150,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Получение модели заказа /// @@ -152,15 +159,15 @@ namespace AircraftPlantListImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var plane in _source.Planes) + foreach (var sushi in _source.Planes) { - if (plane.Id == order.PlaneId) + if (sushi.Id == order.PlaneId) { - viewModel.PlaneName = plane.PlaneName; + viewModel.PlaneName = sushi.PlaneName; break; } } return viewModel; } } -} \ No newline at end of file +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs index c36a155..39f7cf5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs @@ -16,7 +16,9 @@ namespace AircraftPlantDatabaseImplement.Implements public List GetFullList() { using var context = new AircraftPlantDatabase(); - return context.Components.Select(x => x.GetViewModel).ToList(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(ComponentSearchModel model) { @@ -24,8 +26,12 @@ namespace AircraftPlantDatabaseImplement.Implements { return new(); } + using var context = new AircraftPlantDatabase(); - return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -33,9 +39,13 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); - return context.Components.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == - model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + 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) { @@ -44,6 +54,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); context.Components.Add(newComponent); context.SaveChanges(); @@ -57,6 +68,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + component.Update(model); context.SaveChanges(); return component.GetViewModel; @@ -65,13 +77,14 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs index cad61b5..a38c526 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -3,7 +3,11 @@ using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; using AircraftPlantDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Implements { @@ -13,38 +17,32 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plane) - .Select(x => x.GetViewModel) - .ToList(); + .Select(x => GetViewModel(x)) + .ToList(); } - public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) { return new(); } + using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plane) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); } - public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } - using var context = new AircraftPlantDatabase(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } + using var context = new AircraftPlantDatabase(); + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -52,15 +50,12 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; + return GetViewModel(newOrder); } - public OrderViewModel? Update(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); @@ -69,29 +64,33 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + order.Update(model); context.SaveChanges(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; + return GetViewModel(order); } - public OrderViewModel? Delete(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { - var deletedElement = context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return deletedElement; + return GetViewModel(element); } return null; } + private static OrderViewModel GetViewModel(Order order) + { + using var context = new AircraftPlantDatabase(); + var viewModel = order.GetViewModel; + var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId); + if (plane != null) + { + viewModel.PlaneName = plane.PlaneName; + } + return viewModel; + } } } \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs index 8befae3..a4d6007 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs @@ -18,11 +18,11 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(PlaneSearchModel model) { @@ -30,31 +30,31 @@ namespace AircraftPlantDatabaseImplement.Implements { return new(); } + using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.PlaneName.Contains(model.PlaneName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PlaneName.Contains(model.PlaneName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public PlaneViewModel? GetElement(PlaneSearchModel model) { - if (string.IsNullOrEmpty(model.PlaneName) && - !model.Id.HasValue) + if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue) { return null; } + using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && - x.PlaneName == model.PlaneName) || - (model.Id.HasValue && x.Id == - model.Id)) - ?.GetViewModel; + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && + x.PlaneName == model.PlaneName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public PlaneViewModel? Insert(PlaneBindingModel model) { @@ -64,6 +64,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + context.Planes.Add(newPlane); context.SaveChanges(); return newPlane.GetViewModel; @@ -74,12 +75,12 @@ namespace AircraftPlantDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var plane = context.Planes.FirstOrDefault(rec => - rec.Id == model.Id); + var plane = context.Planes.FirstOrDefault(rec => rec.Id == model.Id); if (plane == null) { return null; } + plane.Update(model); context.SaveChanges(); plane.UpdateComponents(context, model); @@ -96,8 +97,8 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); var element = context.Planes - .Include(x => x.Components) - .FirstOrDefault(rec => rec.Id == model.Id); + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Planes.Remove(element); diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs index 7ffeebf..e7710c4 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs @@ -5,28 +5,29 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantDatabaseImplement.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 PlaneComponents { get; set; } = new(); - public static Component? Create(ComponentBindingModel model) { if (model == null) { return null; } + return new Component() { Id = model.Id, @@ -34,27 +35,25 @@ namespace AircraftPlantDatabaseImplement.Models Cost = model.Cost }; } - public static Component Create(ComponentViewModel model) { - return new Component + 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, diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs index 64e7110..c308568 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs @@ -2,40 +2,40 @@ using AircraftPlantContracts.ViewModels; using AircraftPlantDataModels.Enums; using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Models { + /// + /// Сущность "Заказ" + /// public class Order : IOrderModel { public int Id { get; set; } - [Required] public int PlaneId { get; set; } - [Required] public int Count { get; set; } - [Required] public double Sum { get; set; } - [Required] public OrderStatus Status { get; set; } - [Required] public DateTime DateCreate { get; set; } - public DateTime? DateImplement { get; set; } - - public virtual Plane Plane { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } - return new Order() + + return new Order { Id = model.Id, PlaneId = model.PlaneId, @@ -46,17 +46,16 @@ namespace AircraftPlantDatabaseImplement.Models DateImplement = model.DateImplement }; } - public void Update(OrderBindingModel? model) { if (model == null) { return; } + Status = model.Status; DateImplement = model.DateImplement; } - public OrderViewModel GetViewModel => new() { Id = Id, @@ -65,8 +64,7 @@ namespace AircraftPlantDatabaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement, - PlaneName = Plane.PlaneName + DateImplement = DateImplement }; } } \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs index 3108c85..0b047e5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs @@ -1,13 +1,13 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +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 AircraftPlantContracts.BindingModels; -using AircraftPlantContracts.ViewModels; -using AircraftPlantDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; namespace AircraftPlantDatabaseImplement.Models { @@ -19,6 +19,7 @@ namespace AircraftPlantDatabaseImplement.Models [Required] public double Price { get; set; } private Dictionary? _planeComponents = null; + [NotMapped] public Dictionary PlaneComponents { @@ -27,7 +28,7 @@ namespace AircraftPlantDatabaseImplement.Models if (_planeComponents == null) { _planeComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); } return _planeComponents; } @@ -55,6 +56,7 @@ namespace AircraftPlantDatabaseImplement.Models PlaneName = model.PlaneName; Price = model.Price; } + public PlaneViewModel GetViewModel => new() { Id = Id, @@ -66,10 +68,11 @@ namespace AircraftPlantDatabaseImplement.Models { var planeComponents = context.PlaneComponents.Where(rec => rec.PlaneId == model.Id).ToList(); if (planeComponents != null && planeComponents.Count > 0) - { // удалили те, которых нет в модели + { + // Удаление компонентов, которых нет в модели context.PlaneComponents.RemoveRange(planeComponents.Where(rec => !model.PlaneComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); - // обновили количество у существующих записей + // Обновление количества у существующих записей foreach (var updateComponent in planeComponents) { updateComponent.Count = model.PlaneComponents[updateComponent.ComponentId].Item2; @@ -91,4 +94,4 @@ namespace AircraftPlantDatabaseImplement.Models _planeComponents = null; } } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs index 86f99b5..15b6801 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs @@ -12,19 +12,18 @@ using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Models { + /// + /// Сущность "Магазин" + /// public class Shop : IShopModel { public int Id { get; set; } - [Required] public string ShopName { get; set; } = string.Empty; - [Required] public string Address { get; set; } = string.Empty; - [Required] public DateTime DateOpening { get; set; } - private Dictionary? _shopPlanes = null; [NotMapped] @@ -41,10 +40,8 @@ namespace AircraftPlantDatabaseImplement.Models } } public int MaxPlanes { get; set; } - [ForeignKey("ShopId")] public List Planes { get; set; } = new(); - public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model) { return new Shop -- 2.25.1 From 86427cd141916f1d3fb8d224cbb8fef4304034c2 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 14:02:46 +0400 Subject: [PATCH 08/10] =?UTF-8?q?=D0=BB=D0=BE=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AircraftPlant/AbstractShopListImplement/OrderStorage.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs index cdd1b93..6ae4819 100644 --- a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs +++ b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs @@ -159,11 +159,11 @@ namespace AircraftPlantListImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var sushi in _source.Planes) + foreach (var plane in _source.Planes) { - if (sushi.Id == order.PlaneId) + if (plane.Id == order.PlaneId) { - viewModel.PlaneName = sushi.PlaneName; + viewModel.PlaneName = plane.PlaneName; break; } } -- 2.25.1 From 9e079e969f71b847eee396a08960d4ab7e8fc4e0 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 14:04:27 +0400 Subject: [PATCH 09/10] =?UTF-8?q?=D1=88=D0=B0=D0=BB=D1=8E=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=BC=D0=BD=D0=BE=D0=B6=D0=BA=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AircraftPlant/AbstractShopListImplement/OrderStorage.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs index 6ae4819..2dd4cfc 100644 --- a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs +++ b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs @@ -28,7 +28,6 @@ namespace AircraftPlantListImplement.Implements { _source = DataListSingleton.GetInstance(); } - /// /// Получение полного списка /// @@ -42,7 +41,6 @@ namespace AircraftPlantListImplement.Implements } return result; } - /// /// Получение фильтрованного списка /// @@ -65,7 +63,6 @@ namespace AircraftPlantListImplement.Implements } return result; } - /// /// Получение элемента /// @@ -87,7 +84,6 @@ namespace AircraftPlantListImplement.Implements } return null; } - /// /// Добавление элемента /// @@ -113,7 +109,6 @@ namespace AircraftPlantListImplement.Implements _source.Orders.Add(newOrder); return GetViewModel(newOrder); } - /// /// Редактирование элемента /// @@ -131,7 +126,6 @@ namespace AircraftPlantListImplement.Implements } return null; } - /// /// Удаление элемента /// @@ -150,7 +144,6 @@ namespace AircraftPlantListImplement.Implements } return null; } - /// /// Получение модели заказа /// @@ -170,4 +163,4 @@ namespace AircraftPlantListImplement.Implements return viewModel; } } -} +} \ No newline at end of file -- 2.25.1 From 80385a8faaf323ad92736ef83f2bfa4c10510505 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 23:40:13 +0400 Subject: [PATCH 10/10] =?UTF-8?q?=D0=B1=D0=B4=20=D1=88=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftPlantDatabase.cs | 2 +- .../20240326170504_InitialCreate.Designer.cs | 171 ------------------ .../Migrations/20240621094116_NewMigration.cs | 78 -------- ... 20240621193713_InitMigration.Designer.cs} | 8 +- ...ate.cs => 20240621193713_InitMigration.cs} | 61 ++++++- .../AircraftPlantDatabaseModelSnapshot.cs | 4 +- AircraftPlant/AircraftPlantView/Program.cs | 2 +- 7 files changed, 66 insertions(+), 260 deletions(-) delete mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs delete mode 100644 AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs rename AircraftPlant/AircraftPlantDatabaseImplement/Migrations/{20240621094116_NewMigration.Designer.cs => 20240621193713_InitMigration.Designer.cs} (98%) rename AircraftPlant/AircraftPlantDatabaseImplement/Migrations/{20240326170504_InitialCreate.cs => 20240621193713_InitMigration.cs} (65%) diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs index 2150817..edb7da1 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs @@ -15,7 +15,7 @@ namespace AircraftPlantDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6QDRI0N\SQLEXPRESS;Initial Catalog=AircraftPlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6QDRI0N\SQLEXPRESS;Initial Catalog=AircraftPlantDatabase10;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs deleted file mode 100644 index eac223c..0000000 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.Designer.cs +++ /dev/null @@ -1,171 +0,0 @@ -// -using System; -using AircraftPlantDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace AircraftPlantDatabaseImplement.Migrations -{ - [DbContext(typeof(AircraftPlantDatabase))] - [Migration("20240326170504_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.17") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.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("AircraftPlantDatabaseImplement.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("PlaneId") - .HasColumnType("int"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("PlaneId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("PlaneName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Planes"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("PlaneId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("PlaneId"); - - b.ToTable("PlaneComponents"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => - { - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") - .WithMany("Orders") - .HasForeignKey("PlaneId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Plane"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => - { - b.HasOne("AircraftPlantDatabaseImplement.Models.Component", "Component") - .WithMany("PlaneComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") - .WithMany("Components") - .HasForeignKey("PlaneId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Plane"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b => - { - b.Navigation("PlaneComponents"); - }); - - modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs deleted file mode 100644 index 2f08a64..0000000 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace AircraftPlantDatabaseImplement.Migrations -{ - /// - public partial class NewMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Shops", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ShopName = table.Column(type: "nvarchar(max)", nullable: false), - Address = table.Column(type: "nvarchar(max)", nullable: false), - DateOpening = table.Column(type: "datetime2", nullable: false), - MaxPlanes = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Shops", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "ShopPlanes", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ShopId = table.Column(type: "int", nullable: false), - PlaneId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ShopPlanes", x => x.Id); - table.ForeignKey( - name: "FK_ShopPlanes_Planes_PlaneId", - column: x => x.PlaneId, - principalTable: "Planes", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_ShopPlanes_Shops_ShopId", - column: x => x.ShopId, - principalTable: "Shops", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_ShopPlanes_PlaneId", - table: "ShopPlanes", - column: "PlaneId"); - - migrationBuilder.CreateIndex( - name: "IX_ShopPlanes_ShopId", - table: "ShopPlanes", - column: "ShopId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "ShopPlanes"); - - migrationBuilder.DropTable( - name: "Shops"); - } - } -} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.Designer.cs similarity index 98% rename from AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs rename to AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.Designer.cs index 1b9363f..cfe7a09 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621094116_NewMigration.Designer.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace AircraftPlantDatabaseImplement.Migrations { [DbContext(typeof(AircraftPlantDatabase))] - [Migration("20240621094116_NewMigration")] - partial class NewMigration + [Migration("20240621193713_InitMigration")] + partial class InitMigration { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -179,13 +179,11 @@ namespace AircraftPlantDatabaseImplement.Migrations modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => { - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) .WithMany("Orders") .HasForeignKey("PlaneId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Plane"); }); modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.cs similarity index 65% rename from AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.cs rename to AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.cs index 7d0e945..b920e56 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240326170504_InitialCreate.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240621193713_InitMigration.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace AircraftPlantDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class InitMigration : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -39,6 +39,22 @@ namespace AircraftPlantDatabaseImplement.Migrations table.PrimaryKey("PK_Planes", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxPlanes = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -90,6 +106,33 @@ namespace AircraftPlantDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopPlanes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + PlaneId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPlanes", x => x.Id); + table.ForeignKey( + name: "FK_ShopPlanes_Planes_PlaneId", + column: x => x.PlaneId, + principalTable: "Planes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPlanes_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_Orders_PlaneId", table: "Orders", @@ -104,6 +147,16 @@ namespace AircraftPlantDatabaseImplement.Migrations name: "IX_PlaneComponents_PlaneId", table: "PlaneComponents", column: "PlaneId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPlanes_PlaneId", + table: "ShopPlanes", + column: "PlaneId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPlanes_ShopId", + table: "ShopPlanes", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace AircraftPlantDatabaseImplement.Migrations migrationBuilder.DropTable( name: "PlaneComponents"); + migrationBuilder.DropTable( + name: "ShopPlanes"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Planes"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs index 535ac0e..dc221f5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/AircraftPlantDatabaseModelSnapshot.cs @@ -176,13 +176,11 @@ namespace AircraftPlantDatabaseImplement.Migrations modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b => { - b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane") + b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) .WithMany("Orders") .HasForeignKey("PlaneId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - - b.Navigation("Plane"); }); modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => diff --git a/AircraftPlant/AircraftPlantView/Program.cs b/AircraftPlant/AircraftPlantView/Program.cs index 216dece..c880187 100644 --- a/AircraftPlant/AircraftPlantView/Program.cs +++ b/AircraftPlant/AircraftPlantView/Program.cs @@ -63,8 +63,8 @@ namespace AircraftPlantView services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); } -- 2.25.1