Merge branch 'LabWork03_Base' into LabWork03_Hard

This commit is contained in:
Павел Сорокин 2023-03-19 15:15:44 +04:00
commit 572b93667a
20 changed files with 1128 additions and 2 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardBusinessLogic", "Sh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardListImplement", "ShipyardListImplement»\ShipyardListImplement.csproj", "{47C73846-F085-4EB5-A8F4-78D1379AA405}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardFileImplement", "ShipyardFileImplement\ShipyardFileImplement.csproj", "{924C876B-5BF9-487B-8743-50219FDEC743}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardFileImplement", "ShipyardFileImplement\ShipyardFileImplement.csproj", "{924C876B-5BF9-487B-8743-50219FDEC743}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardDataBaseImplement", "ShipyardDataBaseImplement\ShipyardDataBaseImplement.csproj", "{8EE572CC-9CB0-4DAC-B396-FAC4234E416A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{924C876B-5BF9-487B-8743-50219FDEC743}.Debug|Any CPU.Build.0 = Debug|Any CPU
{924C876B-5BF9-487B-8743-50219FDEC743}.Release|Any CPU.ActiveCfg = Release|Any CPU
{924C876B-5BF9-487B-8743-50219FDEC743}.Release|Any CPU.Build.0 = Release|Any CPU
{8EE572CC-9CB0-4DAC-B396-FAC4234E416A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EE572CC-9CB0-4DAC-B396-FAC4234E416A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EE572CC-9CB0-4DAC-B396-FAC4234E416A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EE572CC-9CB0-4DAC-B396-FAC4234E416A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -4,7 +4,7 @@ using NLog.Extensions.Logging;
using ShipyardBusinessLogic.BusinessLogics;
using ShipyardContracts.BusinessLogicsContracts;
using ShipyardContracts.StoragesContracts;
using ShipyardFileImplement.Implements;
using ShipyardDataBaseImplement.Implements;
namespace ShipyardView
{

View File

@ -9,6 +9,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<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>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
@ -16,6 +26,7 @@
<ItemGroup>
<ProjectReference Include="..\ShipyardBusinessLogic\ShipyardBusinessLogic.csproj" />
<ProjectReference Include="..\ShipyardDataBaseImplement\ShipyardDataBaseImplement.csproj" />
<ProjectReference Include="..\ShipyardFileImplement\ShipyardFileImplement.csproj" />
<ProjectReference Include="..\ShipyardListImplement»\ShipyardListImplement.csproj" />
</ItemGroup>

View File

@ -7,6 +7,12 @@
</PropertyGroup>
<ItemGroup>
<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>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />

View File

@ -7,6 +7,12 @@
</PropertyGroup>
<ItemGroup>
<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>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />

View File

@ -0,0 +1,90 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement.Implements
{
public class DetailStorage:IDetailStorage
{
public List<DetailViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Details
.Select(x => x.GetViewModel)
.ToList();
}
public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName))
{
return new();
}
using var context = new ShipyardDataBase();
return context.Details
.Where(x => x.DetailName.Contains(model.DetailName))
.Select(x => x.GetViewModel)
.ToList();
}
public DetailViewModel? GetElement(DetailSearchModel model)
{
if (string.IsNullOrEmpty(model.DetailName) && !model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return context.Details
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DetailName) && x.DetailName ==
model.DetailName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public DetailViewModel? Insert(DetailBindingModel model)
{
var newDetail = Detail.Create(model);
if (newDetail == null)
{
return null;
}
using var context = new ShipyardDataBase();
context.Details.Add(newDetail);
context.SaveChanges();
return newDetail.GetViewModel;
}
public DetailViewModel? Update(DetailBindingModel model)
{
using var context = new ShipyardDataBase();
var detail = context.Details.FirstOrDefault(x => x.Id ==
model.Id);
if (detail == null)
{
return null;
}
detail.Update(model);
context.SaveChanges();
return detail.GetViewModel;
}
public DetailViewModel? Delete(DetailBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Details.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Details.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,103 @@
using Microsoft.EntityFrameworkCore;
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement.Implements
{
public class OrderStorage:IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Orders
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.Ship)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return context.Orders
.Include(x=>x.Ship)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ShipyardDataBase();
return context.Orders
.Include(x=>x.Ship)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Orders
.Include(x => x.Ship)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ShipyardDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders
.Include(x => x.Ship)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new ShipyardDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders
.Include(x => x.Ship)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}
}

View File

@ -0,0 +1,110 @@
using Microsoft.EntityFrameworkCore;
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement.Implements
{
public class ShipStorage:IShipStorage
{
public List<ShipViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Ships.Include(x => x.Details)
.ThenInclude(x => x.Detail)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName))
{
return new();
}
using var context = new ShipyardDataBase();
return context.Ships
.Include(x => x.Details)
.ThenInclude(x => x.Detail)
.Where(x => x.ShipName.Contains(model.ShipName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShipViewModel? GetElement(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return context.Ships
.Include(x => x.Details)
.ThenInclude(x => x.Detail)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShipName) &&
x.ShipName == model.ShipName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public ShipViewModel? Insert(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
var newProduct = Ship.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Ships.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ShipViewModel? Update(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Ships.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 ShipViewModel? Delete(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Ships
.Include(x => x.Details)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Ships.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View 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 ShipyardDataBaseImplement;
#nullable disable
namespace ShipyardDataBaseImplement.Migrations
{
[DbContext(typeof(ShipyardDataBase))]
[Migration("20230227171903_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Cost")
.HasColumnType("float");
b.Property<string>("DetailName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Details");
});
modelBuilder.Entity("ShipyardDataBaseImplement.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>("ShipId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ShipId");
b.ToTable("Orders");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ShipName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ships");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DetailId")
.HasColumnType("int");
b.Property<int>("ShipId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("DetailId");
b.HasIndex("ShipId");
b.ToTable("ShipDetails");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Order", b =>
{
b.HasOne("ShipyardDataBaseImplement.Models.Ship", "Ship")
.WithMany("Orders")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b =>
{
b.HasOne("ShipyardDataBaseImplement.Models.Detail", "Detail")
.WithMany("ShipDetails")
.HasForeignKey("DetailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ShipyardDataBaseImplement.Models.Ship", "Ship")
.WithMany("Details")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Detail");
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b =>
{
b.Navigation("ShipDetails");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b =>
{
b.Navigation("Details");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,125 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ShipyardDataBaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Details",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
DetailName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Details", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Ships",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShipName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Ships", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShipId = 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_Ships_ShipId",
column: x => x.ShipId,
principalTable: "Ships",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShipDetails",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShipId = table.Column<int>(type: "int", nullable: false),
DetailId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShipDetails", x => x.Id);
table.ForeignKey(
name: "FK_ShipDetails_Details_DetailId",
column: x => x.DetailId,
principalTable: "Details",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShipDetails_Ships_ShipId",
column: x => x.ShipId,
principalTable: "Ships",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ShipId",
table: "Orders",
column: "ShipId");
migrationBuilder.CreateIndex(
name: "IX_ShipDetails_DetailId",
table: "ShipDetails",
column: "DetailId");
migrationBuilder.CreateIndex(
name: "IX_ShipDetails_ShipId",
table: "ShipDetails",
column: "ShipId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShipDetails");
migrationBuilder.DropTable(
name: "Details");
migrationBuilder.DropTable(
name: "Ships");
}
}
}

View File

@ -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 ShipyardDataBaseImplement;
#nullable disable
namespace ShipyardDataBaseImplement.Migrations
{
[DbContext(typeof(ShipyardDataBase))]
partial class ShipyardDataBaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Cost")
.HasColumnType("float");
b.Property<string>("DetailName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Details");
});
modelBuilder.Entity("ShipyardDataBaseImplement.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>("ShipId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ShipId");
b.ToTable("Orders");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ShipName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ships");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DetailId")
.HasColumnType("int");
b.Property<int>("ShipId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("DetailId");
b.HasIndex("ShipId");
b.ToTable("ShipDetails");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Order", b =>
{
b.HasOne("ShipyardDataBaseImplement.Models.Ship", "Ship")
.WithMany("Orders")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b =>
{
b.HasOne("ShipyardDataBaseImplement.Models.Detail", "Detail")
.WithMany("ShipDetails")
.HasForeignKey("DetailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ShipyardDataBaseImplement.Models.Ship", "Ship")
.WithMany("Details")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Detail");
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b =>
{
b.Navigation("ShipDetails");
});
modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b =>
{
b.Navigation("Details");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,64 @@
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
namespace ShipyardDataBaseImplement.Models
{
public class Detail:IDetailModel
{
public int Id { get; private set; }
[Required]
public string DetailName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("DetailId")]
public virtual List<ShipDetail> ShipDetails { get; set; } = new();
public static Detail? Create(DetailBindingModel model)
{
if (model == null)
{
return null;
}
return new Detail()
{
Id = model.Id,
DetailName = model.DetailName,
Cost = model.Cost
};
}
public static Detail Create(DetailViewModel model)
{
return new Detail
{
Id = model.Id,
DetailName = model.DetailName,
Cost = model.Cost
};
}
public void Update(DetailBindingModel model)
{
if (model == null)
{
return;
}
DetailName= model.DetailName;
Cost = model.Cost;
}
public DetailViewModel GetViewModel => new()
{
Id = Id,
DetailName = DetailName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,71 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Enums;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement.Models
{
public class Order:IOrderModel
{
[Required]
public int ShipId { 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 int Id { get; set; }
public virtual Ship Ship { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
ShipId = model.ShipId,
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,
ShipId = ShipId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ShipName=Ship.ShipName
};
}
}

View File

@ -0,0 +1,99 @@
using ShipyardDataModels.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 ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
namespace ShipyardDataBaseImplement.Models
{
public class Ship:IShipModel
{
public int Id { get; set; }
[Required]
public string ShipName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IDetailModel, int)>? _shipDetails = null;
[NotMapped]
public Dictionary<int, (IDetailModel, int)> ShipDetails
{
get
{
if (_shipDetails == null)
{
_shipDetails = Details.ToDictionary(recPC => recPC.DetailId, recPC =>
(recPC.Detail as IDetailModel, recPC.Count));
}
return _shipDetails;
}
}
[ForeignKey("ShipId")]
public virtual List<ShipDetail> Details { get; set; } = new();
[ForeignKey("ShipId")]
public virtual List<Order> Orders { get; set; } = new();
public static Ship Create(ShipyardDataBase context, ShipBindingModel model)
{
return new Ship()
{
Id = model.Id,
ShipName = model.ShipName,
Price = model.Price,
Details = model.ShipDetails.Select(x => new
ShipDetail
{
Detail = context.Details.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShipBindingModel model)
{
ShipName = model.ShipName;
Price = model.Price;
}
public ShipViewModel GetViewModel => new()
{
Id = Id,
ShipName = ShipName,
Price = Price,
ShipDetails = ShipDetails
};
public void UpdateComponents(ShipyardDataBase context,
ShipBindingModel model)
{
var shipDetails = context.ShipDetails.Where(rec =>
rec.ShipId == model.Id).ToList();
if (shipDetails != null && shipDetails.Count > 0)
{ // удалили те, которых нет в модели
context.ShipDetails.RemoveRange(shipDetails.Where(rec
=> !model.ShipDetails.ContainsKey(rec.DetailId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateDetail in shipDetails)
{
updateDetail.Count = model.ShipDetails[updateDetail.DetailId].Item2;
model.ShipDetails.Remove(updateDetail.DetailId);
}
context.SaveChanges();
}
var ship = context.Ships.First(x => x.Id == Id);
foreach (var pc in model.ShipDetails)
{
context.ShipDetails.Add(new ShipDetail
{
Ship = ship,
Detail = context.Details.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shipDetails = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement.Models
{
public class ShipDetail
{
public int Id { get; set; }
[Required]
public int ShipId { get; set; }
[Required]
public int DetailId { get; set; }
[Required]
public int Count { get; set; }
public virtual Detail Detail { get; set; } = new();
public virtual Ship Ship { get; set; } = new();
}
}

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using ShipyardDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDataBaseImplement
{
public class ShipyardDataBase:DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-M8EKKPQ\SQLEXPRESS;Initial Catalog=ShipyardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Detail> Details { set; get; }
public virtual DbSet<Ship> Ships { set; get; }
public virtual DbSet<ShipDetail> ShipDetails { 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.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>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShipyardContracts\ShipyardContracts.csproj" />
<ProjectReference Include="..\ShipyardDataModels\ShipyardDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -7,6 +7,12 @@
</PropertyGroup>
<ItemGroup>
<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>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />

View File

@ -6,6 +6,15 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<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>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShipyardContracts\ShipyardContracts.csproj" />
<ProjectReference Include="..\ShipyardDataModels\ShipyardDataModels.csproj" />

View File

@ -8,6 +8,12 @@
</PropertyGroup>
<ItemGroup>
<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>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />