PIbd-22. Safiulova K.N. LabWork_03_hard #15

Closed
safiulova.k wants to merge 11 commits from LabWork_03_hard into LabWork_02_hard
19 changed files with 1591 additions and 4 deletions

View File

@ -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

View File

@ -0,0 +1,29 @@
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=AircraftPlantDatabase10;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Plane> Planes { set; get; }
public virtual DbSet<PlaneComponent> PlaneComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopPlane> ShopPlanes { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AircraftPlantContracts»\AircraftPlantContracts.csproj" />
<ProjectReference Include="..\AircraftPlantDataModels\AircraftPlantDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,90 @@
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<ComponentViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> 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;
}
}
}

View File

@ -0,0 +1,96 @@
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<OrderViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Orders
.Select(x => GetViewModel(x))
.ToList();
}
public List<OrderViewModel> 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;
}
}
}

View File

@ -0,0 +1,111 @@
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<PlaneViewModel> 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<PlaneViewModel> 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;
}
}
}

View File

@ -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<ShopViewModel> 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<ShopViewModel> 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();
}
}
}

View File

@ -0,0 +1,246 @@
// <auto-generated />
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("20240621193713_InitMigration")]
partial class InitMigration
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PlaneId");
b.ToTable("Orders");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PlaneName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Planes");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PlaneId");
b.ToTable("PlaneComponents");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxPlanes")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.Property<int>("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", 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.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
}
}
}

View File

@ -0,0 +1,184 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AircraftPlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Planes",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PlaneName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Planes", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxPlanes = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PlaneId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PlaneId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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.CreateTable(
name: "ShopPlanes",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopId = table.Column<int>(type: "int", nullable: false),
PlaneId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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",
column: "PlaneId");
migrationBuilder.CreateIndex(
name: "IX_PlaneComponents_ComponentId",
table: "PlaneComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "PlaneComponents");
migrationBuilder.DropTable(
name: "ShopPlanes");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Planes");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -0,0 +1,243 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PlaneId");
b.ToTable("Orders");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PlaneName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Planes");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PlaneId");
b.ToTable("PlaneComponents");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxPlanes")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PlaneId")
.HasColumnType("int");
b.Property<int>("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", 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.ShopPlane", b =>
{
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
.WithMany()
Review

Связь настроена не до конца

Связь настроена не до конца
.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
}
}
}

View File

@ -0,0 +1,64 @@
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; }
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<PlaneComponent> 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
};
}
}

View File

@ -0,0 +1,70 @@
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
{
/// <summary>
/// Сущность "Заказ"
/// </summary>
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
};
}
}

View File

@ -0,0 +1,97 @@
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<int, (IComponentModel, int)>? _planeComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> 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<PlaneComponent> Components { get; set; } = new();
[ForeignKey("PlaneId")]
public virtual List<Order> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,108 @@
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
{
/// <summary>
/// Сущность "Магазин"
/// </summary>
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<int, (IPlaneModel, int)>? _shopPlanes = null;
[NotMapped]
public Dictionary<int, (IPlaneModel, int)> 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<ShopPlane> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
</ItemGroup>
@ -17,6 +21,7 @@
<ProjectReference Include="..\AbstractShopListImplement\AircraftPlantListImplement.csproj" />
<ProjectReference Include="..\AircraftPlantBusinessLogic\AircraftPlantBusinessLogic.csproj" />
<ProjectReference Include="..\AircraftPlantContracts»\AircraftPlantContracts.csproj" />
<ProjectReference Include="..\AircraftPlantDatabaseImplement\AircraftPlantDatabaseImplement.csproj" />
<ProjectReference Include="..\AircraftPlantFileImplement\AircraftPlantFileImplement.csproj" />
</ItemGroup>

View File

@ -154,6 +154,7 @@ namespace AircraftPlantView
}
}
}
/// <summary>
/// Кнопка "Заказ выдан"
/// </summary>

View File

@ -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;
@ -64,8 +63,8 @@ namespace AircraftPlantView
services.AddTransient<FormPlane>();
services.AddTransient<FormPlaneComponent>();
services.AddTransient<FormPlanes>();
services.AddTransient<FormShops>();
services.AddTransient<FormShop>();
services.AddTransient<FormShops>();
services.AddTransient<FormSupply>();
services.AddTransient<FormSell>();
}