diff --git a/AircraftPlant/AircraftPlant.sln b/AircraftPlant/AircraftPlant.sln index a769c0c..52cb0a9 100644 --- a/AircraftPlant/AircraftPlant.sln +++ b/AircraftPlant/AircraftPlant.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AircraftPlantListImplement" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantFileImplement", "AircraftPlantFileImplement\AircraftPlantFileImplement.csproj", "{4456EC84-CEFC-419B-9A30-F1EE409120E4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantDatabaseImplement", "AircraftPlantDatabaseImplement\AircraftPlantDatabaseImplement.csproj", "{F10A6166-F5D7-4DE4-A991-19E7D51C624C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {4456EC84-CEFC-419B-9A30-F1EE409120E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {4456EC84-CEFC-419B-9A30-F1EE409120E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {4456EC84-CEFC-419B-9A30-F1EE409120E4}.Release|Any CPU.Build.0 = Release|Any CPU + {F10A6166-F5D7-4DE4-A991-19E7D51C624C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F10A6166-F5D7-4DE4-A991-19E7D51C624C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F10A6166-F5D7-4DE4-A991-19E7D51C624C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F10A6166-F5D7-4DE4-A991-19E7D51C624C}.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..549c5f6 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/AircraftPlantDatabase.cs @@ -0,0 +1,49 @@ +using AircraftPlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantDatabaseImplement +{ + /// + /// Класс для взаимодействия с базой данных + /// + public class AircraftPlantDatabase : DbContext + { + /// + /// Подключение к базе данных + /// + /// + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=FACTORINO\SQLEXPRESS;Initial Catalog=AircraftPlantDatabase;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..6881766 --- /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..84d70fe --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,127 @@ +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) + { + return null; + } + + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..fc05f33 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,140 @@ +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..8d299b1 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs @@ -0,0 +1,148 @@ +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; + } + } +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240309195916_InitialCreate.Designer.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240309195916_InitialCreate.Designer.cs new file mode 100644 index 0000000..d4060e2 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240309195916_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("20240309195916_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("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/20240309195916_InitialCreate.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240309195916_InitialCreate.cs new file mode 100644 index 0000000..7d0e945 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240309195916_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..2f4a9d4 --- /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.3") + .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..4adbba0 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs @@ -0,0 +1,104 @@ +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.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, + 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..29f1df4 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,112 @@ +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..920d429 --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs @@ -0,0 +1,145 @@ +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; + +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..57ab42b --- /dev/null +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/PlaneComponent.cs @@ -0,0 +1,48 @@ +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 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 3c2b7e3..d6dc72f 100644 --- a/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj +++ b/AircraftPlant/AircraftPlantView/AircraftPlantView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -27,6 +31,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;