From bb3f6a30f13a39779f66a9f29e557f88026512a6 Mon Sep 17 00:00:00 2001 From: Marselchi Date: Sun, 24 Mar 2024 13:58:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D1=82=D1=80?= =?UTF-8?q?=D0=B5=D1=82=D1=8C=D1=8F=20=D1=84=D1=83=D0=BB=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shipyard/Shipyard.sln | 8 +- .../Implements/DetailStorage.cs | 84 +++++++++ .../Implements/OrderStorage.cs | 98 ++++++++++ .../Implements/ShipStorage.cs | 102 +++++++++++ .../20240310181644_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240310181644_InitialCreate.cs | 125 +++++++++++++ .../ShipyardDataBaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Detail.cs | 57 ++++++ .../ShipyardDataBaseImplement/Models/Order.cs | 64 +++++++ .../ShipyardDataBaseImplement/Models/Ship.cs | 90 +++++++++ .../Models/ShipDetail.cs | 18 ++ .../ShipyardDataBase.cs | 22 +++ .../ShipyardDataBaseImplement.csproj | 23 +++ Shipyard/ShipyardView/FormMain.Designer.cs | 2 +- Shipyard/ShipyardView/Program.cs | 2 +- Shipyard/ShipyardView/ShipyardView.csproj | 6 +- 16 files changed, 1036 insertions(+), 4 deletions(-) create mode 100644 Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.Designer.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Migrations/ShipyardDataBaseModelSnapshot.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Models/Detail.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Models/Order.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Models/Ship.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj diff --git a/Shipyard/Shipyard.sln b/Shipyard/Shipyard.sln index 5014d46..ec3c7f8 100644 --- a/Shipyard/Shipyard.sln +++ b/Shipyard/Shipyard.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardListImplement", "Sh EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardView", "ShipyardView\ShipyardView.csproj", "{BE881F13-7D8F-43B7-AA58-18BBE668FA79}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardFileImplement", "ShipyardFileImplement\ShipyardFileImplement.csproj", "{804C4D1B-2098-4653-8CBA-F5931605A28D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ShipyardFileImplement", "ShipyardFileImplement\ShipyardFileImplement.csproj", "{804C4D1B-2098-4653-8CBA-F5931605A28D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShipyardDataBaseImplement", "ShipyardDataBaseImplement\ShipyardDataBaseImplement.csproj", "{1AAB9FB9-BD51-4EF1-BC80-E17FA9B6851E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {804C4D1B-2098-4653-8CBA-F5931605A28D}.Debug|Any CPU.Build.0 = Debug|Any CPU {804C4D1B-2098-4653-8CBA-F5931605A28D}.Release|Any CPU.ActiveCfg = Release|Any CPU {804C4D1B-2098-4653-8CBA-F5931605A28D}.Release|Any CPU.Build.0 = Release|Any CPU + {1AAB9FB9-BD51-4EF1-BC80-E17FA9B6851E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1AAB9FB9-BD51-4EF1-BC80-E17FA9B6851E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1AAB9FB9-BD51-4EF1-BC80-E17FA9B6851E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1AAB9FB9-BD51-4EF1-BC80-E17FA9B6851E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs new file mode 100644 index 0000000..a973634 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs @@ -0,0 +1,84 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardDataBaseImplement.Models; + +namespace ShipyardDataBaseImplement.Implements +{ + public class DetailStorage : IDetailStorage + { + public List GetFullList() + { + using var context = new ShipyardDataBase(); + return context.Details + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..a9061f8 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,98 @@ +using Microsoft.EntityFrameworkCore; +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardDataBaseImplement.Models; + +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 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 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; + } + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs new file mode 100644 index 0000000..81c6103 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs @@ -0,0 +1,102 @@ +using Microsoft.EntityFrameworkCore; +using ShipyardContracts.BindingModels; +using ShipyardContracts.SearchModels; +using ShipyardContracts.StoragesContracts; +using ShipyardContracts.ViewModels; +using ShipyardDataBaseImplement.Models; + +namespace ShipyardDataBaseImplement.Implements +{ + public class ShipStorage : IShipStorage + { + public List 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 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; + } + + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.Designer.cs b/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.Designer.cs new file mode 100644 index 0000000..c4dcc47 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240310181644_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("DetailName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Details"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ShipId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ShipId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ShipName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ships"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DetailId") + .HasColumnType("int"); + + b.Property("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 + } + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.cs b/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.cs new file mode 100644 index 0000000..8986c9a --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Migrations/20240310181644_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ShipyardDataBaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Details", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DetailName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Details", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Ships", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShipName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Ships", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShipId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Ships_ShipId", + column: x => x.ShipId, + principalTable: "Ships", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShipDetails", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShipId = table.Column(type: "int", nullable: false), + DetailId = table.Column(type: "int", nullable: false), + Count = table.Column(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"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ShipDetails"); + + migrationBuilder.DropTable( + name: "Details"); + + migrationBuilder.DropTable( + name: "Ships"); + } + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Migrations/ShipyardDataBaseModelSnapshot.cs b/Shipyard/ShipyardDataBaseImplement/Migrations/ShipyardDataBaseModelSnapshot.cs new file mode 100644 index 0000000..b512989 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Migrations/ShipyardDataBaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Detail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("DetailName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Details"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ShipId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ShipId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.Ship", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ShipName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ships"); + }); + + modelBuilder.Entity("ShipyardDataBaseImplement.Models.ShipDetail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DetailId") + .HasColumnType("int"); + + b.Property("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 + } + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Detail.cs b/Shipyard/ShipyardDataBaseImplement/Models/Detail.cs new file mode 100644 index 0000000..e7e05a3 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Detail.cs @@ -0,0 +1,57 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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 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 + }; + } + +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Order.cs b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..a2f5ce5 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs @@ -0,0 +1,64 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Enums; +using ShipyardDataModels.Models; +using System.ComponentModel.DataAnnotations; + +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 + }; + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs b/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs new file mode 100644 index 0000000..63c008e --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs @@ -0,0 +1,90 @@ +using ShipyardContracts.BindingModels; +using ShipyardContracts.ViewModels; +using ShipyardDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +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? _shipDetails = null; + [NotMapped] + public Dictionary 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 Details { get; set; } = new(); + [ForeignKey("ShipId")] + public virtual List 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; + } + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs b/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs new file mode 100644 index 0000000..4f614ed --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +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(); + + } +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs new file mode 100644 index 0000000..8c69374 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using ShipyardDataBaseImplement.Models; + +namespace ShipyardDataBaseImplement +{ + public class ShipyardDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-JJTVLCG\SQLEXPRESS02;Initial Catalog=ShipyardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Details { set; get; } + public virtual DbSet Ships { set; get; } + public virtual DbSet ShipDetails { set; get; } + public virtual DbSet Orders { set; get; } + } + +} \ No newline at end of file diff --git a/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj new file mode 100644 index 0000000..6679c67 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Shipyard/ShipyardView/FormMain.Designer.cs b/Shipyard/ShipyardView/FormMain.Designer.cs index e273ee8..940ddd5 100644 --- a/Shipyard/ShipyardView/FormMain.Designer.cs +++ b/Shipyard/ShipyardView/FormMain.Designer.cs @@ -156,7 +156,7 @@ Controls.Add(dataGridView); Controls.Add(toolStrip); Name = "FormMain"; - Text = "FormMain"; + Text = "Форма заказов"; Load += FormMain_Load; toolStrip.ResumeLayout(false); toolStrip.PerformLayout(); diff --git a/Shipyard/ShipyardView/Program.cs b/Shipyard/ShipyardView/Program.cs index 0a6aa03..2d6ed7d 100644 --- a/Shipyard/ShipyardView/Program.cs +++ b/Shipyard/ShipyardView/Program.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using ShipyardBusinessLogic.BusinessLogics; using ShipyardContracts.BusinessLogicsContracts; using ShipyardContracts.StoragesContracts; -using ShipyardFileImplement.Implements; +using ShipyardDataBaseImplement.Implements; using NLog.Extensions.Logging; namespace ShipyardView diff --git a/Shipyard/ShipyardView/ShipyardView.csproj b/Shipyard/ShipyardView/ShipyardView.csproj index 6a65f03..f4cc43c 100644 --- a/Shipyard/ShipyardView/ShipyardView.csproj +++ b/Shipyard/ShipyardView/ShipyardView.csproj @@ -13,12 +13,16 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - +