Done
This commit is contained in:
parent
457e1e26e3
commit
2327f90082
@ -1,4 +1,10 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantContracts.SearchModels;
|
||||
using MotorPlantContracts.StoragesContracts;
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -6,7 +12,74 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Implements
|
||||
{
|
||||
internal class ComponentStorage
|
||||
public class ComponentStorage : IComponentStorage
|
||||
{
|
||||
public List<ComponentViewModel> GetFullList()
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
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 MotorPlantDatabase();
|
||||
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 MotorPlantDatabase();
|
||||
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 MotorPlantDatabase();
|
||||
context.Components.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
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 MotorPlantDatabase();
|
||||
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Components.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,11 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantContracts.SearchModels;
|
||||
using MotorPlantContracts.StoragesContracts;
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -6,7 +13,95 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Implements
|
||||
{
|
||||
internal class EngineStorage
|
||||
public class EngineStorage : IEngineStorage
|
||||
{
|
||||
public List<EngineViewModel> GetFullList()
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Engines
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<EngineViewModel> GetFilteredList(EngineSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.EngineName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Engines
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.Where(x => x.EngineName.Contains(model.EngineName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public EngineViewModel? GetElement(EngineSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.EngineName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Engines
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.EngineName) && x.EngineName == model.EngineName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public EngineViewModel? Insert(EngineBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
var newEngine = Engine.Create(context, model);
|
||||
if (newEngine == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Engines.Add(newEngine);
|
||||
context.SaveChanges();
|
||||
return newEngine.GetViewModel;
|
||||
}
|
||||
public EngineViewModel? Update(EngineBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var product = context.Engines.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
product.Update(model);
|
||||
context.SaveChanges();
|
||||
product.UpdateComponents(context, model);
|
||||
transaction.Commit();
|
||||
return product.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public EngineViewModel? Delete(EngineBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
var element = context.Engines
|
||||
.Include(x => x.Components)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Engines.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,11 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantContracts.SearchModels;
|
||||
using MotorPlantContracts.StoragesContracts;
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -6,7 +13,73 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Implements
|
||||
{
|
||||
internal class OrderStorage
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Orders.Include(x => x.Engine).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new MotorPlantDatabase();
|
||||
return context.Orders.Include(x => x.Engine).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
if (model == null)
|
||||
return null;
|
||||
var newOrder = Order.Create(context, model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDatabase();
|
||||
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (order != null)
|
||||
{
|
||||
context.Orders.Remove(order);
|
||||
context.SaveChanges();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
171
MotorPlant/MotorPlantDatabaseImplement/Migrations/20240424124024_InitialCreate.Designer.cs
generated
Normal file
171
MotorPlant/MotorPlantDatabaseImplement/Migrations/20240424124024_InitialCreate.Designer.cs
generated
Normal file
@ -0,0 +1,171 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using MotorPlantDatabaseImplement;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(MotorPlantDatabase))]
|
||||
[Migration("20240424124024_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("EngineVersion", "7.0.3")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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("MotorPlantDatabaseImplement.Models.Engine", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("EngineName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Engines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", 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>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.ToTable("EngineComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("EngineComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Engine");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Engine");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("EngineComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : 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: "Engines",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
EngineName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Price = table.Column<double>(type: "float", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Engines", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "EngineComponents",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
EngineId = 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_EngineComponents", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_EngineComponents_Components_ComponentId",
|
||||
column: x => x.ComponentId,
|
||||
principalTable: "Components",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_EngineComponents_Engines_EngineId",
|
||||
column: x => x.EngineId,
|
||||
principalTable: "Engines",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Orders",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
EngineId = 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_Engines_EngineId",
|
||||
column: x => x.EngineId,
|
||||
principalTable: "Engines",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_EngineComponents_ComponentId",
|
||||
table: "EngineComponents",
|
||||
column: "ComponentId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_EngineComponents_EngineId",
|
||||
table: "EngineComponents",
|
||||
column: "EngineId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Orders_EngineId",
|
||||
table: "Orders",
|
||||
column: "EngineId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "EngineComponents");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Components");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Engines");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using MotorPlantDatabaseImplement;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(MotorPlantDatabase))]
|
||||
partial class MotorPlantDatabaseModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("EngineVersion", "7.0.3")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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("MotorPlantDatabaseImplement.Models.Engine", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("EngineName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Engines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", 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>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.ToTable("EngineComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("EngineComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Engine");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Engine");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("EngineComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Engine", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,61 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
internal class Component
|
||||
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<EngineComponent> EngineComponents { 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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,94 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
internal class Engine
|
||||
public class Engine : IEngineModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string EngineName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _EngineComponents = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> EngineComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_EngineComponents == null)
|
||||
{
|
||||
_EngineComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _EngineComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("EngineId")]
|
||||
public virtual List<EngineComponent> Components { get; set; } = new();
|
||||
[ForeignKey("EngineId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
public static Engine Create(MotorPlantDatabase context, EngineBindingModel model)
|
||||
{
|
||||
return new Engine()
|
||||
{
|
||||
Id = model.Id,
|
||||
EngineName = model.EngineName,
|
||||
Price = model.Price,
|
||||
Components = model.EngineComponents.Select(x => new EngineComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(EngineBindingModel model)
|
||||
{
|
||||
EngineName = model.EngineName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public EngineViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
EngineName = EngineName,
|
||||
Price = Price,
|
||||
EngineComponents = EngineComponents
|
||||
};
|
||||
public void UpdateComponents(MotorPlantDatabase context, EngineBindingModel model)
|
||||
{
|
||||
var EngineComponents = context.EngineComponents.Where(rec => rec.EngineId == model.Id).ToList();
|
||||
if (EngineComponents != null && EngineComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.EngineComponents.RemoveRange(EngineComponents.Where(rec => !model.EngineComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in EngineComponents)
|
||||
{
|
||||
updateComponent.Count = model.EngineComponents[updateComponent.ComponentId].Item2;
|
||||
model.EngineComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var Engine = context.Engines.First(x => x.Id == Id);
|
||||
foreach (var pc in model.EngineComponents)
|
||||
{
|
||||
context.EngineComponents.Add(new EngineComponent
|
||||
{
|
||||
Engine = Engine,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_EngineComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,25 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
internal class EngineComponent
|
||||
public class EngineComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int EngineId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Engine Engine { get; set; } = new();
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,74 @@
|
||||
using System;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDataModels.Enums;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
internal class Order
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int EngineId { get; private set; }
|
||||
|
||||
public virtual Engine Engine { get; set; } = new();
|
||||
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public static Order Create(MotorPlantDatabase context, OrderBindingModel model)
|
||||
{
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
EngineId = model.EngineId,
|
||||
Engine = context.Engines.First(x => x.Id == model.EngineId),
|
||||
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,
|
||||
EngineId = EngineId,
|
||||
EngineName = Engine.EngineName,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,20 @@
|
||||
namespace MotorPlantDatabaseImplement
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
namespace MotorPlantDatabaseImplement
|
||||
{
|
||||
public class MotorPlantDatabase
|
||||
public class MotorPlantDatabase : DbContext
|
||||
{
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=MotorPlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
public virtual DbSet<Engine> Engines { set; get; }
|
||||
public virtual DbSet<EngineComponent> EngineComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.29" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.29" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.29">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
@ -9,7 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.29">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
@ -21,6 +21,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MotorPlantBusinessLogics\MotorPlantBusinessLogics.csproj" />
|
||||
<ProjectReference Include="..\MotorPlantContracts\MotorPlantContracts.csproj" />
|
||||
<ProjectReference Include="..\MotorPlantDatabaseImplement\MotorPlantDatabaseImplement.csproj" />
|
||||
<ProjectReference Include="..\MotorPlantFileImplement\MotorPlantFileImplement.csproj" />
|
||||
<ProjectReference Include="..\MotorPlantListImplement\MotorPlantListImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
@ -1,7 +1,7 @@
|
||||
using MotorPlantBusinessLogics.BusinessLogics;
|
||||
using MotorPlantContracts.BusinessLogicsContracts;
|
||||
using MotorPlantContracts.StoragesContracts;
|
||||
using MotorPlantFileImplement.Implements;
|
||||
using MotorPlantDatabaseImplement.Implements;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
|
Loading…
Reference in New Issue
Block a user