From c27f6c966232562d4449e3a2e8ff9b2aa8f98022 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Mon, 27 Feb 2023 21:51:13 +0400 Subject: [PATCH 1/3] LabWork03 Base --- Shipyard/Shipyard.sln | 8 +- Shipyard/Shipyard/Program.cs | 2 +- Shipyard/Shipyard/ShipyardView.csproj | 11 ++ .../ShipyardBusinessLogic.csproj | 6 + .../ShipyardContracts.csproj | 6 + .../Implements/DetailStorage.cs | 90 +++++++++ .../Implements/OrderStorage.cs | 108 +++++++++++ .../Implements/ShipStorage.cs | 110 +++++++++++ .../20230227171903_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230227171903_InitialCreate.cs | 125 +++++++++++++ .../ShipyardDataBaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Detail.cs | 64 +++++++ .../ShipyardDataBaseImplement/Models/Order.cs | 70 +++++++ .../ShipyardDataBaseImplement/Models/Ship.cs | 99 ++++++++++ .../Models/ShipDetail.cs | 24 +++ .../ShipyardDataBase.cs | 29 +++ .../ShipyardDataBaseImplement.csproj | 22 +++ .../ShipyardDataModels.csproj | 6 + .../ShipyardFileImplement.csproj | 9 + .../ShipyardListImplement.csproj | 6 + 20 files changed, 1132 insertions(+), 2 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/20230227171903_InitialCreate.Designer.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_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 16aa712..1238319 100644 --- a/Shipyard/Shipyard.sln +++ b/Shipyard/Shipyard.sln @@ -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 diff --git a/Shipyard/Shipyard/Program.cs b/Shipyard/Shipyard/Program.cs index e77cb2e..3e79094 100644 --- a/Shipyard/Shipyard/Program.cs +++ b/Shipyard/Shipyard/Program.cs @@ -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 diff --git a/Shipyard/Shipyard/ShipyardView.csproj b/Shipyard/Shipyard/ShipyardView.csproj index be242cd..3892717 100644 --- a/Shipyard/Shipyard/ShipyardView.csproj +++ b/Shipyard/Shipyard/ShipyardView.csproj @@ -9,6 +9,16 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +26,7 @@ + diff --git a/Shipyard/ShipyardBusinessLogic/ShipyardBusinessLogic.csproj b/Shipyard/ShipyardBusinessLogic/ShipyardBusinessLogic.csproj index e6df0f8..d14caa2 100644 --- a/Shipyard/ShipyardBusinessLogic/ShipyardBusinessLogic.csproj +++ b/Shipyard/ShipyardBusinessLogic/ShipyardBusinessLogic.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Shipyard/ShipyardContracts/ShipyardContracts.csproj b/Shipyard/ShipyardContracts/ShipyardContracts.csproj index a70e98d..45e1102 100644 --- a/Shipyard/ShipyardContracts/ShipyardContracts.csproj +++ b/Shipyard/ShipyardContracts/ShipyardContracts.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs new file mode 100644 index 0000000..9976a0a --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/DetailStorage.cs @@ -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 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; + } + + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..8369eda --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,108 @@ +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) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ShipyardDataBase(); + return context.Orders + .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 + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new ShipyardDataBase(); + var element = context.Ships + .FirstOrDefault(x => x.Id == order.ShipId); + viewModel.ShipName = element.ShipName; + return viewModel; + } + + public List GetFullList() + { + using var context = new ShipyardDataBase(); + return context.Orders + .Select(x => new OrderViewModel + { + Id = x.Id, + ShipId = x.ShipId, + Count = x.Count, + Sum = x.Sum, + Status = x.Status, + DateCreate = x.DateCreate, + DateImplement = x.DateImplement, + ShipName = x.Ship.ShipName + }) + .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 newOrder.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 order.GetViewModel; + } + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs new file mode 100644 index 0000000..34c433b --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/ShipStorage.cs @@ -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 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; + } + + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_InitialCreate.Designer.cs b/Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_InitialCreate.Designer.cs new file mode 100644 index 0000000..d21b8ab --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_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("20230227171903_InitialCreate")] + partial class InitialCreate + { + /// + 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("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/20230227171903_InitialCreate.cs b/Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_InitialCreate.cs new file mode 100644 index 0000000..8986c9a --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Migrations/20230227171903_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..8013b5f --- /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.3") + .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..d662338 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Detail.cs @@ -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 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 + }; + } + +} + diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Order.cs b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..a3cc7bd --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs @@ -0,0 +1,70 @@ +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 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 + }; + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs b/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs new file mode 100644 index 0000000..aaa5cdc --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/Ship.cs @@ -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? _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; + } + } +} + diff --git a/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs b/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs new file mode 100644 index 0000000..cc2ec9d --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Models/ShipDetail.cs @@ -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(); + + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs new file mode 100644 index 0000000..0d85e8d --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBase.cs @@ -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 Details { set; get; } + public virtual DbSet Ships { set; get; } + public virtual DbSet ShipDetails { set; get; } + public virtual DbSet Orders { set; get; } + } + +} + diff --git a/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj new file mode 100644 index 0000000..39bebaf --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/ShipyardDataBaseImplement.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Shipyard/ShipyardDataModels/ShipyardDataModels.csproj b/Shipyard/ShipyardDataModels/ShipyardDataModels.csproj index daf9ebc..fa1a1ef 100644 --- a/Shipyard/ShipyardDataModels/ShipyardDataModels.csproj +++ b/Shipyard/ShipyardDataModels/ShipyardDataModels.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Shipyard/ShipyardFileImplement/ShipyardFileImplement.csproj b/Shipyard/ShipyardFileImplement/ShipyardFileImplement.csproj index 6f16bc4..79514f7 100644 --- a/Shipyard/ShipyardFileImplement/ShipyardFileImplement.csproj +++ b/Shipyard/ShipyardFileImplement/ShipyardFileImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj b/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj index c7524df..22ef310 100644 --- a/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj +++ b/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj @@ -8,6 +8,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + -- 2.25.1 From 235873dc7a1aeea251ddcfaf821556ebb239ea24 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Tue, 28 Feb 2023 12:31:50 +0400 Subject: [PATCH 2/3] Fixes --- .../Implements/OrderStorage.cs | 16 ++++------------ .../ShipyardDataBaseImplement/Models/Order.cs | 5 +++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs index 8369eda..be227d3 100644 --- a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs +++ b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs @@ -1,4 +1,5 @@ -using ShipyardContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using ShipyardContracts.BindingModels; using ShipyardContracts.SearchModels; using ShipyardContracts.StoragesContracts; using ShipyardContracts.ViewModels; @@ -65,17 +66,8 @@ namespace ShipyardDataBaseImplement.Implements { using var context = new ShipyardDataBase(); return context.Orders - .Select(x => new OrderViewModel - { - Id = x.Id, - ShipId = x.ShipId, - Count = x.Count, - Sum = x.Sum, - Status = x.Status, - DateCreate = x.DateCreate, - DateImplement = x.DateImplement, - ShipName = x.Ship.ShipName - }) + .Include(x => x.Ship) + .Select(x => x.GetViewModel) .ToList(); } diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Order.cs b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs index a3cc7bd..b1d8d01 100644 --- a/Shipyard/ShipyardDataBaseImplement/Models/Order.cs +++ b/Shipyard/ShipyardDataBaseImplement/Models/Order.cs @@ -27,7 +27,7 @@ namespace ShipyardDataBaseImplement.Models public DateTime? DateImplement { get; set; } public int Id { get; set; } - public Ship? Ship { get; set; } + public virtual Ship Ship { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -64,7 +64,8 @@ namespace ShipyardDataBaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + ShipName=Ship.ShipName }; } } -- 2.25.1 From ed2f4e0e6991b18eb844d5451bccfaa16719cb47 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 10 Mar 2023 11:11:14 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B8=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs index be227d3..3e93b67 100644 --- a/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs +++ b/Shipyard/ShipyardDataBaseImplement/Implements/OrderStorage.cs @@ -21,9 +21,13 @@ namespace ShipyardDataBaseImplement.Implements .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 element.GetViewModel; + return deletedElement; } return null; } @@ -36,6 +40,7 @@ namespace ShipyardDataBaseImplement.Implements } using var context = new ShipyardDataBase(); return context.Orders + .Include(x=>x.Ship) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } @@ -48,19 +53,11 @@ namespace ShipyardDataBaseImplement.Implements } using var context = new ShipyardDataBase(); return context.Orders + .Include(x=>x.Ship) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); } - private static OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - using var context = new ShipyardDataBase(); - var element = context.Ships - .FirstOrDefault(x => x.Id == order.ShipId); - viewModel.ShipName = element.ShipName; - return viewModel; - } public List GetFullList() { @@ -81,7 +78,10 @@ namespace ShipyardDataBaseImplement.Implements using var context = new ShipyardDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Ship) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -94,7 +94,10 @@ namespace ShipyardDataBaseImplement.Implements } order.Update(model); context.SaveChanges(); - return order.GetViewModel; + return context.Orders + .Include(x => x.Ship) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; } } } -- 2.25.1