Morozov V.S. LabWork3 #3

Closed
VoldemarProger wants to merge 3 commits from LabWork3 into LabWork2
18 changed files with 1116 additions and 5 deletions

View File

@ -58,7 +58,7 @@ namespace AutomobilePlantBusinessLogic.BusinessLogics
model.Status = newStatus;
if (model.Status == OrderStatus.Выдан)
model.DateImplement = DateTime.Now;
model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
if (_orderStorage.Update(model) == null)
{

View File

@ -16,7 +16,7 @@ namespace AutomobilePlantContracts.BindingModels
public int Count { get; set; }
public double Sum { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
public DateTime? DateImplement { get; set; }
}
}

View File

@ -23,7 +23,7 @@ namespace AutomobilePlantContracts.ViewModel
[DisplayName("Статус")]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[DisplayName("Дата создания")]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
[DisplayName("Дата выполнения")]
public DateTime? DateImplement { get; set; }

View File

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlant", "Automobi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomomilePlantFileImplement", "AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj", "{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDataBaseImplements", "AutomobilePlantDataBaseImplements\AutomobilePlantDataBaseImplements.csproj", "{166A9D0A-A545-4634-811B-7BC2BC33631D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.Build.0 = Release|Any CPU
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -19,12 +19,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AbstractAutoBusinessLogic\AutomobilePlantBusinessLogic.csproj" />
<ProjectReference Include="..\AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj" />
<ProjectReference Include="..\AutomobilePlantDataBaseImplements\AutomobilePlantDataBaseImplements.csproj" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@
using AutomobilePlantBusinessLogic.BusinessLogics;
using AutomobilePlantContracts.BusinessLogicsContracts;
using AutomobilePlantContracts.StoragesContracts;
using AutomomilePlantFileImplement.Implements;
using AutomobilePlantDataBaseImplements.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

View File

@ -0,0 +1,31 @@
using AutomobilePlantDataBaseImplements.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataBaseImplements
{
public class AutoPlantDataBase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql("Server=PostgreSQL;Host=localhost;Port=5432;Database=AutoPlantDataBase;Username=postgres;Password=postgres");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Car> Cars { set; get; }
public virtual DbSet<CarComponent> CarComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,22 @@
<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.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AbstractAutoContracts\AutomobilePlantContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,111 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.StoragesContracts;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataBaseImplements.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataBaseImplements.Implements
{
public class CarStorage : ICarStorage
{
public List<CarViewModel> GetFullList()
{
using var context = new AutoPlantDataBase();
return context.Cars
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<CarViewModel> GetFilteredList(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.CarName))
{
return new();
}
using var context = new AutoPlantDataBase();
return context.Cars
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.CarName.Contains(model.CarName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public CarViewModel? GetElement(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue)
{
return null;
}
using var context = new AutoPlantDataBase();
return context.Cars
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarName) && x.CarName == model.CarName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public CarViewModel? Insert(CarBindingModel model)
{
using var context = new AutoPlantDataBase();
var newCar = Car.Create(context, model);
if (newCar == null)
{
return null;
}
context.Cars.Add(newCar);
context.SaveChanges();
return newCar.GetViewModel;
}
public CarViewModel? Update(CarBindingModel model)
{
using var context = new AutoPlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var car = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);
if (car == null)
{
return null;
}
car.Update(model);
context.SaveChanges();
car.UpdateComponents(context, model);
transaction.Commit();
return car.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CarViewModel? Delete(CarBindingModel model)
{
using var context = new AutoPlantDataBase();
var element = context.Cars
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cars.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.StoragesContracts;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataBaseImplements.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new AutoPlantDataBase();
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 AutoPlantDataBase();
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 AutoPlantDataBase();
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 AutoPlantDataBase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new AutoPlantDataBase();
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 AutoPlantDataBase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,102 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModel;
using AutomobilePlantContracts.StoragesContracts;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataBaseImplements.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataBaseImplements.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new AutoPlantDataBase();
return context.Orders
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (model.Id.HasValue)
{
return new();
}
using var context = new AutoPlantDataBase();
return context.Orders
.Where(x => x.Id == model.Id)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AutoPlantDataBase();
return context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new AutoPlantDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new AutoPlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
transaction.Commit();
return order.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new AutoPlantDataBase();
var element = context.Orders
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,175 @@
// <auto-generated />
using System;
using AutomobilePlantDataBaseImplements;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutomobilePlantDataBaseImplements.Migrations
{
[DbContext(typeof(AutoPlantDataBase))]
[Migration("20230419145215_InitialMigration")]
partial class InitialMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Car", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("CarName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Cars");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.CarComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CarId")
.HasColumnType("integer");
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("CarId");
b.HasIndex("ComponentId");
b.ToTable("CarComponents");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CarId")
.HasColumnType("integer");
b.Property<string>("CarName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp with time zone");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CarId");
b.ToTable("Orders");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.CarComponent", b =>
{
b.HasOne("AutomobilePlantDataBaseImplements.Models.Car", "Car")
.WithMany("Components")
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutomobilePlantDataBaseImplements.Models.Component", "Component")
.WithMany("CarComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Car");
b.Navigation("Component");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Order", b =>
{
b.HasOne("AutomobilePlantDataBaseImplements.Models.Car", "car")
.WithMany("Orders")
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("car");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Car", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Component", b =>
{
b.Navigation("CarComponents");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,127 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutomobilePlantDataBaseImplements.Migrations
{
/// <inheritdoc />
public partial class InitialMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Cars",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CarName = table.Column<string>(type: "text", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Cars", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ComponentName = table.Column<string>(type: "text", nullable: false),
Cost = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CarId = table.Column<int>(type: "integer", nullable: false),
CarName = table.Column<string>(type: "text", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false),
Sum = table.Column<double>(type: "double precision", nullable: false),
Status = table.Column<int>(type: "integer", nullable: false),
DateCreate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
DateImplement = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Cars_CarId",
column: x => x.CarId,
principalTable: "Cars",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "CarComponents",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
CarId = table.Column<int>(type: "integer", nullable: false),
ComponentId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CarComponents", x => x.Id);
table.ForeignKey(
name: "FK_CarComponents_Cars_CarId",
column: x => x.CarId,
principalTable: "Cars",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_CarComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_CarComponents_CarId",
table: "CarComponents",
column: "CarId");
migrationBuilder.CreateIndex(
name: "IX_CarComponents_ComponentId",
table: "CarComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_Orders_CarId",
table: "Orders",
column: "CarId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CarComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Cars");
}
}
}

View File

@ -0,0 +1,172 @@
// <auto-generated />
using System;
using AutomobilePlantDataBaseImplements;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutomobilePlantDataBaseImplements.Migrations
{
[DbContext(typeof(AutoPlantDataBase))]
partial class AutoPlantDataBaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Car", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("CarName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Cars");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.CarComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CarId")
.HasColumnType("integer");
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("CarId");
b.HasIndex("ComponentId");
b.ToTable("CarComponents");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CarId")
.HasColumnType("integer");
b.Property<string>("CarName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp with time zone");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("CarId");
b.ToTable("Orders");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.CarComponent", b =>
{
b.HasOne("AutomobilePlantDataBaseImplements.Models.Car", "Car")
.WithMany("Components")
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutomobilePlantDataBaseImplements.Models.Component", "Component")
.WithMany("CarComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Car");
b.Navigation("Component");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Order", b =>
{
b.HasOne("AutomobilePlantDataBaseImplements.Models.Car", "car")
.WithMany("Orders")
.HasForeignKey("CarId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("car");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Car", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("AutomobilePlantDataBaseImplements.Models.Component", b =>
{
b.Navigation("CarComponents");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,104 @@
using AutomobilePlantDataModels.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;
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModel;
namespace AutomobilePlantDataBaseImplements.Models
{
public class Car : ICarModel
{
public int Id { get; set; }
[Required]
public string CarName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _carComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> CarComponents
{
get
{
if (_carComponents == null)
{
_carComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _carComponents;
}
}
[ForeignKey("CarId")]
public virtual List<CarComponent> Components { get; set; } = new();
[ForeignKey("CarId")]
public virtual List<Order> Orders { get; set; } = new();
public static Car Create(AutoPlantDataBase context, CarBindingModel model)
{
return new Car()
{
Id = model.Id,
CarName = model.CarName,
Price = model.Price,
Components = model.CarComponents.Select(x => new CarComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(CarBindingModel model)
{
CarName = model.CarName;
Price = model.Price;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
CarName = CarName,
Price = Price,
CarComponents = CarComponents
};
public void UpdateComponents(AutoPlantDataBase context, CarBindingModel model)
{
var carComponents = context.CarComponents.Where(rec => rec.CarId == model.Id).ToList();
if (carComponents != null && carComponents.Count > 0)
{
context.CarComponents.RemoveRange(carComponents.Where(rec => !model.CarComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in carComponents)
{
updateComponent.Count = model.CarComponents[updateComponent.ComponentId].Item2;
model.CarComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var car = context.Cars.First(x => x.Id == Id);
foreach (var pc in model.CarComponents)
{
context.CarComponents.Add(new CarComponent
{
Car = car,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_carComponents = null;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDataBaseImplements.Models
{
public class CarComponent
{
public int Id { get; set; }
[Required]
public int CarId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Car Car { get; set; } = new();
}
}

View File

@ -0,0 +1,68 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataModels.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 AutomobilePlantDataBaseImplements.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<CarComponent> CarComponents { 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,73 @@
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModel;
using AutomobilePlantDataModels.Enums;
using AutomobilePlantDataModels.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 AutomobilePlantDataBaseImplements.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int CarId { get; private set; }
public string CarName { get; private set; }
Review

Вычисляемое поле, не хранится в памяти

Вычисляемое поле, не хранится в памяти
public Car car { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
CarId = model.CarId,
CarName = model.CarName,
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,
CarId = CarId,
CarName = CarName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}